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

View File

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

View File

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

View File

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

View File

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

View File

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