chore: domain import task

This commit is contained in:
xiaojunnuo
2026-01-25 02:09:16 +08:00
parent 7058d5cb93
commit 65f9d482f3
9 changed files with 253 additions and 66 deletions
@@ -220,7 +220,7 @@ export class AutoCRegisterCron {
name: 'domain-expire-check',
cron: `0 ${randomMinute} ${randomHour} ? * ${randomWeek}`, // 每周随机一天检查一次
job: async () => {
await this.domainService.doSyncDomainsExpirationDate({})
await this.domainService.startSyncExpirationTask({})
}
})
}
@@ -9,8 +9,11 @@ export class BackTaskExecutor {
this.tasks[type] = {}
}
const oldTask = this.tasks[type][task.key]
if (oldTask && oldTask.status === "running") {
throw new Error(`任务 ${task.title} 正在运行中`)
if (oldTask ){
if (oldTask.status === "running") {
throw new Error(`任务 ${task.title} 正在运行中`)
}
this.clear(type, task.key);
}
this.tasks[type][task.key] = task
this.run(task);
@@ -55,9 +58,9 @@ export class BackTaskExecutor {
} finally {
task.endTime = Date.now();
task.status = "done";
task.timeoutId = setTimeout(() => {
task.setTimeoutId(setTimeout(() => {
this.clear(type, task.key);
}, 24 * 60 * 60 * 1000);
}, 24 * 60 * 60 * 1000));
delete task.run;
}
}
@@ -76,11 +79,11 @@ export class BackTask {
endTime: number;
status: string = "pending";
errors?: string[] = [];
timeoutId?: NodeJS.Timeout;
private _timeoutId?: NodeJS.Timeout;
run: (task: BackTask) => Promise<void>;
private _run: (task: BackTask) => Promise<void>;
constructor(opts: {
type: string,
@@ -90,21 +93,64 @@ export class BackTask {
this.type = type
this.key = key;
this.title = title;
Object.defineProperty(this, 'run', {
value: run,
writable: true,
enumerable: false, // 设置为false使其不可遍历
this._run = run;
Object.defineProperty(this, '_run', {
enumerable: false,
value: this._run
})
Object.defineProperty(this, '_timeoutId', {
enumerable: false,
value: null
})
Object.defineProperty(this, 'progress', {
get: ()=> {
return this.getProgress()
},
enumerable: true, // 关键:设置为可枚举
configurable: true
});
Object.defineProperty(this, 'successCount', {
get: ()=> {
return this.getSuccessCount()
},
enumerable: true, // 关键:设置为可枚举
configurable: true
})
Object.defineProperty(this, 'errorCount', {
get: ()=> {
return this.getErrorCount()
},
enumerable: true, // 关键:设置为可枚举
configurable: true
})
Object.defineProperty(this, 'skipCount', {
get: ()=> {
return this.getSkipCount()
},
enumerable: true, // 关键:设置为可枚举
configurable: true
})
}
async run(task: BackTask) {
return await this._run(task);
}
clearTimeout() {
if (this.timeoutId) {
clearTimeout(this.timeoutId);
this.timeoutId = null;
if (this._timeoutId) {
clearTimeout(this._timeoutId);
this._timeoutId = null;
}
}
setTimeoutId(timeoutId: NodeJS.Timeout) {
this.clearTimeout();
this._timeoutId = timeoutId;
}
setTotal(total: number) {
this.total = total;
}
@@ -117,22 +163,23 @@ export class BackTask {
this.errors.push(error)
}
getSuccessCount() {
return this.current - this.errors.length
}
getErrorCount() {
return this.errors.length
}
getProgress() {
return (this.current / this.total * 1.0).toFixed(2)
}
getSkipCount() {
return this.skip
}
getSuccessCount() {
return this.current - this.errors.length
}
getProgress() {
return (this.current * 1.0 / this.total * 100.0);
}
incrementSkip() {
this.skip++
}
@@ -7,15 +7,15 @@ import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import dayjs from 'dayjs';
import { In, Not, Repository } from 'typeorm';
import { BackTask, taskExecutor } from '../../basic/service/task-executor.js';
import { CnameRecordEntity } from "../../cname/entity/cname-record.js";
import { CnameRecordService } from '../../cname/service/cname-record-service.js';
import { UserDomainImportSetting } from '../../mine/service/models.js';
import { UserSettingsService } from '../../mine/service/user-settings-service.js';
import { SubDomainsGetter } from '../../pipeline/service/getter/sub-domain-getter.js';
import { TaskServiceBuilder } from '../../pipeline/service/getter/task-service-getter.js';
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;
@@ -306,7 +306,7 @@ export class DomainService extends BaseService<DomainEntity> {
}
await doPageTurn({ pager, getPage, itemHandle, batchHandle })
const key = `user_${userId || 0}`
logger.info(`从域名提供商${dnsProviderType}导入域名完成(${key}),共导入${task.current}个域名,跳过${task.getSkipCount()}个域名`)
logger.info(`从域名提供商${dnsProviderType}导入域名完成(${key}),共导入${task.total}个域名,跳过${task.getSkipCount()}个域名,成功${task.getSuccessCount()}个域名,失败${task.getErrorCount()}个域名`)
}
async getDomainImportTaskStatus(req:{userId?:number}) {
@@ -324,7 +324,7 @@ export class DomainService extends BaseService<DomainEntity> {
taskList.push({
...item,
task,
task:task,
})
}
return taskList
@@ -377,7 +377,7 @@ export class DomainService extends BaseService<DomainEntity> {
async getSyncExpirationTaskStatus(req:{userId?:number}) {
const userId = req.userId || 0
const userId = req.userId ?? 'all'
const key = `user_${userId}`
const task = taskExecutor.get(DOMAIN_EXPIRE_TASK_TYPE,key)
return task
@@ -385,7 +385,7 @@ export class DomainService extends BaseService<DomainEntity> {
async startSyncExpirationTask(req: { userId?: number }) {
const userId = req.userId
const key = `user_${userId || 0}`
const key = `user_${userId ?? 'all'}`
taskExecutor.start(new BackTask({
type: DOMAIN_EXPIRE_TASK_TYPE,
key,
@@ -501,7 +501,7 @@ export class DomainService extends BaseService<DomainEntity> {
await doPageTurn({ pager, getPage: getDomainPage, itemHandle: itemHandle })
const key = `user_${req.userId || 'all'}`
logger.info(`同步用户(${key})注册域名过期时间完成(${req.task.getSuccessCount()}个成功,${req.task.errors.length}个失败)` )
logger.info(`同步用户(${key})注册域名过期时间完成(${req.task.getSuccessCount()}个成功,${req.task.getErrorCount()}个失败)` )
}
@@ -499,7 +499,8 @@ export class CnameRecordService extends BaseService<CnameRecordEntity> {
throw new ValidateException("CNAME服务提供商不能为空");
}
taskExecutor.start("cnameImport",new BackTask({
taskExecutor.start(new BackTask({
type:"cnameImport",
key: "user_"+userId,
title: "导入CNAME记录",
run: async (task) => {