From fdaffe5b031c32ccec5f25796c90a6e432ebf623 Mon Sep 17 00:00:00 2001 From: Chris Rybicki Date: Wed, 3 Jul 2024 18:06:12 -0400 Subject: [PATCH] fix(sdk): cron events not triggered on simulator (#6844) Needed for https://github.com/winglang/wing/issues/6552 ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [ ] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- .../src/target-sim/schedule.inflight.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/libs/wingsdk/src/target-sim/schedule.inflight.ts b/libs/wingsdk/src/target-sim/schedule.inflight.ts index 01717f9e766..7b4ba33e906 100644 --- a/libs/wingsdk/src/target-sim/schedule.inflight.ts +++ b/libs/wingsdk/src/target-sim/schedule.inflight.ts @@ -24,7 +24,6 @@ export class Schedule constructor(props: ScheduleSchema) { this.interval = parseExpression(props.cronExpression, { utc: true }); - this.scheduleFunction(); } private get context(): ISimulatorContext { @@ -36,19 +35,32 @@ export class Schedule // Calculate the delay for the next execution private nextDelay(interval: CronExpression) { - return interval.next().toDate().getTime() - Date.now(); + const nextDate = interval.next().toDate(); + this.context.addTrace({ + type: TraceType.RESOURCE, + level: LogLevel.VERBOSE, + data: { + message: `Scheduling the next task to run at: ${nextDate} (the current time is ${new Date()})`, + }, + sourcePath: this.context.resourcePath, + sourceType: SCHEDULE_FQN, + timestamp: new Date().toISOString(), + }); + return nextDate.getTime() - new Date().getTime(); } // Recursively schedule the function to be executed private scheduleFunction() { + const delay = this.nextDelay(this.interval); this.intervalTimeout = setTimeout(() => { this.runTasks(); this.scheduleFunction(); - }, this.nextDelay(this.interval)); + }, delay); } public async init(context: ISimulatorContext): Promise { this._context = context; + this.scheduleFunction(); return {}; }