1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/model/tasks/TaskManager.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-07-28 04:56:12 +08:00
import {ITaskManager} from '../interfaces/ITaskManager';
import {TaskProgressDTO} from '../../../common/entities/settings/TaskProgressDTO';
import {ITask} from './ITask';
import {TaskRepository} from './TaskRepository';
import {Config} from '../../../common/config/private/Config';
2019-08-20 18:54:45 +08:00
import {TaskTriggerType} from '../../../common/entities/task/TaskScheduleDTO';
2019-07-28 04:56:12 +08:00
export class TaskManager implements ITaskManager {
getProgresses(): { [id: string]: TaskProgressDTO } {
const m: { [id: string]: TaskProgressDTO } = {};
TaskRepository.Instance.getAvailableTasks().forEach(t => m[t.Name] = t.Progress);
return m;
}
start(taskName: string, config: any): void {
const t = this.findTask(taskName);
if (t) {
t.start(config);
}
}
stop(taskName: string): void {
const t = this.findTask(taskName);
if (t) {
t.stop();
}
}
getAvailableTasks(): ITask<any>[] {
return TaskRepository.Instance.getAvailableTasks();
}
2019-08-20 18:54:45 +08:00
public runSchedules(): void {
Config.Server.tasks.scheduled.forEach(schedule => {
let nextRun = null;
switch (schedule.trigger.type) {
case TaskTriggerType.scheduled:
nextRun = Date.now() - schedule.trigger.time;
break;
/*case TaskTriggerType.periodic:
//TODo finish it
const getNextDayOfTheWeek = (dayOfWeek: number) => {
const refDate = new Date();
refDate.setHours(0, 0, 0, 0);
refDate.setDate(refDate.getDate() + (dayOfWeek + 7 - refDate.getDay()) % 7);
return refDate;
};
nextRun = Date.now() - schedule.trigger.periodicity;
break;*/
}
if (nextRun != null) {
setTimeout(() => {
this.start(schedule.taskName, schedule.config);
}, nextRun);
}
});
}
2019-07-28 04:56:12 +08:00
protected findTask(taskName: string): ITask<any> {
return this.getAvailableTasks().find(t => t.Name === taskName);
}
}