diff --git a/package.json b/package.json index e655c4e..e683dd3 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "node服务端", "main": "index.js", "scripts": { - "dev": "pm2 restart pm2.config.js --only dev --attach", + "dev": "nodemon index.js", "pro": "pm2 restart pm2.config.js --only oms-node", "deploy": "pm2 deploy pm2.config.js production", "stop": "pm2 delete all" diff --git a/src/rest/custom/getZip.js b/src/rest/custom/getZip.js index da6d559..37dd562 100644 --- a/src/rest/custom/getZip.js +++ b/src/rest/custom/getZip.js @@ -2,38 +2,69 @@ const db = require("../../utils/dbUitls"); const execute = require("../../tools/exec") const dayjs = require("dayjs") const fse = require("fs-extra"); +const axios = require('axios') module.exports = { action: "/node/custom/getZip", method: "post", execute: (request, response) => { let {id} = request.query, uniCon = `id='${id}'` - db.query(`select * from node_custom_config where id='${id}'`).then(res => { - let info = res?.[0], sql - if (info?.id) { - sql = `update node_custom_config set download=null,error=null,zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}' where ${uniCon}` - const buildPath = { - web: 'base-web', - wxwork: 'base-wxcp', - mp: 'dvcp_v2_wxmp' - }[info.type] || {} - let path = `../${buildPath}`, - {dist} = info - dist = dist || `../zips/${info.name}v${info.version || "1.0.0"}` - Promise.all([ - execute(`./shell/update.sh ${info.name} ${path}`), - db.query(sql) - ]).then(() => setTimeout(() => { - response.send({code: 0}) - }, 1000)) - execute(`cd ${path}&&npm run apps&&node bin/pages.js ${id}&&npm run build`) - .then(() => fse.emptyDir(dist)) - .then(() => execute(`./shell/move.sh ${info.type} ${path} ${dist}`)) - .then(() => { - return db.query(`update node_custom_config set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}',error='' where ${uniCon}`) - }).catch(err => { - console.log(err) - return fse.outputFile(`./logs/errors/${dayjs().format("YYYY-MM-DD")}.log`, err) - }) + db.query(`select * + from node_custom_config + where id = '${id}'`).then(res => { + let info = res?.[0] || {}, sql + if (info.id) { + sql = `update node_custom_config + set download=null, + error=null, + zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}' + where ${uniCon}` + if (info.type == 'web') { + const {name, version, dist = `${name}_v${version}`} = info + const buildConfig = { + web: {task: "devops-web", token: 'fLeOGSVIRs405Me'}, + }[info.type] + db.query(sql).then(() => axios.get(`https://jenkins.sinoecare.com/view/devops/job/${buildConfig.task}/buildWithParameters`,null,{ + params:{ + token: buildConfig.token, + pid: id, dist + } + })) + .then(() => db.query(`update node_custom_config + set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}', + error='' + where ${uniCon}`)) + .then(() => response.send({code: 0, msg: "打包任务已发送"})) + .catch(err => { + console.log(err) + return fse.outputFile(`./logs/errors/${dayjs().format("YYYY-MM-DD")}.log`, err) + }) + } else { + const buildPath = { + web: 'base-web', + wxwork: 'base-wxcp', + mp: 'dvcp_v2_wxmp' + }[info.type] || {} + let path = `../${buildPath}`, + {dist = `../zips/${info.name}v${info.version || "1.0.0"}`} = info + Promise.all([ + execute(`./shell/update.sh ${info.name} ${path}`), + db.query(sql) + ]).then(() => setTimeout(() => { + response.send({code: 0}) + }, 1000)) + execute(`cd ${path}&&npm run apps&&node bin/pages.js ${id}&&npm run build`) + .then(() => fse.emptyDir(dist)) + .then(() => execute(`./shell/move.sh ${info.type} ${path} ${dist}`)) + .then(() => { + return db.query(`update node_custom_config + set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}', + error='' + where ${uniCon}`) + }).catch(err => { + console.log(err) + return fse.outputFile(`./logs/errors/${dayjs().format("YYYY-MM-DD")}.log`, err) + }) + } } else return response.send({code: 1, err: "无法找到定制项目信息"}) }).catch(err => { console.log(err) diff --git a/src/websocket/custom/getZip.js b/src/websocket/custom/getZip.js index 13a0e7e..84a0024 100644 --- a/src/websocket/custom/getZip.js +++ b/src/websocket/custom/getZip.js @@ -2,7 +2,13 @@ const db = require("../../utils/dbUitls"); const execute = require("../../tools/exec") const dayjs = require("dayjs") const fse = require("fs-extra"); +const axios = require('axios') +const http = axios.create({ + baseURL: 'https://jenkins:114295d0b955e67a18b11917bd281ea9dc@jenkins.sinoecare.com', +}) + +const jobs = {} let controller = new AbortController() const handleZip = (id, ws) => { const uniCon = `id='${id}'` @@ -37,33 +43,82 @@ const handleZip = (id, ws) => { } } - db.detail({table: "node_custom_config", id}).then(info => { - if (info?.id) { + const startUpdateSql = () => db.query(`update node_custom_config + set download=null, + error=null, + zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}' + where ${uniCon}`) + const endUpdateSql = () => db.query(`update node_custom_config + set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}', + error='' + where ${uniCon}`) + const errorUpdateSql = msg => db.query(`update node_custom_config + set error='${msg}', + zipTime=null + where ${uniCon}`) + db.detail({table: "node_custom_config", id}).then((info = {}) => { + if (info.id) { const {signal} = controller; const progress = new counter(`正在处理 ${info.name} 的打包工作...`) - const buildPath = { - web: 'base-web', wxwork: 'base-wxcp', mp: 'dvcp_v2_wxmp' - }[info.type] || {} - let path = `../${buildPath}`, {dist} = info - dist = dist || `../zips/${info.name}v${info.version || "1.0.0"}` - Promise.all([db.query(`update node_custom_config set download=null,error=null,zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}' where ${uniCon}`), execute(`./shell/update.sh ${info.name} ${path}`, signal)]) - .then(cmd => progress.set(30, info?.id)) - .then(() => execute(`cd ${path}&&npm run apps&&node bin/pages.js ${id}&&npm run build`, signal)) - .then(cmd => progress.set(70, info?.id)) - .then(() => fse.emptyDir(dist)) - .then(() => execute(`./shell/move.sh ${info.type} ${path} ${dist}`, signal)) - .then(cmd => progress.set(90, info?.id)) - .then(() => db.query(`update node_custom_config set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}',error='' where ${uniCon}`)) - .then(() => db.detail({table: "node_custom_config", id})) - .then(row => progress.finish(row)) - .catch(err => { + if (info.type == 'web') { + const {name, version, dist = `${name}_v${version}`} = info + const buildConfig = { + web: {task: "devops-web", token: 'fLeOGSVIRs405Me'}, + }[info.type] + startUpdateSql() + .then(() => progress.set(30, id)) + .then(() => http.get(`/view/devops/job/${buildConfig.task}/buildWithParameters`, + {params: {token: buildConfig.token, pid: id, dist}})) + .then(() => http.get(`/view/devops/job/${buildConfig.task}/api/json`).then(res => res.data.lastBuild.number)) + .then(currentJob => new Promise((resolve, reject) => { + jobs[id] = {task: buildConfig.task, build: currentJob} + const timer = setInterval(() => { + http.get(`/view/devops/job/${buildConfig.task}/${currentJob}/api/json`).then(res => { + if (res.data.result == 'SUCCESS') { + clearInterval(timer) + resolve(endUpdateSql()) + } else if (res.data.res == 'ABORTED') { + clearInterval(timer) + reject("构建取消") + } else if (res.data.res == 'FAILURE') { + clearInterval(timer) + reject("构建失败") + } + }) + }, 1000) + })).catch(err => { console.log(err) - const msg = `执行失败:${err.cmd}` - return db.query(`update node_custom_config set error='${msg}',zipTime=null where ${uniCon}`) + const msg = `执行失败:${err.cmd || err}` + return errorUpdateSql(msg) .then(() => db.detail({table: "node_custom_config", id})) .then(row => progress.stop(row)) .catch(() => 0) }) + } else { + const buildPath = { + web: 'base-web', wxwork: 'base-wxcp', mp: 'dvcp_v2_wxmp' + }[info.type] || {} + let path = `../${buildPath}`, {dist} = info + dist = dist || `../zips/${info.name}v${info.version || "1.0.0"}` + Promise.all([startUpdateSql(), execute(`./shell/update.sh ${info.name} ${path}`, signal)]) + .then(cmd => progress.set(30, info?.id)) + .then(() => execute(`cd ${path}&&npm run apps&&node bin/pages.js ${id}&&npm run build`, signal)) + .then(cmd => progress.set(70, info?.id)) + .then(() => fse.emptyDir(dist)) + .then(() => execute(`./shell/move.sh ${info.type} ${path} ${dist}`, signal)) + .then(cmd => progress.set(90, info?.id)) + .then(() => endUpdateSql()) + .then(() => db.detail({table: "node_custom_config", id})) + .then(row => progress.finish(row)) + .catch(err => { + console.log(err) + const msg = `执行失败:${err.cmd}` + return errorUpdateSql(msg) + .then(() => db.detail({table: "node_custom_config", id})) + .then(row => progress.stop(row)) + .catch(() => 0) + }) + } } }).catch(err => { console.log(err) @@ -74,8 +129,13 @@ module.exports = { action: "/custom/getZip", execute: (ws, request) => { const {id, cid} = request if (cid) { - controller.abort() - controller = new AbortController() + if (jobs[cid]) { + const {task, build} = jobs[cid] + http.post(`/job/${task}/${build}/stop`) + } else { + controller.abort() + controller = new AbortController() + } } else if (id) { handleZip(id, ws) }