Skip to content

Commit

Permalink
fix(sdk): cron events not triggered on simulator (#6844)
Browse files Browse the repository at this point in the history
Needed for #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)*.
  • Loading branch information
Chriscbr authored Jul 3, 2024
1 parent 90f2f73 commit fdaffe5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions libs/wingsdk/src/target-sim/schedule.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class Schedule

constructor(props: ScheduleSchema) {
this.interval = parseExpression(props.cronExpression, { utc: true });
this.scheduleFunction();
}

private get context(): ISimulatorContext {
Expand All @@ -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<ScheduleAttributes> {
this._context = context;
this.scheduleFunction();
return {};
}

Expand Down

0 comments on commit fdaffe5

Please sign in to comment.