Some refactoring
This commit is contained in:
		| @@ -11,7 +11,7 @@ export function registerAction(constructor: Function) { | |||||||
|     actionMap[constructor.name] = constructor; |     actionMap[constructor.name] = constructor; | ||||||
| } | } | ||||||
|  |  | ||||||
| export function createAction(name: string, scheduler: Scheduler): ActionController | undefined { | export function createActionHandler(name: string, scheduler: Scheduler): ActionController | undefined { | ||||||
|     const storedFunction = actionMap[name]; |     const storedFunction = actionMap[name]; | ||||||
|     if (storedFunction === undefined) { |     if (storedFunction === undefined) { | ||||||
|         return undefined; |         return undefined; | ||||||
|   | |||||||
| @@ -3,8 +3,8 @@ import { AbortTaskError, ActionError, TryLaterError } from './Errors'; | |||||||
| import { Task } from './Queue/TaskQueue'; | import { Task } from './Queue/TaskQueue'; | ||||||
| import { Command } from './Command'; | import { Command } from './Command'; | ||||||
| import { TaskQueueRenderer } from './TaskQueueRenderer'; | import { TaskQueueRenderer } from './TaskQueueRenderer'; | ||||||
| import { createAction } from './Action/ActionController'; | import { createActionHandler } from './Action/ActionController'; | ||||||
| import { createTask } from './Task/TaskController'; | import { createTaskHandler } from './Task/TaskController'; | ||||||
| import { ConsoleLogger, Logger } from './Logger'; | import { ConsoleLogger, Logger } from './Logger'; | ||||||
| import { GrabberManager } from './Grabber/GrabberManager'; | import { GrabberManager } from './Grabber/GrabberManager'; | ||||||
| import { Scheduler } from './Scheduler'; | import { Scheduler } from './Scheduler'; | ||||||
| @@ -36,29 +36,30 @@ export class Executor { | |||||||
|         await waitForLoad(); |         await waitForLoad(); | ||||||
|         await sleepMicro(); |         await sleepMicro(); | ||||||
|  |  | ||||||
|  |         this.renderInfo(); | ||||||
|  |  | ||||||
|  |         const sleep = createExecutionLoopSleeper(); | ||||||
|  |  | ||||||
|  |         while (true) { | ||||||
|  |             await sleep(); | ||||||
|  |             if (!this.isPaused()) { | ||||||
|  |                 await this.doTaskProcessingStep(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private renderInfo() { | ||||||
|         try { |         try { | ||||||
|             markPage('Executor', this.version); |             markPage('Executor', this.version); | ||||||
|             this.renderTaskQueue(); |             this.renderTaskQueue(); | ||||||
|         } catch (e) { |         } catch (e) { | ||||||
|             this.logger.warn(e); |             this.logger.warn(e); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         let skipFirstSleep = true; |  | ||||||
|  |  | ||||||
|         while (true) { |  | ||||||
|             if (skipFirstSleep) { |  | ||||||
|                 skipFirstSleep = false; |  | ||||||
|             } else { |  | ||||||
|                 await sleepMicro(); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     private isPaused(): boolean { | ||||||
|         const { pauseTs } = this.executionState.getExecutionSettings(); |         const { pauseTs } = this.executionState.getExecutionSettings(); | ||||||
|             if (pauseTs > timestamp()) { |         return pauseTs > timestamp(); | ||||||
|                 continue; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             await this.doTaskProcessingStep(); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private renderTaskQueue() { |     private renderTaskQueue() { | ||||||
| @@ -96,24 +97,24 @@ export class Executor { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private async processActionCommand(cmd: Command, task: Task) { |     private async processActionCommand(cmd: Command, task: Task) { | ||||||
|         const actionController = createAction(cmd.name, this.scheduler); |         const actionHandler = createActionHandler(cmd.name, this.scheduler); | ||||||
|         this.logger.log('PROCESS ACTION', cmd.name, actionController); |         this.logger.log('PROCESS ACTION', cmd.name, actionHandler); | ||||||
|         if (cmd.args.taskId !== task.id) { |         if (cmd.args.taskId !== task.id) { | ||||||
|             throw new ActionError(`Action task id ${cmd.args.taskId} not equal current task id ${task.id}`); |             throw new ActionError(`Action task id ${cmd.args.taskId} not equal current task id ${task.id}`); | ||||||
|         } |         } | ||||||
|         if (actionController) { |         if (actionHandler) { | ||||||
|             this.statistics.incrementAction(); |             this.statistics.incrementAction(); | ||||||
|             await actionController.run(cmd.args, task); |             await actionHandler.run(cmd.args, task); | ||||||
|         } else { |         } else { | ||||||
|             this.logger.warn('ACTION NOT FOUND', cmd.name); |             this.logger.warn('ACTION NOT FOUND', cmd.name); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private async processTaskCommand(task: Task) { |     private async processTaskCommand(task: Task) { | ||||||
|         const taskController = createTask(task.name, this.scheduler); |         const taskHandler = createTaskHandler(task.name, this.scheduler); | ||||||
|         this.logger.log('PROCESS TASK', task.name, task, taskController); |         this.logger.log('PROCESS TASK', task.name, task, taskHandler); | ||||||
|         if (taskController) { |         if (taskHandler) { | ||||||
|             await taskController.run(task); |             await taskHandler.run(task); | ||||||
|         } else { |         } else { | ||||||
|             this.logger.warn('TASK NOT FOUND', task.name); |             this.logger.warn('TASK NOT FOUND', task.name); | ||||||
|             this.scheduler.removeTask(task.id); |             this.scheduler.removeTask(task.id); | ||||||
| @@ -153,3 +154,14 @@ export class Executor { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | function createExecutionLoopSleeper() { | ||||||
|  |     let skipFirstSleep = true; | ||||||
|  |     return async () => { | ||||||
|  |         if (skipFirstSleep) { | ||||||
|  |             skipFirstSleep = false; | ||||||
|  |         } else { | ||||||
|  |             await sleepMicro(); | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  | } | ||||||
|   | |||||||
| @@ -9,7 +9,7 @@ export function registerTask(constructor: Function) { | |||||||
|     taskMap[constructor.name] = constructor; |     taskMap[constructor.name] = constructor; | ||||||
| } | } | ||||||
|  |  | ||||||
| export function createTask(name: string, scheduler: Scheduler): TaskController | undefined { | export function createTaskHandler(name: string, scheduler: Scheduler): TaskController | undefined { | ||||||
|     const storedFunction = taskMap[name]; |     const storedFunction = taskMap[name]; | ||||||
|     if (storedFunction === undefined) { |     if (storedFunction === undefined) { | ||||||
|         return undefined; |         return undefined; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user