-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcronJob.js
More file actions
148 lines (113 loc) · 3.76 KB
/
cronJob.js
File metadata and controls
148 lines (113 loc) · 3.76 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const CronJob = require('cron').CronJob;
const moment = require('moment');
const sequelize = require('sequelize');
const WeixinAction = require('./lib/WeixinAction');
const models = require('./models');
const MetaModel = models.MetaModel;
const UserModel = models.UserModel;
const NoticeModel = models.NoticeModel;
const status = require('./config/status.json');
// 每隔一小时获取 access_token
(new CronJob('0 0 */1 * * *', async function () {
const AT = require('./lib/AccessToken');
try {
let at = new AT();
let token = await at.getToken();
let expire = moment().format('X') - 0 + 3600; // 一小时刷新一次
await AT.setAccessToken(token, expire);
console.log('save access token success = ' + token);
} catch (err) {
console.log(err);
}
}, null, true, 'PRC')).start();
// 每隔一小时重置 answer_mode
(new CronJob('0 0 */1 * * *', async function () {
const Op = sequelize.Op;
let time = moment.unix(moment().format('X') - 3600).format('YYYY-MM-D H:m:s')
MetaModel
.findAll({
where: {
key: 'answer_mode',
value: 'robot_answer',
updatedAt: {
[Op.lte]: time
}
}
})
.then(list => {
return Promise.all(list.map(item => {
item.value = 'normal_answer';
return item.save();
})
);
})
.then(res => {
console.log(res.length);
})
.catch(err => {
console.log(err)
})
}, null, true, 'PRC')).start();
// 每隔一小时尝试获取 status = 11 的 user 信息
(new CronJob('0 30 */1 * * *', function () {
let wxAction = new WeixinAction();
UserModel
.findAll({
where: {
'status': status.user.pending,
}
})
.then(list => {
return Promise.all(list.map(item => {
return wxAction.getUserInfo(item.openid)
.then(userInfo => {
UserModel.saveUserByApiData(item.openid, userInfo)
})
})
);
})
.then(res => {
console.log(res.length)
})
.catch(err => {
console.log(err)
})
}, null, true, 'PRC')).start();
// 每隔 5 分钟发送周期性通知
(new CronJob('0 */5 * * * *', async function () {
let list = await NoticeModel.listByRepeat('day');
let pas = [];
list.forEach((item) => {
[alertTimeStamp, noticeTime] = NoticeModel.calAlertTime(item.notice_time, item.alert_time_options, item.repeat_frequency)
let now = moment().format('X');
if (now >= alertTimeStamp && now <= alertTimeStamp + 300) {
pas.push(NoticeModel.generateSendPromise(item, noticeTime))
}
})
Promise.all(pas)
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
}, null, true, 'PRC')).start();
// 每隔 5 分钟发送一次性通知
(new CronJob('0 */5 * * * *', async function () {
let list = await NoticeModel.listByRepeat('never');
let pas = [];
list.forEach((item) => {
[alertTimeStamp, noticeTime] = NoticeModel.calAlertTime(item.notice_time, item.alert_time_options, item.repeat_frequency)
let now = moment().format('X');
if (now >= alertTimeStamp && now <= alertTimeStamp + 300) {
pas.push(NoticeModel.generateSendPromise(item, noticeTime))
}
})
Promise.all(pas)
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
}, null, true, 'PRC')).start();