서로 다른 간격으로 작업을 수행 할 수있는 Nodejs 스케줄러가 필요합니다.
다른 간격으로 여러 작업을 예약 할 수있는 노드 작업 일정을 찾고 있습니다. 예를 들어
- 30 초마다 통화 기능 A
- 60 초마다 함수 B 호출
- 7 일마다 함수 C 호출
또한 프로세스를 시작하고 중지 할 수 있기를 원합니다.
지금까지 살펴 보았습니다.
나중에 -구문이 혼란 스럽습니다. 또한 한 달 이상 작업을 예약 할 수 없습니다.
의제 -가장 유망한 것으로 보이지만 데이터베이스 기능에 대해 혼란 스럽습니다.
타임 플랜 -너무 단순해서 시작 및 중지 할 수 없음
나는 후자의 구문이 혼란 스럽다는 것을 발견했다.
나는 추천 할 것이다 node-cron
. Cron 패턴을 사용하여 작업 을 실행할 수 있습니다.
'* * * * * *' - runs every second
'*/5 * * * * *' - runs every 5 seconds
'10,20,30 * * * * *' - run at 10th, 20th and 30th second of every minute
'0 * * * * *' - runs every minute
'0 0 * * * *' - runs every hour (at 0 minutes and 0 seconds)
그러나 더 복잡한 스케줄
'00 30 11 * * 1-5' - Runs every weekday (Monday through Friday) at 11:30:00 AM. It does not run on Saturday or Sunday.
샘플 코드 : 10 분마다 작업 실행 :
var cron = require('cron');
var cronJob = cron.job("0 */10 * * * *", function(){
// perform operation e.g. GET request http.get() etc.
console.info('cron job completed');
});
cronJob.start();
node-cron Wiki 에서 더 많은 예제를 찾을 수 있습니다
크론 구성에 대한 자세한 내용은 크론 위키 에서 찾을 수 있습니다.
많은 프로젝트에서 해당 라이브러리를 사용하고 있으며 작업을 수행합니다. 도움이 되길 바랍니다.
node-cron 은 매우 간단한 라이브러리로 crontab과 같은 매우 기본적이고 이해하기 쉬운 API를 제공합니다. 구성이 필요하지 않으며 작동합니다.
var cronJob = require('cron').CronJob;
var myJob = new cronJob('00 30 11 * * 1-5', function(){...});
myJob.start();
의제 는 매우 강력하며 훨씬 복잡한 서비스에 적합합니다. ifttt 에 대해 생각하십시오 . 수백만 개의 작업을 실행해야합니다. 의제 가 최선의 선택이 될 것입니다.
참고 : 일정을 사용 하려면 Mongodb 가 필요합니다
var Agenda = require("Agenda");
var agenda = new Agenda({db: { address: 'localhost:27017/agenda-example'}});
agenda.every('*/3 * * * *', 'delete old users');
agenda.start();
최고의 순위는
1. 노드 일정
2. 나중에
3. 크론 탭
노드 일정 샘플 은 다음과 같습니다.
var schedule = require("node-schedule");
var rule = new schedule.RecurrenceRule();
//rule.minute = 40;
rule.second = 10;
var jj = schedule.scheduleJob(rule, function(){
console.log("execute jj");
});
노드 모듈 에서 답을 찾을 수 있습니다 .
선언적 인터페이스를 제공하는 모멘트 지속 시간을 사용하여 setInterval 주위에 래퍼를 제공하는 노드 모듈을 작성했습니다.
npm 매 순간 설치
var every = require('every-moment');
var timer = every(5, 'seconds', function() {
console.log(this.duration);
});
every(2, 'weeks', function() {
console.log(this.duration);
timer.stop();
this.set(1, 'week');
this.start();
});
https://www.npmjs.com/package/every-moment
https://github.com/raygerrard/every-moment
nodeJS 기본값
https://nodejs.org/api/timers.html
setInterval(function() {
// your function
}, 5000);
I have written a small module to do just that, called timexe:
- Its simple,small reliable code and has no dependencies
- Resolution is in milliseconds and has high precision over time
- Cron like, but not compatible (reversed order and other Improvements)
- I works in the browser too
Install:
npm install timexe
use:
var timexe = require('timexe');
//Every 30 sec
var res1=timexe(”* * * * * /30”, function() console.log(“Its time again”)});
//Every minute
var res2=timexe(”* * * * *”,function() console.log(“a minute has passed”)});
//Every 7 days
var res3=timexe(”* y/7”,function() console.log(“its the 7th day”)});
//Every Wednesdays
var res3=timexe(”* * w3”,function() console.log(“its Wednesdays”)});
// Stop "every 30 sec. timer"
timexe.remove(res1.id);
you can achieve start/stop functionality by removing/re-adding the entry directly in the timexe job array. But its not an express function.
'Programing' 카테고리의 다른 글
C #에서 클래스는 다른 클래스와 인터페이스에서 상속 할 수 있습니까? (0) | 2020.07.11 |
---|---|
Eclipse의 패키지 탐색기에서 트리 항목의 글꼴 크기를 어떻게 구성 할 수 있습니까? (0) | 2020.07.11 |
Java Keystore를 PEM 형식으로 변환 (0) | 2020.07.11 |
Angular에서 angular.copy의 대안 (0) | 2020.07.11 |
현재 도메인 받기 (0) | 2020.07.11 |