Skip to content

Let's use AI to Earn!

feat: 修复任务的自动启动

牛文政b5b965debe85
diff --git a/electron/main/autoRun/comment.ts b/electron/main/autoRun/comment.ts
index 6e3b0132..75c8cc74 100644
--- a/electron/main/autoRun/comment.ts
+++ b/electron/main/autoRun/comment.ts
@@ -11,11 +11,20 @@ export const autoRunTypeEtTag = new Map<AutoRunType, string>([
[AutoRunType.ReplyComment, 'ET_AUTO_RUN_REPLY_COMMENT'],
]);
+export enum CycleType {
+ day = 'day', // 每天HH点触发
+ week = 'week', // 每周D日触发(周日=0,周一=1,..., 周六=6)
+ month = 'month', // 每月DD日触发
+}
+
// 工具函数:解析周期类型
-function parseCycleType(cycleType: string): { type: string; param: number } {
+export function parseCycleType(cycleType: string): {
+ type: CycleType | '';
+ param: number;
+} {
const [_, type, paramStr] = cycleType.match(/(\w+)-(\d+)/) || [];
return {
- type: type || '',
+ type: type as CycleType,
param: parseInt(paramStr || '0'),
};
}
diff --git a/electron/main/autoRun/controller.ts b/electron/main/autoRun/controller.ts
index 16272dd2..05d54f7c 100644
--- a/electron/main/autoRun/controller.ts
+++ b/electron/main/autoRun/controller.ts
@@ -10,7 +10,7 @@ import { AutoRunService } from './service';
import { AutoRunStatus, AutoRunType } from '../../db/models/autoRun';
import { getUserInfo } from '../user/comment';
import { EtEvent } from '../../global/event';
-import { autoRunTypeEtTag, hasTriggered } from './comment';
+import { autoRunTypeEtTag } from './comment';
import { AutoRunRecordStatus } from '../../db/models/autoRunRecord';
@Controller()
@@ -161,14 +161,15 @@ export class AutoRunController {
userInfo.id,
);
- // 遍历自动运行列表,满足运行条件的通知运行
- for (const item of autoRunList) {
+ autoRunList.map(async (item) => {
const tag = autoRunTypeEtTag.get(item.type);
- if (!tag) continue;
- const needRun = hasTriggered(item.cycleType);
- if (!needRun) continue;
+ if (!tag) return;
+
+ const needRun = await this.autoRunService.isNeedAutoRunToRun(item);
+ if (!needRun) return;
+
EtEvent.emit(tag, item);
- }
+ });
} catch (error) {
console.error('---- syncAllAutoRunStart ---- error', error);
}
diff --git a/electron/main/autoRun/service.ts b/electron/main/autoRun/service.ts
index e408b8ce..8fabf534 100644
--- a/electron/main/autoRun/service.ts
+++ b/electron/main/autoRun/service.ts
@@ -14,10 +14,11 @@ import {
AutoRunRecordModel,
AutoRunRecordStatus,
} from '../../db/models/autoRunRecord';
-import { FindOptionsWhere, Not, Repository } from 'typeorm';
+import { FindOptionsWhere, MoreThan, Not, Repository } from 'typeorm';
import { AppDataSource } from '../../db';
import windowOperate from '../../util/windowOperate';
import { SendChannelEnum } from '../../../commont/UtilsEnum';
+import { hasTriggered, parseCycleType } from './comment';
@Injectable()
export class AutoRunService {
@@ -193,4 +194,50 @@ export class AutoRunService {
error,
);
}
+
+ // 查找周期内的最近一条记录
+ async findLastAutoRunRecord(
+ autoRun: AutoRunModel,
+ cycleType: 'day' | 'week' | 'month',
+ ) {
+ const where: FindOptionsWhere<AutoRunRecordModel> = {
+ autoRunId: autoRun.id,
+ };
+
+ if (cycleType === 'day') {
+ where.createTime = MoreThan(new Date(new Date().setHours(0, 0, 0, 0)));
+ } else if (cycleType === 'week') {
+ where.createTime = MoreThan(
+ new Date(new Date().setDate(new Date().getDate() - 7)),
+ );
+ } else if (cycleType === 'month') {
+ where.createTime = MoreThan(
+ new Date(new Date().setDate(new Date().getDate() - 30)),
+ );
+ }
+
+ const lastRecord = await this.autoRunRecordRepository.findOne({
+ where,
+ order: {
+ createTime: 'DESC',
+ },
+ });
+
+ return lastRecord;
+ }
+
+ // 判断是否需要触发运行
+ async isNeedAutoRunToRun(autoRun: AutoRunModel): Promise<boolean> {
+ const { cycleType } = autoRun;
+
+ const isHasTriggered = hasTriggered(cycleType);
+ if (!isHasTriggered) return false;
+
+ const { type } = parseCycleType(cycleType);
+ if (!type) return false;
+
+ const record = await this.findLastAutoRunRecord(autoRun, type);
+
+ return !record;
+ }
}