mirror of
https://github.com/certd/certd.git
synced 2026-08-11 01:35:49 +08:00
refactor(certd-server): 优化流水线触发逻辑并新增配置
1. 新增流水线执行异常错误码和提示信息 2. 调整服务端口配置为7004/7005 3. 优化流水线状态查询逻辑,增加更新时间判断 4. 新增失败流水线重试间隔限制逻辑 5. 添加对应单元测试
This commit is contained in:
@@ -117,6 +117,10 @@ export const Constants = {
|
|||||||
code: 20014,
|
code: 20014,
|
||||||
message: "域名校验方式未配置",
|
message: "域名校验方式未配置",
|
||||||
},
|
},
|
||||||
|
openPipelineError: {
|
||||||
|
code: 20015,
|
||||||
|
message: "流水线执行异常,请稍后重试",
|
||||||
|
},
|
||||||
openEmailNotFound: {
|
openEmailNotFound: {
|
||||||
code: 20021,
|
code: 20021,
|
||||||
message: "用户邮箱还未配置",
|
message: "用户邮箱还未配置",
|
||||||
|
|||||||
@@ -93,13 +93,13 @@ export default (req: any) => {
|
|||||||
// with options
|
// with options
|
||||||
"/api": {
|
"/api": {
|
||||||
//配套后端 https://github.com/fast-crud/fs-server-js
|
//配套后端 https://github.com/fast-crud/fs-server-js
|
||||||
target: "http://127.0.0.1:7001",
|
target: "http://127.0.0.1:7004",
|
||||||
//忽略证书
|
//忽略证书
|
||||||
// agent: new https.Agent({ rejectUnauthorized: false }),
|
// agent: new https.Agent({ rejectUnauthorized: false }),
|
||||||
},
|
},
|
||||||
"/certd/api": {
|
"/certd/api": {
|
||||||
//配套后端 https://github.com/fast-crud/fs-server-js
|
//配套后端 https://github.com/fast-crud/fs-server-js
|
||||||
target: "http://127.0.0.1:7001/api",
|
target: "http://127.0.0.1:7004/api",
|
||||||
rewrite: (path: any) => path.replace(/^\/certd\/api/, ""),
|
rewrite: (path: any) => path.replace(/^\/certd\/api/, ""),
|
||||||
//忽略证书
|
//忽略证书
|
||||||
// agent: new https.Agent({ rejectUnauthorized: false }),
|
// agent: new https.Agent({ rejectUnauthorized: false }),
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
LEGO_VERSION=4.30.1
|
LEGO_VERSION=4.30.1
|
||||||
JKS_GO_VERSION=1.0.3
|
JKS_GO_VERSION=1.0.3
|
||||||
certd_plugin_loadmode=dev
|
certd_plugin_loadmode=dev
|
||||||
|
|
||||||
|
certd_koa_port=7004
|
||||||
|
certd_https_port=7005
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { CertInfoFacade } from "./cert-info-facade.js";
|
||||||
|
|
||||||
|
describe("CertInfoFacade.triggerApplyPipeline", () => {
|
||||||
|
it("returns a pipeline error when the pipeline failed within the last three hours", async () => {
|
||||||
|
const facade = new CertInfoFacade();
|
||||||
|
let triggered = false;
|
||||||
|
facade.pipelineService = {
|
||||||
|
async getStatus() {
|
||||||
|
return {
|
||||||
|
status: "error",
|
||||||
|
updateTime: new Date(Date.now() - 2 * 60 * 60 * 1000),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
async trigger() {
|
||||||
|
triggered = true;
|
||||||
|
},
|
||||||
|
} as any;
|
||||||
|
facade.certInfoService = {} as any;
|
||||||
|
|
||||||
|
await assert.rejects(
|
||||||
|
() => facade.triggerApplyPipeline({ pipelineId: 1 }),
|
||||||
|
(error: any) => {
|
||||||
|
assert.equal(error.code, 20015);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert.equal(triggered, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("retries a pipeline that failed more than three hours ago", async function () {
|
||||||
|
this.timeout(5000);
|
||||||
|
const facade = new CertInfoFacade();
|
||||||
|
let triggered = false;
|
||||||
|
facade.pipelineService = {
|
||||||
|
async getStatus() {
|
||||||
|
return {
|
||||||
|
status: "error",
|
||||||
|
updateTime: new Date(Date.now() - 3 * 60 * 60 * 1000 - 1),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
async trigger() {
|
||||||
|
triggered = true;
|
||||||
|
},
|
||||||
|
} as any;
|
||||||
|
facade.certInfoService = {
|
||||||
|
async getByPipelineId() {
|
||||||
|
return { id: 2 };
|
||||||
|
},
|
||||||
|
} as any;
|
||||||
|
|
||||||
|
await assert.rejects(
|
||||||
|
() => facade.triggerApplyPipeline({ pipelineId: 1 }),
|
||||||
|
(error: any) => {
|
||||||
|
assert.equal(error.code, 20013);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert.equal(triggered, true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -153,7 +153,16 @@ export class CertInfoFacade {
|
|||||||
|
|
||||||
async triggerApplyPipeline(req: { pipelineId: number }) {
|
async triggerApplyPipeline(req: { pipelineId: number }) {
|
||||||
//查询流水线状态
|
//查询流水线状态
|
||||||
const status = await this.pipelineService.getStatus(req.pipelineId);
|
const pipelineStatus = await this.pipelineService.getStatus(req.pipelineId);
|
||||||
|
const status = pipelineStatus?.status;
|
||||||
|
if (status === "error" && !this.canRetryErrorPipeline(pipelineStatus?.updateTime)) {
|
||||||
|
throw new CodeException({
|
||||||
|
...Constants.res.openPipelineError,
|
||||||
|
data: {
|
||||||
|
pipelineId: req.pipelineId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
if (status != "running" && status != "start") {
|
if (status != "running" && status != "start") {
|
||||||
await this.pipelineService.trigger(req.pipelineId);
|
await this.pipelineService.trigger(req.pipelineId);
|
||||||
await utils.sleep(2000);
|
await utils.sleep(2000);
|
||||||
@@ -167,4 +176,12 @@ export class CertInfoFacade {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private canRetryErrorPipeline(updateTime?: Date) {
|
||||||
|
if (!updateTime) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const retryInterval = 3 * 60 * 60 * 1000;
|
||||||
|
return Date.now() - updateTime.getTime() >= retryInterval;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1390,15 +1390,15 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getStatus(pipelineId: number) {
|
async getStatus(pipelineId: number) {
|
||||||
const res = await this.repository.findOne({
|
return await this.repository.findOne({
|
||||||
select: {
|
select: {
|
||||||
status: true,
|
status: true,
|
||||||
|
updateTime: true,
|
||||||
},
|
},
|
||||||
where: {
|
where: {
|
||||||
id: pipelineId,
|
id: pipelineId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return res?.status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPipelineUserId(pipelineId: number) {
|
async getPipelineUserId(pipelineId: number) {
|
||||||
|
|||||||
Reference in New Issue
Block a user