Skip to content

Commit

Permalink
revert(sdk): making cron schedule more cloud agnostic (#5953)
Browse files Browse the repository at this point in the history
This reverts commit 81fbfb5.

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] 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
hasanaburayyan authored Mar 16, 2024
1 parent 81fbfb5 commit d9beaf1
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 1,561 deletions.
10 changes: 1 addition & 9 deletions docs/docs/04-standard-library/cloud/schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,13 @@ Trigger events according to a cron schedule using the UNIX cron format.

Timezone is UTC.
[minute] [hour] [day of month] [month] [day of week]
'*' means all possible values.
'-' means a range of values.
',' means a list of values.
[minute] allows 0-59.
[hour] allows 0-23.
[day of month] allows 1-31.
[month] allows 1-12 or JAN-DEC.
[day of week] allows 0-6 or SUN-SAT.

---

*Example*

```wing
"* * * * *"
"0/1 * ? * *"
```


Expand Down
30 changes: 20 additions & 10 deletions libs/awscdk/src/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import { Construct } from "constructs";
import { App } from "./app";
import { cloud, core, std } from "@winglang/sdk";
import { convertBetweenHandlers } from "@winglang/sdk/lib/shared/convert";
import { convertUnixCronToAWSCron } from "@winglang/sdk/lib/shared-aws/schedule";
import { isAwsCdkFunction } from "./function";


/**
* AWS implementation of `cloud.Schedule`.
*
Expand All @@ -27,15 +25,27 @@ export class Schedule extends cloud.Schedule {

const { rate, cron } = props;

/*
* The schedule cron string is Unix cron format: [minute] [hour] [day of month] [month] [day of week]
* AWS EventBridge Schedule uses a 6 field format which includes year: [minute] [hour] [day of month] [month] [day of week] [year]
* https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html#cron-based
*
* We append * to the cron string for year field.
*/
if (cron) {
let cronOpt: { [k: string]: string } = {};
const awsCron = convertUnixCronToAWSCron(cron);
const cronArr = awsCron.split(" ");
if (cronArr[0] !== "*" && cronArr[0] !== "?") { cronOpt.minute = cronArr[0]; }
if (cronArr[1] !== "*" && cronArr[1] !== "?") { cronOpt.hour = cronArr[1]; }
if (cronArr[2] !== "*" && cronArr[2] !== "?") { cronOpt.day = cronArr[2]; }
if (cronArr[3] !== "*" && cronArr[3] !== "?") { cronOpt.month = cronArr[3]; }
if (cronArr[4] !== "*" && cronArr[4] !== "?") { cronOpt.weekDay = cronArr[4]; }
const cronArr = cron.split(" ");
let cronOpt: { [k: string]: string } = {
minute: cronArr[0],
hour: cronArr[1],
month: cronArr[3],
year: "*",
};
if (cronArr[2] !== "?") {
cronOpt.day = cronArr[2];
}
if (cronArr[4] !== "?") {
cronOpt.weekDay = cronArr[4];
}

this.scheduleExpression = EventSchedule.cron(cronOpt);
} else {
Expand Down
Loading

0 comments on commit d9beaf1

Please sign in to comment.