forked from lukesyy/jd_yun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (161 loc) · 4.81 KB
/
index.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
//'use strict';
exports.main_handler = async (event, context, callback) => {
let params = {}
let scripts = []
if (event['TriggerName'] == 'config') {
let now_hour = (new Date().getUTCHours() + 8) % 24
console.log('hourly config触发,当前:', now_hour)
if (event['Message']) {
const hour = Number(event['Message'])
if (!isNaN(hour) && hour >= 0 && hour <= 23) {
now_hour = hour
console.log('hourly config触发,自定义触发小时:', now_hour)
}
}
const { readFileSync, accessSync, constants } = require('fs')
const config_file = process.cwd() + '/config.json'
try {
await accessSync(config_file, constants.F_OK)
console.log(`${config_file} 存在`)
} catch (err) {
console.error(`${config_file} 不存在,结束`)
return
}
let config
try {
config = JSON.parse(await readFileSync(config_file))
} catch (e) {
console.error(`read config error:${e}`)
return
}
// console.debug(JSON.stringify(config))
params = config['params']
delete config['params']
const config_diy_file = process.cwd() + '/config_diy.json'
try {
await accessSync(config_diy_file, constants.F_OK)
console.log(`${config_diy_file} 存在`)
const config_diy = JSON.parse(await readFileSync(config_diy_file))
if (config_diy['params']) {
params = { ...params, ...config_diy['params'] }
delete config_diy['params']
}
config = { ...config, ...config_diy }
} catch (err) {
console.error(`${config_diy_file} 不存在或解析异常`)
}
console.log('params:', params)
for (let script in config) {
// console.debug(`script:${script}`)
const cron = config[script]
if (typeof cron == 'number') {
// console.debug(`number param:${cron}`)
if (now_hour % cron == 0) {
console.debug(`${script}:number cron triggered`)
scripts.push(script)
}
} else {
// console.debug(`dict param:${cron}`)
if (cron.includes(now_hour)) {
console.debug(`${script}:array cron triggered`)
scripts.push(script)
}
}
}
} else {
if (!event['Message']) {
console.error('未接收到任何参数,请阅读@hshx123大佬教程的测试步骤,查看如何使用.')
return
}
console.log('参数触发方式(不读取配置文件),触发参数:', event['Message'])
scripts = event['Message'].split('&')
}
if (process.env.NOT_RUN) {
const not_run = process.env.NOT_RUN.split('&')
scripts = scripts.filter(script => {
const flag = not_run.includes(script)
if (flag) {
console.log(`not run:${script}`)
}
return !flag
})
}
if (!scripts.length) {
console.log('No Script to Execute, Exit!')
return
}
const is_sync = params['global'] && params['global']['exec'] == 'sync'
console.log('当前是', is_sync ? '同' : '异', '步执行')
if (is_sync) {
const { execFile } = require('child_process')
const min = 1000 * 60
const param_names = ['timeout']
for (const script of scripts) {
const name = './' + script + '.js'
const param_run = {}
const param = params[script]
for (const param_name of param_names) {
if (param) {
if (param[param_name]) {
console.debug(`${script} has specific ${param_name}:${param[param_name]}`)
param_run[param_name] = min * param[param_name]
}
} else if (params['global'] && params['global'][param_name]) {
console.debug(`${script} use global ${param_name}`)
param_run[param_name] = min * params['global'][param_name]
} else {
console.warn(`No global ${param_name}!`)
}
}
console.log(`run script:${script}`)
try {
await (async () => {
return new Promise(resolve => {
const child = execFile(process.execPath, [name], param_run)
child.stdout.on('data', function (data) {
console.log(data)
})
child.stderr.on('data', function (data) {
console.error(data)
})
child.on('close', function (code) {
console.log(`${script} finished`)
delete child
resolve()
})
})
})()
} catch (e) {
console.error(`${script} ERROR:${e}`)
console.error(`stdout:${e.stdout}`)
}
}
} else {
console.log('异步执行不支持params参数!')
;['log', 'warn', 'error', 'debug', 'info'].forEach(methodName => {
const originalMethod = console[methodName]
console[methodName] = (...args) => {
try {
throw new Error()
} catch (error) {
let stack = error.stack // Grabs the stack trace
.split('\n')[2] // Grabs third line
.split('/')
.slice(-1)[0] // Grabs file name and line number
.replace('.js', '')
stack = `${stack.substring(0, stack.lastIndexOf(':'))}:`
originalMethod.apply(console, [stack, ...args])
}
}
})
for (const script of scripts) {
console.log(`run script:${script}`)
const name = './' + script + '.js'
try {
require(name)
} catch (e) {
console.error(`异步${script}异常:`, e)
}
}
}
}