Skip to content

Commit

Permalink
Merge of #5956
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Mar 18, 2024
2 parents 2607739 + a437d2e commit 4b19eb2
Show file tree
Hide file tree
Showing 15 changed files with 1,561 additions and 80 deletions.
10 changes: 9 additions & 1 deletion docs/docs/04-standard-library/cloud/schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,21 @@ 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
8 changes: 0 additions & 8 deletions examples/tests/sdk_tests/schedule/init.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,4 @@ if (util.env("WING_TARGET") != "sim") {
error = e;
}
assert(error == "cron string must be UNIX cron format [minute] [hour] [day of month] [month] [day of week]");


try {
new cloud.Schedule( cron: "* * * * *" ) as "s5";
} catch e {
error = e;
}
assert(error == "cannot use * in both the Day-of-month and Day-of-week fields. If you use it in one, you must use ? in the other");
}
30 changes: 10 additions & 20 deletions libs/awscdk/src/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ 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 @@ -25,27 +27,15 @@ 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) {
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];
}
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]; }

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

0 comments on commit 4b19eb2

Please sign in to comment.