fix(async-task-manager): add progressThrottles

This commit is contained in:
Chareice 2025-01-02 22:38:06 +08:00
parent 3111f29715
commit 64229f8409
No known key found for this signature in database
2 changed files with 55 additions and 30 deletions

View File

@ -129,10 +129,10 @@ export const useShared = () => {
}; };
} }
const file = value[0] ?? {}; const file = value[0] ?? {};
if (file.size > 10 * 1024 * 1024) { if (file.size > 80 * 1024 * 1024) {
return { return {
type: 'error', type: 'error',
message: t('File size cannot exceed 10M'), message: t('File size cannot exceed 80M'),
}; };
} }
if (!INCLUDE_FILE_TYPE.includes(file.type)) { if (!INCLUDE_FILE_TYPE.includes(file.type)) {

View File

@ -3,8 +3,11 @@ import { BaseTaskManager } from './base-task-manager';
import { AsyncTasksManager } from './interfaces/async-task-manager'; import { AsyncTasksManager } from './interfaces/async-task-manager';
import { CommandTaskType } from './command-task-type'; import { CommandTaskType } from './command-task-type';
import asyncTasksResource from './resourcers/async-tasks'; import asyncTasksResource from './resourcers/async-tasks';
import { throttle } from 'lodash';
export class PluginAsyncExportServer extends Plugin { export class PluginAsyncExportServer extends Plugin {
private progressThrottles: Map<string, Function> = new Map();
async afterAdd() {} async afterAdd() {}
async beforeLoad() { async beforeLoad() {
@ -17,13 +20,37 @@ export class PluginAsyncExportServer extends Plugin {
}); });
this.app.container.get<AsyncTasksManager>('AsyncTaskManager').registerTaskType(CommandTaskType); this.app.container.get<AsyncTasksManager>('AsyncTaskManager').registerTaskType(CommandTaskType);
this.app.acl.allow('asyncTasks', ['get', 'fetchFile'], 'loggedIn'); this.app.acl.allow('asyncTasks', ['get', 'fetchFile'], 'loggedIn');
} }
getThrottledProgressEmitter(taskId: string, userId: string) {
if (!this.progressThrottles.has(taskId)) {
this.progressThrottles.set(
taskId,
throttle(
(progress: any) => {
this.app.emit('ws:sendToTag', {
tagKey: 'userId',
tagValue: userId,
message: {
type: 'async-tasks:progress',
payload: {
taskId,
progress,
},
},
});
},
500,
{ leading: true, trailing: true },
),
);
}
return this.progressThrottles.get(taskId);
}
async load() { async load() {
this.app.resourceManager.define(asyncTasksResource); this.app.resourceManager.define(asyncTasksResource);
const asyncTaskManager = this.app.container.get<AsyncTasksManager>('AsyncTaskManager'); const asyncTaskManager = this.app.container.get<AsyncTasksManager>('AsyncTaskManager');
this.app.on(`ws:message:request:async-tasks:list`, async (message) => { this.app.on(`ws:message:request:async-tasks:list`, async (message) => {
@ -71,38 +98,36 @@ export class PluginAsyncExportServer extends Plugin {
asyncTaskManager.on('taskProgress', ({ task, progress }) => { asyncTaskManager.on('taskProgress', ({ task, progress }) => {
const userId = task.tags['userId']; const userId = task.tags['userId'];
if (userId) { if (userId) {
this.app.emit('ws:sendToTag', { const throttledEmit = this.getThrottledProgressEmitter(task.taskId, userId);
tagKey: 'userId', throttledEmit(progress);
tagValue: userId,
message: {
type: 'async-tasks:progress',
payload: {
taskId: task.taskId,
progress,
},
},
});
} }
}); });
asyncTaskManager.on('taskStatusChange', ({ task, status }) => { asyncTaskManager.on('taskStatusChange', ({ task, status }) => {
const userId = task.tags['userId']; const userId = task.tags['userId'];
if (userId) { if (!userId) return;
this.app.emit('ws:sendToTag', {
tagKey: 'userId', this.app.emit('ws:sendToTag', {
tagValue: userId, tagKey: 'userId',
message: { tagValue: userId,
type: 'async-tasks:status', message: {
payload: { type: 'async-tasks:status',
taskId: task.taskId, payload: {
status: task.toJSON().status, taskId: task.taskId,
}, status: task.toJSON().status,
}, },
}); },
} });
});
if (status.type !== 'running' && status.type !== 'pending') {
const throttled = this.progressThrottles.get(task.taskId);
if (throttled) {
// @ts-ignore
throttled.cancel();
this.progressThrottles.delete(task.taskId);
}
}
asyncTaskManager.on('taskStatusChange', ({ status }) => {
if (status.type === 'success') { if (status.type === 'success') {
this.app.emit('workflow:dispatch'); this.app.emit('workflow:dispatch');
} }