Commit 6471b6b5 Harvey

no message

1 个父辈 ee207e6a
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const express = require("express"); const express = require("express");
const config = require('../config/config.js'); const config = require('../config/config.js');
console.log(config);
const app = express(); const app = express();
app.use(require('body-parser').json()); app.use(require('body-parser').json());
new (require("./xxl_job/index").XxlJobExecutor)(config.xxl_job, require('./job').job_handlers).applyMiddleware({ app, domain: config.app_domain, path: '' }); new (require("./xxl_job/index").XxlJobExecutor)(config.xxl_job, require('./job').job_handlers).applyMiddleware({ app, domain: config.app_domain });
app.listen(config.port, () => { app.listen(config.port, () => {
console.log(`job-ydn-zq app listening on port ${config.port}`); console.log(`job-ydn-zq app listening on port ${config.port}`);
}); });
......
...@@ -30,24 +30,14 @@ class Executor { ...@@ -30,24 +30,14 @@ class Executor {
/** /**
* 应用执行器中间件 * 应用执行器中间件
* @param {*} app * @param {*} app
* @param {string} app_domain * @param {string} domain
* @param {string} uri
*/ */
applyMiddleware({ app, app_domain, uri }) { applyMiddleware({ app, domain }) {
this.executorUrl = domain;
const express = require('express'); const express = require('express');
var router = new express.Router(); var router = new express.Router();
this.initRouter(router, uri); //请求认证
this.addJobRoutes(router, uri); router.use(async (req, res, next) => {
app.use(router);
this.executorUrl = (app_domain || 'http://192.168.31.251:8088') + uri;
}
/**
* 添加xxl-job相关的路由,供调度中心访问
* @param {express.Router} router
* @param {string} baseUri
*/
initRouter(router, uri) {
router.use(uri, async (req, res, next) => {
res.status(200); res.status(200);
const token = req.headers && req.headers['xxl-job-access-token']; const token = req.headers && req.headers['xxl-job-access-token'];
if (!!this.accessToken && this.accessToken !== token) { if (!!this.accessToken && this.accessToken !== token) {
...@@ -60,26 +50,28 @@ class Executor { ...@@ -60,26 +50,28 @@ class Executor {
} }
next(); next();
}); });
this.addJobRoutes(router);
app.use(router);
} }
/** /**
* 添加xxl-job相关的路由,供调度中心访问 * 添加xxl-job相关的路由,供调度中心访问
* @param {express.Router} router * @param {express.Router} router
* @param {string} baseUri * @param {string} baseUri
*/ */
addJobRoutes(router, baseUri) { addJobRoutes(router) {
router.post(`${baseUri}/beat`, async (req, res, next) => { router.post(`/beat`, async (req, res, next) => {
res.send(this.beat()); res.send(this.beat());
}); });
router.post(`${baseUri}/idleBeat`, async (req, res, next) => { router.post(`/idleBeat`, async (req, res, next) => {
res.send(this.idleBeat(req.body.jobId || -1)); res.send(this.idleBeat(req.body.jobId || -1));
}); });
router.post(`${baseUri}/run`, async (req, res, next) => { router.post(`/run`, async (req, res, next) => {
res.send(this.run(req.body || {})); res.send(this.run(req.body || {}));
}); });
router.post(`${baseUri}/kill`, async (req, res, next) => { router.post(`/kill`, async (req, res, next) => {
res.send(this.killJob(req.body.jobId || -1)); res.send(this.killJob(req.body.jobId || -1));
}); });
router.post(`${baseUri}/log`, async (req, res, next) => { router.post(`/log`, async (req, res, next) => {
const { logDateTim, logId, fromLineNum } = req.body || {}; const { logDateTim, logId, fromLineNum } = req.body || {};
const data = await this.readLog(logDateTim, logId, fromLineNum); const data = await this.readLog(logDateTim, logId, fromLineNum);
res.send(data); res.send(data);
......
...@@ -22,10 +22,9 @@ class XxlJobExecutor { ...@@ -22,10 +22,9 @@ class XxlJobExecutor {
* @param {Object} args * @param {Object} args
* @param {any} args.app 执行器server, express * @param {any} args.app 执行器server, express
* @param {string} args.appDomain 执行器 server 地址 * @param {string} args.appDomain 执行器 server 地址
* @param {string} args.path 执行器挂载的 uri 路径
*/ */
applyMiddleware({ app, domain, path }) { applyMiddleware({ app, domain }) {
this.executor.applyMiddleware({ app, domain, path }); this.executor.applyMiddleware({ app, domain });
const registry = this.executor.registry.bind(this.executor); const registry = this.executor.registry.bind(this.executor);
registry() && (this.registryInterval = setInterval(registry, 30000)); registry() && (this.registryInterval = setInterval(registry, 30000));
} }
......
...@@ -8,7 +8,7 @@ try { ...@@ -8,7 +8,7 @@ try {
// 执行器AppName,在调度中心配置执行器时使用 // 执行器AppName,在调度中心配置执行器时使用
XXL_JOB_EXECUTOR_KEY: "executor-job-ydn-zq", XXL_JOB_EXECUTOR_KEY: "executor-job-ydn-zq",
// 调度中心地址 // 调度中心地址
XXL_JOB_SCHEDULE_CENTER_URL: "http://xxljob.ydniu.com/xxl-job-admin", XXL_JOB_SCHEDULE_CENTER_URL: "http://192.168.2.168:8080/xxl-job-admin",
// 调度中心设置的请求令牌,调度中心和执行器都会进行校验,双方AccessToken匹配才允许通讯 // 调度中心设置的请求令牌,调度中心和执行器都会进行校验,双方AccessToken匹配才允许通讯
XXL_JOB_ACCESS_TOKEN: "default_token", XXL_JOB_ACCESS_TOKEN: "default_token",
// 任务执行日志的存储路径 // 任务执行日志的存储路径
...@@ -20,7 +20,7 @@ try { ...@@ -20,7 +20,7 @@ try {
port: 8088, port: 8088,
// 前端地址 // 前端地址
app_domain: "http://[2408:8352:602:1100:1c8a:80f5:da47:66ef]:8088", app_domain: "http://[2408:8352:602:1100:1c8a:80f5:da47:66ef]:8088/",
}; };
} }
......
import * as express from 'express' import * as express from 'express'
const config = require('../config/config.js') const config = require('../config/config.js')
console.log(config)
const app = express() const app = express()
app.use(require('body-parser').json()) app.use(require('body-parser').json())
new (require("./xxl_job/index").XxlJobExecutor)( new (require("./xxl_job/index").XxlJobExecutor)(
config.xxl_job, config.xxl_job,
require('./job').job_handlers require('./job').job_handlers
).applyMiddleware({ app, domain: config.app_domain, path: '' }) ).applyMiddleware({ app, domain: config.app_domain })
app.listen(config.port, () => { app.listen(config.port, () => {
console.log(`job-ydn-zq app listening on port ${config.port}`) console.log(`job-ydn-zq app listening on port ${config.port}`)
......
...@@ -32,29 +32,15 @@ export class Executor { ...@@ -32,29 +32,15 @@ export class Executor {
/** /**
* 应用执行器中间件 * 应用执行器中间件
* @param {*} app * @param {*} app
* @param {string} app_domain * @param {string} domain
* @param {string} uri
*/ */
applyMiddleware({ app, app_domain, uri }) { applyMiddleware({ app, domain }) {
this.executorUrl = domain
const express = require('express') const express = require('express')
var router = new express.Router() var router = new express.Router()
this.initRouter(router, uri)
this.addJobRoutes(router, uri)
app.use(router) //请求认证
router.use(async (req, res, next) => {
this.executorUrl = (app_domain || 'http://192.168.31.251:8088') + uri
}
/**
* 添加xxl-job相关的路由,供调度中心访问
* @param {express.Router} router
* @param {string} baseUri
*/
initRouter(router: any, uri: string) {
router.use(uri, async (req, res, next) => {
res.status(200) res.status(200)
const token = req.headers && req.headers['xxl-job-access-token'] const token = req.headers && req.headers['xxl-job-access-token']
...@@ -72,6 +58,12 @@ export class Executor { ...@@ -72,6 +58,12 @@ export class Executor {
next() next()
}) })
this.addJobRoutes(router)
app.use(router)
} }
/** /**
...@@ -79,24 +71,24 @@ export class Executor { ...@@ -79,24 +71,24 @@ export class Executor {
* @param {express.Router} router * @param {express.Router} router
* @param {string} baseUri * @param {string} baseUri
*/ */
addJobRoutes(router, baseUri) { addJobRoutes(router) {
router.post(`${baseUri}/beat`, async (req, res, next) => { router.post(`/beat`, async (req, res, next) => {
res.send(this.beat()) res.send(this.beat())
}) })
router.post(`${baseUri}/idleBeat`, async (req, res, next) => { router.post(`/idleBeat`, async (req, res, next) => {
res.send(this.idleBeat(req.body.jobId || -1)) res.send(this.idleBeat(req.body.jobId || -1))
}) })
router.post(`${baseUri}/run`, async (req, res, next) => { router.post(`/run`, async (req, res, next) => {
res.send(this.run(req.body || {})) res.send(this.run(req.body || {}))
}) })
router.post(`${baseUri}/kill`, async (req, res, next) => { router.post(`/kill`, async (req, res, next) => {
res.send(this.killJob(req.body.jobId || -1)) res.send(this.killJob(req.body.jobId || -1))
}) })
router.post(`${baseUri}/log`, async (req, res, next) => { router.post(`/log`, async (req, res, next) => {
const { logDateTim, logId, fromLineNum } = req.body || {} const { logDateTim, logId, fromLineNum } = req.body || {}
const data = await this.readLog(logDateTim, logId, fromLineNum) const data = await this.readLog(logDateTim, logId, fromLineNum)
res.send(data) res.send(data)
......
...@@ -29,10 +29,9 @@ export class XxlJobExecutor { ...@@ -29,10 +29,9 @@ export class XxlJobExecutor {
* @param {Object} args * @param {Object} args
* @param {any} args.app 执行器server, express * @param {any} args.app 执行器server, express
* @param {string} args.appDomain 执行器 server 地址 * @param {string} args.appDomain 执行器 server 地址
* @param {string} args.path 执行器挂载的 uri 路径
*/ */
public applyMiddleware({ app, domain, path }) { public applyMiddleware({ app, domain }) {
this.executor.applyMiddleware({ app, domain, path }) this.executor.applyMiddleware({ app, domain })
const registry = this.executor.registry.bind(this.executor) const registry = this.executor.registry.bind(this.executor)
registry() && (this.registryInterval = setInterval(registry, 30000)) registry() && (this.registryInterval = setInterval(registry, 30000))
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!