refactor: huawei

pull/21/merge
xiaojunnuo 2023-05-09 14:11:01 +08:00
parent f489c59ca3
commit 8c152371a1
6 changed files with 29 additions and 20 deletions

View File

@ -1,8 +1,10 @@
import { IStorage, MemoryStorage } from "./storage";
export interface IContext {
get(key: string): Promise<any>;
set(key: string, value: any): Promise<void>;
get(key: string): Promise<string | null>;
set(key: string, value: string): Promise<void>;
getObj(key: string): Promise<any>;
setObj(key: string, value: any): Promise<void>;
}
export class ContextFactory {
@ -32,8 +34,15 @@ export class StorageContext implements IContext {
this.scope = scope;
this.namespace = namespace;
}
async get(key: string): Promise<any> {
const str = await this.storage.get(this.scope, this.namespace, key);
async get(key: string) {
return await this.storage.get(this.scope, this.namespace, key);
}
async set(key: string, value: string) {
return await this.storage.set(this.scope, this.namespace, key, value);
}
async getObj(key: string): Promise<any> {
const str = await this.get(key);
if (str) {
const store = JSON.parse(str);
return store.value;
@ -41,7 +50,7 @@ export class StorageContext implements IContext {
return null;
}
async set(key: string, value: any) {
await this.storage.set(this.scope, this.namespace, key, JSON.stringify({ value }));
async setObj(key: string, value: any) {
await this.set(key, JSON.stringify({ value }));
}
}

View File

@ -53,8 +53,8 @@ export class Executor {
if (runnable.strategy?.runStrategy === RunStrategy.SkipWhenSucceed) {
//如果是成功后跳过策略
const lastResult = await this.pipelineContext.get(contextKey);
const lastInput = await this.pipelineContext.get(inputKey);
const lastResult = await this.pipelineContext.getObj(contextKey);
const lastInput = await this.pipelineContext.getObj(inputKey);
let inputChanged = false;
//TODO 参数不变
if (runnableType === "step") {
@ -73,12 +73,12 @@ export class Executor {
try {
await run();
this.runtime.success(runnable);
await this.pipelineContext.set(contextKey, ResultType.success);
await this.pipelineContext.setObj(contextKey, ResultType.success);
await this.onChanged(this.runtime);
return ResultType.success;
} catch (e: any) {
this.runtime.error(runnable, e);
await this.pipelineContext.set(contextKey, ResultType.error);
await this.pipelineContext.setObj(contextKey, ResultType.error);
await this.onChanged(this.runtime);
throw e;
} finally {

View File

@ -7,7 +7,7 @@ function generateId() {
}
export const pipeline: Pipeline = {
version: 1,
id: "3",
id: "huawei.test",
title: "华为管道测试",
userId: 1,
triggers: [],

View File

@ -7,7 +7,7 @@ function generateId() {
}
export const pipeline: Pipeline = {
version: 1,
id: generateId(),
id: "aliyun.test",
title: "测试管道",
userId: 1,
triggers: [],

View File

@ -17,8 +17,8 @@ export class AcmeService {
});
}
async getAccountConfig(email: string) {
return (await this.userContext.get(this.buildAccountKey(email))) || {};
async getAccountConfig(email: string): Promise<any> {
return (await this.userContext.getObj(this.buildAccountKey(email))) || {};
}
buildAccountKey(email: string) {
@ -26,7 +26,7 @@ export class AcmeService {
}
async saveAccountConfig(email: string, conf: any) {
await this.userContext.set(this.buildAccountKey(email), conf);
await this.userContext.setObj(this.buildAccountKey(email), conf);
}
async getAcmeClient(email: string, isTest = false): Promise<acme.Client> {

View File

@ -183,9 +183,9 @@ export class CertApplyPlugin implements ITaskPlugin {
}
let inputChanged = false;
const inputCacheKey = "input.cert";
const oldInputStr = await this.pipelineContext.get(inputCacheKey);
await this.pipelineContext.set(inputCacheKey, this.cert);
const inputCacheKey = "input.domains";
const oldInputStr = await this.pipelineContext.getObj(inputCacheKey);
await this.pipelineContext.setObj(inputCacheKey, this.domains);
const oldInput = JSON.stringify(oldInputStr);
const thisInput = JSON.stringify(this.cert);
if (oldInput !== thisInput) {
@ -276,14 +276,14 @@ export class CertApplyPlugin implements ITaskPlugin {
key: this.formatCert(cert.key),
csr: this.formatCert(cert.csr),
};
await this.pipelineContext.set("cert", newCert);
await this.pipelineContext.setObj("cert", newCert);
await this.pipelineContext.set("cert.crt", newCert.crt);
await this.pipelineContext.set("cert.key", newCert.key);
await this.pipelineContext.set("cert.csr", newCert.csr);
}
async readCurrentCert() {
const cert: any = await this.pipelineContext.get("cert");
const cert: any = await this.pipelineContext.getObj("cert");
if (cert == null) {
return undefined;
}