Skip to content

Commit

Permalink
feat: clean binary merge logic
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Jan 22, 2024
1 parent fb006e3 commit eb375ed
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
4 changes: 4 additions & 0 deletions app/core/entity/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ export class Task<T extends TaskBaseData = TaskBaseData> extends Entity {
return task;
}

public static needMergeWhenWaiting(type: Task['type']) {
return [ TaskType.SyncBinary, TaskType.SyncPackage ].includes(type);
}

start(): TaskUpdateCondition {
const condition = {
taskId: this.taskId,
Expand Down
9 changes: 0 additions & 9 deletions app/core/service/BinarySyncerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Binary } from '../entity/Binary';
import { TaskService } from './TaskService';
import { AbstractBinary, BinaryItem } from '../../common/adapter/binary/AbstractBinary';
import { AbstractService } from '../../common/AbstractService';
import { TaskRepository } from '../../repository/TaskRepository';
import { BinaryType } from '../../common/enum/Binary';
import { sortBy } from 'lodash';

Expand All @@ -36,8 +35,6 @@ export class BinarySyncerService extends AbstractService {
@Inject()
private readonly taskService: TaskService;
@Inject()
private readonly taskRepository: TaskRepository;
@Inject()
private readonly httpclient: EggHttpClient;
@Inject()
private readonly nfsAdapter: NFSAdapter;
Expand Down Expand Up @@ -89,13 +86,7 @@ export class BinarySyncerService extends AbstractService {
return await this.nfsAdapter.getDownloadUrlOrStream(binary.storePath);
}

// SyncBinary 由定时任务每台单机定时触发,手动去重
// 添加 bizId 在 db 防止重复,记录 id 错误
public async createTask(binaryName: BinaryName, lastData?: any) {
const existsTask = await this.taskRepository.findTaskByTargetName(binaryName, TaskType.SyncBinary);
if (existsTask) {
return existsTask;
}
try {
return await this.taskService.createTask(Task.createSyncBinary(binaryName, lastData), false);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion app/core/service/TaskService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class TaskService extends AbstractService {
const existsTask = await this.taskRepository.findTaskByTargetName(task.targetName, task.type);

// 只在包同步场景下做任务合并,其余场景通过 bizId 来进行任务幂等
if (existsTask && [ TaskType.SyncPackage, TaskType.SyncBinary ].includes(task.type)) {
if (existsTask && Task.needMergeWhenWaiting(task.type)) {
// 在包同步场景,如果任务还未被触发,就不继续重复创建
// 如果任务正在执行,可能任务状态已更新,这种情况需要继续创建
if (existsTask.state === TaskState.Waiting) {
Expand Down

0 comments on commit eb375ed

Please sign in to comment.