mirror of
https://github.com/certd/certd.git
synced 2026-04-23 19:57:27 +08:00
perf: 域名导入任务进度条
This commit is contained in:
@@ -3,16 +3,17 @@ import { logger } from "@certd/basic"
|
||||
export class BackTaskExecutor {
|
||||
tasks: Record<string, Record<string, BackTask>> = {}
|
||||
|
||||
start(type: string, task: BackTask) {
|
||||
start(task: BackTask) {
|
||||
const type = task.type || 'default'
|
||||
if (!this.tasks[type]) {
|
||||
this.tasks[type] = {}
|
||||
}
|
||||
const oldTask = this.tasks[type][task.key]
|
||||
if (oldTask && oldTask.status === "running") {
|
||||
throw new Error(`任务 ${type}—${task.key} 正在运行中`)
|
||||
throw new Error(`任务 ${task.title} 正在运行中`)
|
||||
}
|
||||
this.tasks[type][task.key] = task
|
||||
this.run(type, task);
|
||||
this.run(task);
|
||||
}
|
||||
|
||||
get(type: string, key: string) {
|
||||
@@ -37,7 +38,8 @@ export class BackTaskExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private async run(type: string, task: any) {
|
||||
private async run(task: BackTask) {
|
||||
const type = task.type || 'default'
|
||||
if (task.status === "running") {
|
||||
throw new Error(`任务 ${type}—${task.key} 正在运行中`)
|
||||
}
|
||||
@@ -49,7 +51,7 @@ export class BackTaskExecutor {
|
||||
} catch (e) {
|
||||
logger.error(`任务 ${task.title}[${type}-${task.key}] 执行失败`, e.message);
|
||||
task.status = "failed";
|
||||
task.error = e.message;
|
||||
task.addError(e.message)
|
||||
} finally {
|
||||
task.endTime = Date.now();
|
||||
task.status = "done";
|
||||
@@ -61,9 +63,10 @@ export class BackTaskExecutor {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
export class BackTask {
|
||||
type: string;
|
||||
key: string;
|
||||
title: string;
|
||||
total: number = 0;
|
||||
@@ -79,10 +82,12 @@ export class BackTask {
|
||||
|
||||
run: (task: BackTask) => Promise<void>;
|
||||
|
||||
constructor(opts:{
|
||||
constructor(opts: {
|
||||
type: string,
|
||||
key: string, title: string, run: (task: BackTask) => Promise<void>
|
||||
}) {
|
||||
const {key, title, run} = opts
|
||||
const { key, title, run, type } = opts
|
||||
this.type = type
|
||||
this.key = key;
|
||||
this.title = title;
|
||||
Object.defineProperty(this, 'run', {
|
||||
|
||||
@@ -14,6 +14,8 @@ import { TaskServiceBuilder } from '../../pipeline/service/getter/task-service-g
|
||||
import { SubDomainService } from "../../pipeline/service/sub-domain-service.js";
|
||||
import { DomainEntity } from '../entity/domain.js';
|
||||
import { BackTask, taskExecutor } from '../../basic/service/task-executor.js';
|
||||
import { UserSettingsService } from '../../mine/service/user-settings-service.js';
|
||||
import { UserDomainImportSetting } from '../../mine/service/models.js';
|
||||
|
||||
export interface SyncFromProviderReq {
|
||||
userId: number;
|
||||
@@ -22,6 +24,9 @@ export interface SyncFromProviderReq {
|
||||
}
|
||||
|
||||
|
||||
const DOMAIN_IMPORT_TASK_TYPE = 'domainImportTask'
|
||||
const DOMAIN_EXPIRE_TASK_TYPE = 'domainExpirationSyncTask'
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -43,6 +48,9 @@ export class DomainService extends BaseService<DomainEntity> {
|
||||
@Inject()
|
||||
taskServiceBuilder: TaskServiceBuilder;
|
||||
|
||||
@Inject()
|
||||
userSettingService: UserSettingsService;
|
||||
|
||||
//@ts-ignore
|
||||
getRepository() {
|
||||
return this.repository;
|
||||
@@ -207,13 +215,26 @@ export class DomainService extends BaseService<DomainEntity> {
|
||||
}
|
||||
|
||||
|
||||
async doSyncFromProvider(req: SyncFromProviderReq) {
|
||||
const key = `user_${req.userId || 0}`
|
||||
taskExecutor.start('syncFromProviderTask', new BackTask({
|
||||
async startDomainImportTask(req: {userId:number,key:string}) {
|
||||
const key = req.key
|
||||
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(req.userId, UserDomainImportSetting)
|
||||
|
||||
const item = setting.domainImportList.find(item => item.key === key)
|
||||
if (!item) {
|
||||
throw new Error(`域名导入任务配置(${key})还未注册`)
|
||||
}
|
||||
const { dnsProviderType, dnsProviderAccessId,title } = item
|
||||
|
||||
taskExecutor.start(new BackTask({
|
||||
type: DOMAIN_IMPORT_TASK_TYPE,
|
||||
key,
|
||||
title: `从域名提供商导入域名(${key})`,
|
||||
title: title,
|
||||
run: async (task: BackTask) => {
|
||||
await this._syncFromProvider(req, task)
|
||||
await this._syncFromProvider({
|
||||
userId: req.userId,
|
||||
dnsProviderType,
|
||||
dnsProviderAccessId,
|
||||
}, task)
|
||||
},
|
||||
}))
|
||||
}
|
||||
@@ -288,10 +309,85 @@ export class DomainService extends BaseService<DomainEntity> {
|
||||
logger.info(`从域名提供商${dnsProviderType}导入域名完成(${key}),共导入${task.current}个域名,跳过${task.getSkipCount()}个域名`)
|
||||
}
|
||||
|
||||
async doSyncDomainsExpirationDate(req: { userId?: number }) {
|
||||
async getDomainImportTaskStatus(req:{userId?:number}) {
|
||||
const userId = req.userId || 0
|
||||
|
||||
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, UserDomainImportSetting)
|
||||
const list= setting?.domainImportList || []
|
||||
|
||||
const taskList:any = []
|
||||
|
||||
for (const item of list) {
|
||||
const { key } = item
|
||||
|
||||
const task = taskExecutor.get(DOMAIN_IMPORT_TASK_TYPE,key)
|
||||
|
||||
taskList.push({
|
||||
...item,
|
||||
task,
|
||||
})
|
||||
}
|
||||
return taskList
|
||||
}
|
||||
|
||||
async addDomainImportTask(req:{userId?:number,dnsProviderType:string,dnsProviderAccessId:string,title:string}) {
|
||||
const userId = req.userId || 0
|
||||
const { dnsProviderType, dnsProviderAccessId,title } = req
|
||||
const key = `user_${userId}_${dnsProviderType}_${dnsProviderAccessId}`
|
||||
|
||||
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, UserDomainImportSetting)
|
||||
setting.domainImportList = setting.domainImportList || []
|
||||
if (setting.domainImportList.find(item => item.key === key)) {
|
||||
throw new Error(`该域名导入任务${key}已存在`)
|
||||
}
|
||||
|
||||
const access = await this.accessService.getAccessById(dnsProviderAccessId, true, userId)
|
||||
if (!access) {
|
||||
throw new Error(`该授权(${dnsProviderAccessId})不存在,请检查是否已被删除`)
|
||||
}
|
||||
|
||||
const item = {
|
||||
dnsProviderType,
|
||||
dnsProviderAccessId,
|
||||
key,
|
||||
title,
|
||||
}
|
||||
setting.domainImportList.push(item)
|
||||
await this.userSettingService.saveSetting(userId, setting)
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
async deleteDomainImportTask(req:{userId?:number,key:string}) {
|
||||
const userId = req.userId || 0
|
||||
const { key } = req
|
||||
|
||||
const setting = await this.userSettingService.getSetting<UserDomainImportSetting>(userId, UserDomainImportSetting)
|
||||
setting.domainImportList = setting.domainImportList || []
|
||||
const index = setting.domainImportList.findIndex(item => item.key === key)
|
||||
if (index === -1) {
|
||||
throw new Error(`该域名导入任务${key}不存在`)
|
||||
}
|
||||
setting.domainImportList.splice(index, 1)
|
||||
taskExecutor.clear(DOMAIN_IMPORT_TASK_TYPE,key)
|
||||
await this.userSettingService.saveSetting(userId, setting)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
async getSyncExpirationTaskStatus(req:{userId?:number}) {
|
||||
const userId = req.userId || 0
|
||||
const key = `user_${userId}`
|
||||
const task = taskExecutor.get(DOMAIN_EXPIRE_TASK_TYPE,key)
|
||||
return task
|
||||
}
|
||||
|
||||
async startSyncExpirationTask(req: { userId?: number }) {
|
||||
const userId = req.userId
|
||||
const key = `user_${userId || 0}`
|
||||
taskExecutor.start('syncDomainsExpirationDateTask', new BackTask({
|
||||
taskExecutor.start(new BackTask({
|
||||
type: DOMAIN_EXPIRE_TASK_TYPE,
|
||||
key,
|
||||
title: `同步注册域名过期时间(${key}))`,
|
||||
run: async (task: BackTask) => {
|
||||
@@ -407,4 +503,6 @@ export class DomainService extends BaseService<DomainEntity> {
|
||||
const key = `user_${req.userId || 'all'}`
|
||||
logger.info(`同步用户(${key})注册域名过期时间完成(${req.task.getSuccessCount()}个成功,${req.task.errors.length}个失败)` )
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -46,3 +46,11 @@ export class UserGrantSetting extends BaseSettings {
|
||||
|
||||
allowAdminViewCerts:boolean = false;
|
||||
}
|
||||
|
||||
|
||||
export class UserDomainImportSetting extends BaseSettings {
|
||||
static __title__ = "用户域名导入设置";
|
||||
static __key__ = "user.domain.import";
|
||||
|
||||
domainImportList:{dnsProviderType:string,dnsProviderAccessId:string,key:string,title:string}[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user