-
Notifications
You must be signed in to change notification settings - Fork 354
/
utils_task.js
273 lines (260 loc) · 6.8 KB
/
utils_task.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// @grant sudo
// 查看当前任务数
console.log($task.status());
/*
--------------------
{ running: 13, total: 20, sub: 2 }
--------------------
*/
// 添加任务(具体任务格式参考 https://github.com/elecV2/elecV2P-dei/blob/master/docs/06-task.md)
let res = $task.add(
[
{
name: '$task 添加的任务 1',
type: 'cron',
time: '12 15 18 * * *',
job: {
type: 'exec',
target: 'pm2 ls',
},
},
{
name: '$task 添加的任务 2',
type: 'cron',
time: '12 15 18 * * *',
job: {
type: 'exec',
target: 'pm2 ls',
},
},
],
{ type: 'replace' }
);
console.log('$task 添加任务结果', res);
/*
--------------------
>> success
$task 添加任务结果 {
rescode: 0,
message: 'TASK: $task 添加的任务 1 started\n TASK: $task 添加的任务 2 started'
}
>> failure
$task 添加任务结果 {
rescode: 0,
message: 'some task parameters may be invalid(please check docs: https://github.com/elecV2/elecV2P-dei/blob/master/docs/06-task.md)\n' +
'some task parameters may be invalid(please check docs: https://github.com/elecV2/elecV2P-dei/blob/master/docs/06-task.md)'
}
--------------------
*/
// 如果需要添加多个任务,可使用 array 数组的形式
// 比如 $task.add([{}, {}], { type: 'replace' })
// 第二个参数 options 可省略。
// 如设置 type, 表示同名任务的更新方式。有三个有效值:
// - replace 替换原同名任务
// - addition 新增同名任务
// - skip 跳过添加同名任务
// 获取任务名及对应 taskid
let tnlist = $task.nameList();
console.log(tnlist);
/*
--------------------
{
'清空日志': 'BPvOKSlv',
'软更新升级': 'rswqs1uC',
'Python安装(Docker下)': 'Au7FLiaY',
'重启 elecV2P': 'xVrbflqZ',
'任务添加并执行': 'qM9b9JAN',
'Shell 指令远程任务': 'nbLrklPI',
...
}
--------------------
*/
// 返回的是类似于 { '任务名': taskid } 的 object
// 通过该 object,可使用任务名快速查找任务 id
// 如果任务列表不经常变化的话建议使用 $store.put 或 $cache 保存
// 开始任务
console.log($task.start('BPvOKSlv'));
/*
--------------------
{
rescode: 0,
message: '清空日志 is running',
taskinfo: {
name: '清空日志',
type: 'cron',
time: '30 18 23 * * *',
job: {
type: 'runjs',
target: 'https://raw.githubusercontent.com/elecV2/elecV2P/master/script/JSFile/deletelog.js'
},
id: 'BPvOKSlv',
running: true,
group: 'Jyhniazi'
}
}
--------------------
*/
// 停止任务
console.log($task.stop(tnlist['清空日志'])); // 通过任务名查找任务 id
/*
--------------------
{
rescode: 0,
message: '清空日志 stopped',
taskinfo: {
name: '清空日志',
type: 'cron',
time: '30 18 23 * * *',
job: {
type: 'runjs',
target: 'https://raw.githubusercontent.com/elecV2/elecV2P/master/script/JSFile/deletelog.js'
},
id: 'BPvOKSlv',
running: false,
group: 'Jyhniazi'
}
}
--------------------
*/
// 删除任务
console.log($task.delete('BPvOKSlv'));
/*
--------------------
{ rescode: 0, message: 'TASK cron 清空日志 deleted' }
--------------------
*/
// 使用数组的形式传入 taskid,可批量开始/暂停/删除 定时任务
$task.start(['m8LWPxDc', 'ataskid', tnlist['$task 添加的任务'], 'jxwQOSJZ']);
// $task.stop(['taskid1', 'taskid2', ...])
// $task.delete(['taskid1', 'taskid2', ...])
// 查看某个任务信息
let taskinfo = $task.info('rswqs1uC'); // 查看所有任务信息 $task.info() 或者 $task.info('all')
console.log(taskinfo);
/*
--------------------
>> single
{
name: '软更新升级',
type: 'cron',
time: '30 58 23 * * *',
job: {
type: 'runjs',
target: 'https://raw.githubusercontent.com/elecV2/elecV2P/master/script/JSFile/softupdate.js'
},
id: 'rswqs1uC',
running: true,
group: 'Jyhniazi'
}
--------------------
>> all
{
rswqs1uC: {
name: '软更新升级',
type: 'cron',
time: '30 58 23 * * *',
job: {
type: 'runjs',
target: 'https://raw.githubusercontent.com/elecV2/elecV2P/master/script/JSFile/softupdate.js'
},
id: 'rswqs1uC',
running: true,
group: 'Jyhniazi'
},
Au7FLiaY: {
name: 'Python安装(Docker下)',
type: 'schedule',
time: '0',
job: {
type: 'runjs',
target: 'https://raw.githubusercontent.com/elecV2/elecV2P/master/script/JSFile/python-install.js'
},
running: false,
id: 'Au7FLiaY',
group: 'Jyhniazi'
},
xVrbflqZ: {
name: '重启 elecV2P',
type: 'schedule',
time: '0',
job: { type: 'exec', target: 'pm2 restart elecV2P' },
running: false,
id: 'xVrbflqZ',
group: 'Jyhniazi'
},
qM9b9JAN: {
name: '任务添加并执行',
type: 'schedule',
time: '10',
job: { type: 'exec', target: 'node -v' },
running: false,
id: 'qM9b9JAN',
group: 'Jyhniazi'
},
nbLrklPI: {
name: 'Shell 指令远程任务',
type: 'schedule',
time: '0',
job: {
type: 'exec',
target: 'python3 https://raw.githubusercontent.com/elecV2/elecV2P/master/script/Shell/elecV2P-exam.py'
},
running: false,
id: 'nbLrklPI',
group: 'Jyhniazi'
},
WrOz0txU: {
name: 'News',
type: 'sub',
job: {
type: 'replace',
target: 'https://raw.githubusercontent.com/Oreomeow/VIP/main/Tasks/News.json'
},
update_type: 'none'
},
FSEh4K9D: {
name: 'Wool',
type: 'group',
note: '羊毛项目',
bkcolor: '#3de1ad',
collapse: true,
total: 1,
active: 0
},
Jyhniazi: {
name: 'elecV2P',
type: 'group',
note: '系统任务',
bkcolor: '#f47983',
collapse: true,
total: 6,
active: 2
},
UiRsiwmO: {
name: 'News',
type: 'group',
note: '新闻讯息',
bkcolor: '#30dff3',
collapse: true,
total: 7,
active: 5
},
...
}
--------------------
*/
// 查询 __taskid/__taskname (仅在使用定时任务运行脚本时有值,其他情况默认为 undefined
console.log('执行该脚本的任务名:', __taskname);
console.log('相关任务信息', $task.info(__taskid));
// 尝试使用 __taskid 来停止自身定时任务
if (__taskid) {
let stopinfo = $task.stop(__taskid); // 停止自身定时任务
console.log(stopinfo);
}
// 保存当前任务列表
let saveres = $task.save();
console.log(saveres);
/*
--------------------
{ rescode: 0, message: 'success save current task list 12/19/2' }
--------------------
*/