executor.js
4.9 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Executor = void 0;
const job_manager_1 = require("./job-manager");
const axios_1 = require("axios");
class Executor {
executorKey;
scheduleCenterUrl;
accessToken;
executorUrl;
jobHandlers;
jobManager;
constructor(executorKey, scheduleCenterUrl, accessToken, jobLogPath, jobHandlers) {
this.executorKey = executorKey;
this.scheduleCenterUrl = scheduleCenterUrl;
this.accessToken = accessToken;
this.jobHandlers = jobHandlers;
this.jobManager = new job_manager_1.JobManager(jobLogPath);
}
applyMiddleware({ app, domain }) {
this.executorUrl = domain;
const express = require('express');
var router = new express.Router();
router.use(async (req, res, next) => {
res.status(200);
const token = req.headers && req.headers['xxl-job-access-token'];
if (!!this.accessToken && this.accessToken !== token) {
res.send({ code: 500, msg: 'access token incorrect' });
return;
}
if (!req.body) {
res.send({ code: 500, msg: 'body is null' });
return;
}
next();
});
this.addJobRoutes(router);
app.use(router);
}
addJobRoutes(router) {
router.post(`/beat`, async (req, res, next) => {
res.send(this.beat());
});
router.post(`/idleBeat`, async (req, res, next) => {
res.send(this.idleBeat(req.body.jobId || -1));
});
router.post(`/run`, async (req, res, next) => {
res.send(this.run(req.body || {}));
});
router.post(`/kill`, async (req, res, next) => {
res.send(this.killJob(req.body.jobId || -1));
});
router.post(`/log`, async (req, res, next) => {
const { logDateTim, logId, fromLineNum } = req.body || {};
const data = await this.readLog(logDateTim, logId, fromLineNum);
res.send(data);
});
}
beat() {
return { code: 200, msg: 'success' };
}
idleBeat(jobId) {
return (this.jobManager.hasJob(jobId) ? { code: 500, msg: 'busy' } : { code: 200, msg: 'idle' });
}
run({ jobId, executorHandler: handlerName, executorParams: jobJsonParams, executorTimeout, logId, logDateTime }) {
const jobHandler = this.jobHandlers.get(handlerName);
if (!jobHandler) {
return { code: 500, msg: `no matched jobHandler(${handlerName})` };
}
this.jobManager.runJob(jobId, jobJsonParams, logId, logDateTime, executorTimeout, handlerName, jobHandler, this.callback.bind(this));
return { code: 200, msg: 'success' };
}
killJob(jobId) {
return { code: 500, msg: `not yet support, jobId(${jobId})` };
}
async readLog(logDateTime, logId, fromLineNum) {
return {
code: 200, content: {
"fromLineNum": 0,
"toLineNum": 100,
"logContent": "test",
"isEnd": true
}
};
}
async registry() {
return axios_1.default.post(`${this.scheduleCenterUrl}/api/registry`, {
registryGroup: 'EXECUTOR',
registryKey: this.executorKey,
registryValue: this.executorUrl
}, {
headers: {
'XXL-JOB-ACCESS-TOKEN': this.accessToken,
'Content-Type': 'application/json'
}
}).then((response) => {
}).catch(error => console.error('执行器注册失败:', error.message));
}
async registryRemove() {
return axios_1.default.post(`${this.scheduleCenterUrl}/api/registryRemove`, {
'registryGroup': 'EXECUTOR',
'registryKey': this.executorKey,
'registryValue': this.executorUrl
}, {
headers: {
'XXL-JOB-ACCESS-TOKEN': this.accessToken,
'Content-Type': 'application/json'
}
}).then((response) => console.log('执行器移除成功:', response.data)).catch(error => console.error('执行器移除失败:', error.message));
}
async callback({ error, logId, result, use_time }) {
var handleMsg = JSON.stringify({ use_time, result, error: error && error.message });
return axios_1.default.post(`${this.scheduleCenterUrl}/api/callback`, [{
logId,
logDateTim: Date.now(),
handleCode: error ? 500 : 200,
handleMsg
}], {
headers: {
'XXL-JOB-ACCESS-TOKEN': this.accessToken,
'Content-Type': 'application/json'
}
}).then((response) => response.data).catch((err) => {
console.error(`callback error:${JSON.stringify(result)} ${err.message}`);
});
}
}
exports.Executor = Executor;
//# sourceMappingURL=executor.js.map