chore: email

pull/21/merge
xiaojunnuo 2023-06-07 23:36:42 +08:00
parent 4c324960e6
commit 64afebecd4
6 changed files with 163 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import { ConcurrencyStrategy, Pipeline, ResultType, Runnable, RunStrategy, Stage, Step, Task } from "../d.ts";
import { ConcurrencyStrategy, NotificationType, NotificationWhen, Pipeline, ResultType, Runnable, RunStrategy, Stage, Step, Task } from "../d.ts";
import _ from "lodash";
import { RunHistory, RunnableCollection } from "./run-history";
import { AbstractTaskPlugin, PluginDefine, pluginRegistry } from "../plugin";
@ -10,6 +10,7 @@ import { request } from "../utils/util.request";
import { IAccessService } from "../access";
import { RegistryItem } from "../registry";
import { Decorator } from "../decorator";
import { IEmailService } from "../service";
export type ExecutorOptions = {
userId: any;
@ -17,6 +18,7 @@ export type ExecutorOptions = {
storage: IStorage;
onChanged: (history: RunHistory) => Promise<void>;
accessService: IAccessService;
emailService: IEmailService;
};
export class Executor {
pipeline: Pipeline;
@ -51,10 +53,13 @@ export class Executor {
// 读取last
this.runtime = new RunHistory(runtimeId, trigger, this.pipeline);
this.logger.info(`pipeline.${this.pipeline.id} start`);
await this.notification("start");
await this.runWithHistory(this.pipeline, "pipeline", async () => {
await this.runStages(this.pipeline);
});
await this.notification("success");
} catch (e) {
await this.notification("error");
this.logger.error("pipeline 执行失败", e);
} finally {
await this.pipelineContext.setObj("lastRuntime", this.runtime);
@ -203,4 +208,37 @@ export class Executor {
this.runtime.context[stepOutputKey] = instance[key];
});
}
async notification(when: NotificationWhen, error?: any) {
if (!this.pipeline.notifications) {
return;
}
let subject = "";
let content = "";
if (when === "start") {
subject = `【CertD】${this.pipeline.title} 开始执行buildId:${this.runtime.id}`;
content = `【CertD】${this.pipeline.title} 开始执行buildId:${this.runtime.id}`;
} else if (when === "success") {
subject = `【CertD】${this.pipeline.title} 执行成功buildId:${this.runtime.id}`;
content = `【CertD】${this.pipeline.title} 执行成功buildId:${this.runtime.id}`;
} else if (when === "error") {
subject = `【CertD】${this.pipeline.title} 执行失败buildId:${this.runtime.id}`;
content = `<pre>${error.message}</pre>`;
} else {
return;
}
for (const notification of this.pipeline.notifications) {
if (!notification.when.includes(when)) {
continue;
}
if (notification.type === "email") {
this.options.emailService?.send({
subject,
content,
receivers: notification.options.receivers,
});
}
}
}
}

View File

@ -7,3 +7,4 @@ export * from "./plugin";
export * from "./utils";
export * from "./context";
export * from "./decorator";
export * from "./service";

View File

@ -0,0 +1,9 @@
export type EmailSend = {
subject: string;
content: string;
receivers: string[];
};
export interface IEmailService {
send(email: EmailSend): Promise<void>;
}

View File

@ -0,0 +1,53 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/decorator";
import { CrudController } from "../../../basic/crud-controller";
import { SettingsService } from "../service/settings-service";
/**
*/
@Provide()
@Controller('/api/sys/settings')
export class SettingsController extends CrudController<SettingsService> {
@Inject()
service: SettingsService;
getService() {
return this.service;
}
@Post('/page')
async page(@Body(ALL) body) {
body.query = body.query ?? {};
body.query.userId = this.ctx.user.id;
return super.page(body);
}
@Post('/list')
async list(@Body(ALL) body) {
body.userId = this.ctx.user.id;
return super.list(body);
}
@Post('/add')
async add(@Body(ALL) bean) {
bean.userId = this.ctx.user.id;
return super.add(bean);
}
@Post('/update')
async update(@Body(ALL) bean) {
await this.service.checkUserId(bean.id, this.ctx.user.id);
return super.update(bean);
}
@Post('/info')
async info(@Query('id') id) {
await this.service.checkUserId(id, this.ctx.user.id);
return super.info(id);
}
@Post('/delete')
async delete(@Query('id') id) {
await this.service.checkUserId(id, this.ctx.user.id);
return super.delete(id);
}
}

View File

@ -0,0 +1,28 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
/**
*/
@Entity('sys_settings')
export class SettingsEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ name: 'user_id', comment: '用户id' })
userId: number;
@Column({ comment: '名称', length: 100 })
name: string;
@Column({ name: 'setting', comment: '设置', length: 1024, nullable: true })
setting: string;
@Column({
name: 'create_time',
comment: '创建时间',
default: () => 'CURRENT_TIMESTAMP',
})
createTime: Date;
@Column({
name: 'update_time',
comment: '修改时间',
default: () => 'CURRENT_TIMESTAMP',
})
updateTime: Date;
}

View File

@ -0,0 +1,33 @@
import { Provide, Scope, ScopeEnum } from "@midwayjs/decorator";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import { BaseService } from "../../../basic/base-service";
import { SettingsEntity } from "../entity/settings";
/**
*
*/
@Provide()
@Scope(ScopeEnum.Singleton)
export class SettingsService
extends BaseService<SettingsEntity>
{
@InjectEntityModel(SettingsEntity)
repository: Repository<SettingsEntity>;
getRepository() {
return this.repository;
}
async getById(id: any): Promise<any> {
const entity = await this.info(id);
// const access = accessRegistry.get(entity.type);
const setting = JSON.parse(entity.setting);
return {
id: entity.id,
...setting,
};
}
}