@@ -68,8 +68,12 @@ function configOf(runtime: AgentRuntimeContext<CronModelState>): IConfigService
6868 return runtime . get ( IConfigService ) ;
6969}
7070
71+ function readCronConfig ( config : IConfigService ) : CronConfig {
72+ return config . get < CronConfig > ( CRON_SECTION ) ?? DEFAULT_CRON_CONFIG ;
73+ }
74+
7175function cronConfigOf ( runtime : AgentRuntimeContext < CronModelState > ) : CronConfig {
72- return configOf ( runtime ) . get < CronConfig > ( CRON_SECTION ) ?? DEFAULT_CRON_CONFIG ;
76+ return readCronConfig ( configOf ( runtime ) ) ;
7377}
7478
7579function clocksOf ( runtime : AgentRuntimeContext < CronModelState > ) : ClockSources {
@@ -265,9 +269,12 @@ async function processDue(
265269async function tickCron (
266270 runtime : AgentRuntimeContext < CronModelState > ,
267271 state : CronEffectState ,
272+ config : IConfigService ,
273+ isDisposed : ( ) => boolean ,
268274) : Promise < void > {
269- await configOf ( runtime ) . ready ;
270- if ( cronConfigOf ( runtime ) . disabled || runtime . getState ( ) . size === 0 ) return ;
275+ await config . ready ;
276+ if ( isDisposed ( ) ) return ;
277+ if ( readCronConfig ( config ) . disabled || runtime . getState ( ) . size === 0 ) return ;
271278 if ( runtime . get ( IAgentLoopService ) . status ( ) . state === 'running' ) return ;
272279 const now = state . clocks . wallNow ( ) ;
273280 await Promise . all ( [ ...runtime . getState ( ) . values ( ) ] . map ( ( task ) => processDue ( runtime , state , task , now ) ) ) ;
@@ -286,6 +293,7 @@ const cronEffects = fromCallback(({
286293 sendBack : ( event : CronActorEvent ) => void ;
287294} ) => {
288295 if ( input . runtime . agent . agentId !== MAIN_AGENT_ID ) return ;
296+ const config = configOf ( input . runtime ) ;
289297 const timer = new IntervalTimer ( { unref : true } ) ;
290298 const state : CronEffectState = {
291299 clocks : SYSTEM_CLOCKS ,
@@ -297,18 +305,22 @@ const cronEffects = fromCallback(({
297305 let disposed = false ;
298306 let signalHandler : NodeJS . SignalsListener | undefined ;
299307 receive ( ( event ) => {
300- void tickCron ( input . runtime , state ) . then ( event . resolve , event . reject ) ;
308+ if ( disposed ) {
309+ event . resolve ?.( ) ;
310+ return ;
311+ }
312+ void tickCron ( input . runtime , state , config , ( ) => disposed ) . then ( event . resolve , event . reject ) ;
301313 } ) ;
302- input . restore . waitUntil ( configOf ( input . runtime ) . ready . then ( ( ) => {
314+ input . restore . waitUntil ( config . ready . then ( ( ) => {
303315 if ( disposed ) return ;
304- const config = cronConfigOf ( input . runtime ) ;
305- state . clocks = resolveClockSources ( config . clock , config . debug ) ?? SYSTEM_CLOCKS ;
306- const poll = config . manualTick ? null : config . pollIntervalMs ;
316+ const current = readCronConfig ( config ) ;
317+ state . clocks = resolveClockSources ( current . clock , current . debug ) ?? SYSTEM_CLOCKS ;
318+ const poll = current . manualTick ? null : current . pollIntervalMs ;
307319 const interval = poll === undefined ? DEFAULT_POLL_INTERVAL_MS : poll ;
308320 if ( interval !== null && interval !== 0 ) {
309321 timer . cancelAndSet ( ( ) => { sendBack ( { type : 'cron.tick' } ) ; } , interval ) ;
310322 }
311- if ( process . platform !== 'win32' && config . manualTick ) {
323+ if ( process . platform !== 'win32' && current . manualTick ) {
312324 signalHandler = ( ) => { sendBack ( { type : 'cron.tick' } ) ; } ;
313325 process . on ( 'SIGUSR1' , signalHandler ) ;
314326 }
0 commit comments