This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
164 lines (146 loc) · 5.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const { localCrontabToUtcCrontabs } = require("local-crontab");
const { timezones } = require("timezone-enum/src/timezones.json")
/**
* Convert an AWS CloudWatch crontab to a standard crontab.
*
* Main differences are:
* * A year field
* * ? instead of * sometimes
* * Some others.. implementation TBD
*
* The data that is removed is returned as well so that it can be used to
* roundtrip back to an AWS CloudWatch crontab
*
*/
const convertAwsToStandardCrontab = awsCrontab => {
const crontabParts = awsCrontab.split(/\s+/);
// standard crontabs don't have a year
const year = crontabParts.pop();
// replace ? with *, but remember where they were
const questionParts = [];
for (const i in crontabParts) {
if (crontabParts[i] === "?") {
questionParts.push(i);
crontabParts[i] = "*";
}
}
return {
crontab: crontabParts.join(" "),
awsSpecificDetails: {
year,
questionParts
}
};
};
const convertStandardCrontabToAws = ({ crontab, awsSpecificDetails }) => {
const parts = crontab.split(/\s+/);
for (const questionPart of awsSpecificDetails.questionParts) {
parts[questionPart] = parts[questionPart].replace(/\*/, "?");
}
parts.push(awsSpecificDetails.year);
return parts.join(" ");
};
const convertAwsLocalCrontabToAwsUtcCrontab = (localCrontab, timezone) => {
const { crontab, awsSpecificDetails } = convertAwsToStandardCrontab(
localCrontab
);
const utcCrontabs = localCrontabToUtcCrontabs(crontab, timezone);
return utcCrontabs.map(crontab =>
convertStandardCrontabToAws({ crontab, awsSpecificDetails })
);
};
function convertCrontabs() {
this.serverless.cli.log("Converting local crontabs to UTC crontabs...");
const newCrontabsMap = {};
for (const funcName in this.serverless.service.functions) {
for (const eventIndex in this.serverless.service.functions[funcName]
.events) {
const event = this.serverless.service.functions[funcName].events[
eventIndex
];
// only process events with a schedule & a timezone
if (
event.hasOwnProperty("schedule") &&
event.schedule.hasOwnProperty("timezone")
) {
const schedule = event.schedule;
const ratesAreArray = Array.isArray(schedule.rate);
const rates = ratesAreArray ? schedule.rate : [schedule.rate];
const matches = rates
.map(rate => rate.match(/^cron\((.*)\)$/))
.filter(match => match && match[1])
if (!matches.length)
// skip rate() schedules
continue;
// convert the local crontab to utc crontabs
const newCrontabs = matches.flatMap(match => {
const convertedCrontabs = convertAwsLocalCrontabToAwsUtcCrontab(
match[1],
schedule.timezone
);
if (this.options.verbose || this.options.v) {
this.serverless.cli.log(`Converted ${match[1]} ${schedule.timezone} to
${convertedCrontabs.join("\n ")}`);
}
return convertedCrontabs;
})
// remove timezone from original schedule event
delete schedule.timezone;
// append new utc crontab schedule events
newCrontabsMap[funcName] = newCrontabsMap[funcName] || {
newCrontabs: [],
removeIndexes: []
};
newCrontabsMap[funcName].removeIndexes.splice(0, 0, eventIndex);
newCrontabsMap[funcName].newCrontabs.push(
...newCrontabs.map((crontab, i) => {
// When the rates are an array, the version of Serverless being used
// is one that doesn't support named schedules when there are multiple
// schedules or rates. See https://github.com/serverless/serverless/issues/9867 for details.
const addName = !ratesAreArray && schedule.name;
return ({
schedule: Object.assign({}, schedule, {
rate: ratesAreArray ? [`cron(${crontab})`] : `cron(${crontab})`,
name: addName ? `${schedule.name}-${i}` : undefined,
})
});
})
);
}
}
}
// remove the original schedule events
for (const funcName in newCrontabsMap) {
newCrontabsMap[funcName].removeIndexes.forEach(eventIndex => {
this.serverless.service.functions[funcName].events.splice(eventIndex, 1);
});
}
for (const funcName in newCrontabsMap) {
this.serverless.service.functions[funcName].events.push(
...newCrontabsMap[funcName].newCrontabs
);
}
}
class ServerlessLocalCrontabs {
constructor(serverless, options) {
this.serverless = serverless;
if (
this.serverless.configSchemaHandler &&
this.serverless.configSchemaHandler.defineFunctionEventProperties
) {
// Create schema for your properties. For reference use https://github.com/ajv-validator/ajv
this.serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'schedule', {
properties: {
timezone: {
enum: timezones
},
},
});
}
this.options = options;
this.hooks = {
"before:package:initialize": convertCrontabs.bind(this)
};
}
}
module.exports = ServerlessLocalCrontabs;