crontab.js
1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"use strict";
const config = require('./init/config');
// 定义log4js 包含业务日志和系统日志
const logger = require('./init/log4js-init').system;
// 定义db
logger.info('init db');
require('./init/sequelize-init');
//bind exception event to log
process.on('uncaughtException', function (e) {
logger.error('uncaughtException from process', e);
});
process.on('unhandledRejection', (e) => {
logger.warn('unhandledRejection from process', e);
});
process.on('rejectionHandled', (e) => {
logger.warn('rejectionHandled from process', e);
});
// 准备定时器
const redis_client = require('./init/redis-promisify'),
LockKey = require('kml-redis-tools').LockKey;
const lockKey = new LockKey(redis_client);
lockKey.lockKey('cron:' + config.system.project_name, config.cache.ttl.CRON_LOCK)
.then(lock_result => {
if (lock_result.locked) {
const task_list = require('./cron')();
if (!task_list || !task_list.length) {
logger.warn('没有定时器被开启。');
}
task_list.forEach(task => {
task && task.start && task.start();
});
} else {
logger.warn('定时器被锁定,请检查是否已有其他进程负责。');
}
});