import { Application } from 'egg' ; import { scheduleJob } from 'node-schedule' ; import { BasicJobHandler } from './BasicJobHandler' ; import { SchedulerAgentScheduleEvent , AgentWorkerJobHandler } from './AgentWorkerJobHandler' ; import { AppPluginBase } from '../../basic/AppPluginBase' ; import { ISchedulerJobCallback , ISchedulerJobHandler } from './types' ; export class AppSchedulerManager extends AppPluginBase < null > { private handlerMap : Map < string , AgentWorkerJobHandler > ; constructor ( config , app ) { super ( config , app ) ; this . handlerMap = new Map < string , AgentWorkerJobHandler > ( ) ; } public async onReady ( ) { this . app . event . subscribeOne ( SchedulerAgentScheduleEvent , async ( e ) => { this . scheduleAgentJob ( e . name ) ; } ) ; } public async onStart ( ) { } public async onClose ( ) { } public register ( name , time , type , func ) { switch ( type ) { case 'worker' : const awj = new AgentWorkerJobHandler ( this . app , name , time , func ) ; this . handlerMap . set ( name , awj ) ; return awj ; case 'workers' : const j = scheduleJob ( name , time , func ) ; return new BasicJobHandler ( j ) ; default : throw new Error ( `Not supported type ${ type }.` ) ; } } private async scheduleAgentJob ( name ) { const job = this . handlerMap . get ( name ) ; if ( job ) { job . invoke ( ) ; } } }