biz-a-cli 2.3.15 → 2.3.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/app.js +313 -0
- package/bin/hub.js +1 -1
- package/bin/hubEvent.js +6 -5
- package/bin/proxy.js +1 -1
- package/bin/watcher.js +1 -1
- package/callbackController.js +15 -18
- package/envs/env.dev.js +2 -1
- package/envs/env.js +2 -1
- package/mailController.js +2 -2
- package/package.json +11 -6
- package/readme.md +60 -0
- package/scheduler/datalib.js +45 -49
- package/scheduler/timer.js +8 -14
- package/scheduler/watcherController.js +12 -22
- package/tests/app.test.js +384 -0
- package/tests/callback.test.js +8 -17
- package/tests/data.test.js +20 -2
- package/tests/hub.test.js +4 -8
- package/tests/watcherCtl.test.js +9 -12
- package/bin/log/debug.log +0 -0
- package/bin/log/error.log +0 -0
- package/bin/log/exception.log +0 -38
- package/bin/log/info.log +0 -0
package/scheduler/datalib.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { map, tap } from "rxjs";
|
|
2
|
-
import { Axios } from 'axios-observable';
|
|
3
1
|
import { runInThisContext, constants } from "vm";
|
|
4
2
|
import { insertHistory } from "./watcherController.js";
|
|
5
3
|
import axios from "axios";
|
|
@@ -7,7 +5,7 @@ import * as dayjs from "dayjs";
|
|
|
7
5
|
import timezone from "dayjs/plugin/timezone.js";
|
|
8
6
|
import utc from "dayjs/plugin/utc.js"
|
|
9
7
|
import crypto from "crypto";
|
|
10
|
-
import {createLogger, transports, format
|
|
8
|
+
import { createLogger, transports, format } from "winston";
|
|
11
9
|
|
|
12
10
|
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
|
|
13
11
|
dayjs.default.extend(customParseFormat)
|
|
@@ -15,16 +13,16 @@ dayjs.default.extend(timezone)
|
|
|
15
13
|
dayjs.default.extend(utc)
|
|
16
14
|
|
|
17
15
|
const logger = createLogger({
|
|
18
|
-
level:'info',
|
|
19
|
-
transports:[
|
|
20
|
-
new transports.File({filename:'log/error.log', level:'error'}),
|
|
21
|
-
new transports.File({filename:'log/debug.log', level:'debug'}),
|
|
22
|
-
new transports.File({filename:'log/info.log', level:'info'}),
|
|
23
|
-
new transports.File({filename:'log/exception.log', handleExceptions:true})
|
|
16
|
+
level: 'info',
|
|
17
|
+
transports: [
|
|
18
|
+
new transports.File({ filename: 'log/error.log', level: 'error' }),
|
|
19
|
+
new transports.File({ filename: 'log/debug.log', level: 'debug' }),
|
|
20
|
+
new transports.File({ filename: 'log/info.log', level: 'info' }),
|
|
21
|
+
new transports.File({ filename: 'log/exception.log', handleExceptions: true })
|
|
24
22
|
],
|
|
25
23
|
})
|
|
26
|
-
if (process.env.NODE_ENV !== 'production'){
|
|
27
|
-
logger.add(new transports.Console({format: format.simple(), level:'info'}))
|
|
24
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
25
|
+
logger.add(new transports.Console({ format: format.simple(), level: 'info' }))
|
|
28
26
|
}
|
|
29
27
|
|
|
30
28
|
|
|
@@ -40,7 +38,7 @@ if (process.env.NODE_ENV !== 'production'){
|
|
|
40
38
|
|
|
41
39
|
function mapData2Key(res, cols) {
|
|
42
40
|
return res.map(e => {
|
|
43
|
-
return cols.reduce((o, k) => {
|
|
41
|
+
return cols.reduce((o, k) => {
|
|
44
42
|
o[k.key ? k.key : k.data] = e[k.data]
|
|
45
43
|
return o;
|
|
46
44
|
}, {})
|
|
@@ -92,10 +90,10 @@ export function sendModel(model, apiConfig, tableName) {
|
|
|
92
90
|
params: { subdomain: apiConfig.subdomain }
|
|
93
91
|
}
|
|
94
92
|
|
|
95
|
-
return
|
|
93
|
+
return axios.post(url, params, options)
|
|
96
94
|
}
|
|
97
95
|
|
|
98
|
-
export function queryData(param, apiConfig, mapField) {
|
|
96
|
+
export async function queryData(param, apiConfig, mapField) {
|
|
99
97
|
const defaultParam = {
|
|
100
98
|
start: 0, length: -1, order: [], filter: [], columns: []
|
|
101
99
|
};
|
|
@@ -112,17 +110,12 @@ export function queryData(param, apiConfig, mapField) {
|
|
|
112
110
|
params: { subdomain: apiConfig.subdomain }
|
|
113
111
|
}
|
|
114
112
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
map(res => {
|
|
119
|
-
if (!mapField || res.error) return res;
|
|
120
|
-
return mapData2Key(res, param.columns)
|
|
121
|
-
})
|
|
122
|
-
)
|
|
113
|
+
let res = await axios.post(url, params, options);
|
|
114
|
+
res = res.data?.data ? JSON.parse(res.data.data) : res.data;
|
|
115
|
+
return (!mapField || res.error) ? res : mapData2Key(res, param.columns);
|
|
123
116
|
}
|
|
124
117
|
|
|
125
|
-
export function crudData(param, apiConfig, method, path) {
|
|
118
|
+
export async function crudData(param, apiConfig, method, path) {
|
|
126
119
|
const { url, params } = getUrlAndParam(
|
|
127
120
|
apiConfig,
|
|
128
121
|
method,
|
|
@@ -135,20 +128,18 @@ export function crudData(param, apiConfig, method, path) {
|
|
|
135
128
|
params: { subdomain: apiConfig.subdomain }
|
|
136
129
|
}
|
|
137
130
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
)
|
|
131
|
+
const res = await axios.post(url, params, options);
|
|
132
|
+
return res.data?.data ? JSON.parse(res.data.data) : res.data;
|
|
141
133
|
}
|
|
142
134
|
|
|
143
|
-
export function genId(apiConfig, genName) {
|
|
135
|
+
export async function genId(apiConfig, genName) {
|
|
144
136
|
const url = `${getUrlApi(apiConfig, 'genId', false)}/${genName}/${apiConfig.dbindex}`;
|
|
145
137
|
const options = {
|
|
146
138
|
headers: { 'content-type': 'text/plain' },
|
|
147
139
|
params: { subdomain: apiConfig.subdomain }
|
|
148
140
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
)
|
|
141
|
+
const res = await axios.get(url, options);
|
|
142
|
+
return res.data?.data ? JSON.parse(res.data.data) : res.data;
|
|
152
143
|
}
|
|
153
144
|
|
|
154
145
|
export function extractFunctionScript(data) {
|
|
@@ -161,13 +152,12 @@ export function extractFunctionScript(data) {
|
|
|
161
152
|
const scriptFn = runInThisContext(data[0].script, { importModuleDynamically: constants.USE_MAIN_CONTEXT_DEFAULT_LOADER });
|
|
162
153
|
if (!scriptFn().functions) return
|
|
163
154
|
let lib = {
|
|
164
|
-
axios
|
|
165
|
-
dayjs
|
|
155
|
+
axios: axios.default,
|
|
156
|
+
dayjs: dayjs.default,
|
|
166
157
|
crypto: crypto,
|
|
167
|
-
log
|
|
158
|
+
log: logger,
|
|
168
159
|
}
|
|
169
160
|
return scriptFn(lib).functions;
|
|
170
|
-
// return JSON.parse(JSON.stringify(scriptFn.functions), deserialise)
|
|
171
161
|
}
|
|
172
162
|
|
|
173
163
|
export function getQueryDataObsParameters(trigger) {
|
|
@@ -178,7 +168,7 @@ export function getQueryDataObsParameters(trigger) {
|
|
|
178
168
|
}
|
|
179
169
|
}
|
|
180
170
|
|
|
181
|
-
export function
|
|
171
|
+
export function queryDataPromise(config, trigger) {
|
|
182
172
|
let param = getQueryDataObsParameters(trigger);
|
|
183
173
|
|
|
184
174
|
return queryData(param, config, true);
|
|
@@ -191,9 +181,7 @@ export function getConfig(config) {
|
|
|
191
181
|
export function loadCliScript(selectedConfig, trigger) {
|
|
192
182
|
const config = getConfig(selectedConfig);
|
|
193
183
|
|
|
194
|
-
return
|
|
195
|
-
// throttleTime(100)
|
|
196
|
-
)
|
|
184
|
+
return queryDataPromise(config, trigger);
|
|
197
185
|
}
|
|
198
186
|
|
|
199
187
|
export function getInputData(config, trigger) {
|
|
@@ -203,13 +191,21 @@ export function getInputData(config, trigger) {
|
|
|
203
191
|
hostname: urlAndPort[1].split('//')[1],
|
|
204
192
|
port: +urlAndPort[2],
|
|
205
193
|
dbindex: config.dbindex,
|
|
206
|
-
|
|
194
|
+
finaDbIndex: config.finaDbIndex,
|
|
195
|
+
subdomain: config.subdomain,
|
|
196
|
+
smtp: {
|
|
197
|
+
user: config.smtp.auth.user,
|
|
198
|
+
pass:config.smtp.auth.pass,
|
|
199
|
+
host: config.smtp.host,
|
|
200
|
+
port: config.smtp.port,
|
|
201
|
+
secure: config.smtp.secure
|
|
202
|
+
}
|
|
207
203
|
},
|
|
208
204
|
body: trigger.data
|
|
209
205
|
}
|
|
210
206
|
}
|
|
211
207
|
|
|
212
|
-
export function scheduleSubscription(config, data, trigger, needSetHistory, isTest = false) {
|
|
208
|
+
export async function scheduleSubscription(config, data, trigger, needSetHistory, isTest = false) {
|
|
213
209
|
try {
|
|
214
210
|
let functions = extractFunctionScript(data);
|
|
215
211
|
if (functions) {
|
|
@@ -218,7 +214,7 @@ export function scheduleSubscription(config, data, trigger, needSetHistory, isTe
|
|
|
218
214
|
}
|
|
219
215
|
|
|
220
216
|
if (needSetHistory) {
|
|
221
|
-
insertHistory(config, trigger._id, new Date());
|
|
217
|
+
await insertHistory(config, trigger._id, new Date());
|
|
222
218
|
console.log(`Run Recent Schedule : ${trigger.name}`);
|
|
223
219
|
} else {
|
|
224
220
|
console.log(`Run Schedule : ${trigger.name}`);
|
|
@@ -232,12 +228,12 @@ export function scheduleSubscription(config, data, trigger, needSetHistory, isTe
|
|
|
232
228
|
}
|
|
233
229
|
}
|
|
234
230
|
|
|
235
|
-
export function checkSchedule(selectedConfig, trigger, needSetHistory) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
231
|
+
export async function checkSchedule(selectedConfig, trigger, needSetHistory) {
|
|
232
|
+
try {
|
|
233
|
+
const data = await loadCliScript(selectedConfig, trigger);
|
|
234
|
+
await scheduleSubscription(selectedConfig, data, trigger, needSetHistory);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
console.error(error);
|
|
237
|
+
throw new Error(error);
|
|
238
|
+
}
|
|
243
239
|
}
|
package/scheduler/timer.js
CHANGED
|
@@ -3,7 +3,6 @@ import { getWatchers, getHistories } from "./watcherController.js";
|
|
|
3
3
|
import { isItTime, loopTimer } from "./watcherlib.js"
|
|
4
4
|
import { setInterval } from 'timers/promises';
|
|
5
5
|
import { getCompanyObjectId, getSelectedConfig } from "../scheduler/configController.js";
|
|
6
|
-
import { forkJoin } from 'rxjs';
|
|
7
6
|
import { historyRecordToJson, watcherRecordToJson, setUtcDate } from "./converter.js";
|
|
8
7
|
|
|
9
8
|
export const delay = ms => new Promise(res => setTimeout(res, ms));
|
|
@@ -22,7 +21,6 @@ export function getSelectedData(data, field, compareData) {
|
|
|
22
21
|
|
|
23
22
|
async function executeHistory(config, timer, rawLatestRun, currentTime) {
|
|
24
23
|
let latestRun = setUtcDate(rawLatestRun);
|
|
25
|
-
console.log(latestRun);
|
|
26
24
|
|
|
27
25
|
while ((latestRun < currentTime) &&
|
|
28
26
|
((currentTime - latestRun) >= SCHEDULE_INTERVAL)) {
|
|
@@ -53,26 +51,22 @@ export const runScheduler = async (companyName) => {
|
|
|
53
51
|
const company = await getCompanyObjectId(companyName);
|
|
54
52
|
const config = await getSelectedConfig(company._id);
|
|
55
53
|
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
Promise.all([getHistories(config), getWatchers(config)])
|
|
55
|
+
.then(([historyResult, watcherResult]) => {
|
|
58
56
|
const histories = historyRecordToJson(historyResult);
|
|
59
57
|
const watchers = watcherRecordToJson(watcherResult);
|
|
60
58
|
runHistory(histories, config, watchers, new Date());
|
|
61
|
-
}
|
|
62
|
-
});
|
|
59
|
+
});
|
|
63
60
|
|
|
64
61
|
for await (const startTime of setInterval(SCHEDULE_INTERVAL, Date.now())) {
|
|
65
62
|
// if ((now - startTime) > 1000)
|
|
66
63
|
// break;
|
|
67
64
|
|
|
68
|
-
getWatchers(config)
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
});
|
|
65
|
+
const result = await getWatchers(config);
|
|
66
|
+
const watchers = watcherRecordToJson(result);
|
|
67
|
+
for (const watcher of watchers) {
|
|
68
|
+
loopTimer(watcher.timer, Date.now(), config, true);
|
|
69
|
+
}
|
|
76
70
|
|
|
77
71
|
// // reload == true ?
|
|
78
72
|
}
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { from, tap } from 'rxjs';
|
|
2
|
-
import { concatMap, map, catchError } from 'rxjs/operators';
|
|
3
|
-
|
|
4
1
|
import { crudData, genId, queryData } from '../scheduler/datalib.js';
|
|
5
2
|
|
|
6
3
|
//LIST
|
|
@@ -15,7 +12,6 @@ export function getWatcherParameters(watcherFilter) {
|
|
|
15
12
|
{ data: "SYS$WATCHER.ID.WATCHER_ID.SYS$TIMER.NAME", key: 'name' },
|
|
16
13
|
{ data: "SYS$WATCHER.ID.WATCHER_ID.SYS$TIMER.ACTIVE", key: 'active' },
|
|
17
14
|
{ data: "SYS$WATCHER.ID.WATCHER_ID.SYS$TIMER.TIME_ZONE", key: 'timezone' },
|
|
18
|
-
// { data: "SYS$WATCHER.ID.WATCHER_ID.SYS$TIMER.SCRIPT_NAME", key: 'templateName' },
|
|
19
15
|
{ data: "SYS$WATCHER.ID.WATCHER_ID.SYS$TIMER.SEQ", key: 'seq' },
|
|
20
16
|
{ data: "SYS$WATCHER.ID.WATCHER_ID.SYS$TIMER.SCRIPT_ID", key: 'scriptid' },
|
|
21
17
|
{ data: "SYS$TIMER.ID.TIMER_ID.SYS$DAILY.DAYS", key: 'daily_days' },
|
|
@@ -66,25 +62,19 @@ export function getHistoryField(timerObjectId, historyId, currentDate) {
|
|
|
66
62
|
}
|
|
67
63
|
}
|
|
68
64
|
|
|
69
|
-
export function insertHistory(config, timerObjectId, currentDate) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
queryData(listData, config, true, 'list', 'list')
|
|
73
|
-
.pipe(
|
|
74
|
-
concatMap(historyResponse => {
|
|
75
|
-
const history = historyResponse[0];
|
|
65
|
+
export async function insertHistory(config, timerObjectId, currentDate) {
|
|
66
|
+
try {
|
|
67
|
+
const listData = getInsertHistoryParameters(timerObjectId);
|
|
76
68
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
})
|
|
87
|
-
).subscribe();
|
|
69
|
+
const historyResponse = await queryData(listData, config, true, 'list', 'list')
|
|
70
|
+
const history = historyResponse[0];
|
|
71
|
+
const historyId = history ? parseInt(history.history_id) : await genId(config, 'SYS$HISTORY_GEN');
|
|
72
|
+
const param = getHistoryField(timerObjectId, historyId, currentDate);
|
|
73
|
+
await crudData(param, config, 'put', 'orm')
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error(error);
|
|
76
|
+
return new Error(error);
|
|
77
|
+
}
|
|
88
78
|
}
|
|
89
79
|
|
|
90
80
|
export function getHistoryParameters() {
|