Skip to content

Commit c9b440c

Browse files
fix: setting triggers.crons:[] in Wrangler config should delete deployed cron schedules
1 parent ec513a4 commit c9b440c

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed

.changeset/shaky-planets-feel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: setting triggers.crons:[] in Wrangler config should delete deployed cron schedules

packages/wrangler/src/__tests__/config/configuration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe("normalizeAndValidateConfig()", () => {
114114
ai: undefined,
115115
version_metadata: undefined,
116116
triggers: {
117-
crons: [],
117+
crons: undefined,
118118
},
119119
unsafe: {
120120
bindings: undefined,

packages/wrangler/src/config/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export const defaultWranglerConfig: Config = {
326326
jsx_fragment: "React.Fragment",
327327
migrations: [],
328328
triggers: {
329-
crons: [],
329+
crons: undefined,
330330
},
331331
rules: [],
332332
build: { command: undefined, watch_dir: "./src", cwd: undefined },

packages/wrangler/src/config/environment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ interface EnvironmentInheritable {
280280
*
281281
* More details here https://developers.cloudflare.com/workers/platform/cron-triggers
282282
*
283-
* @default {crons:[]}
283+
* @default {crons: undefined}
284284
* @inheritable
285285
*/
286-
triggers: { crons: string[] };
286+
triggers: { crons: string[] | undefined };
287287

288288
/**
289289
* Specify limits for runtime behavior.

packages/wrangler/src/config/validation.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
inheritableInLegacyEnvironments,
1818
isBoolean,
1919
isMutuallyExclusiveWith,
20-
isObjectWith,
2120
isOptionalProperty,
2221
isRequiredProperty,
2322
isString,
@@ -1122,8 +1121,8 @@ function normalizeAndValidateEnvironment(
11221121
topLevelEnv,
11231122
rawEnv,
11241123
"triggers",
1125-
isObjectWith("crons"),
1126-
{ crons: [] }
1124+
validateTriggers,
1125+
{ crons: undefined }
11271126
),
11281127
assets: normalizeAndValidateAssets(diagnostics, topLevelEnv, rawEnv),
11291128
limits: normalizeAndValidateLimits(diagnostics, topLevelEnv, rawEnv),
@@ -1509,6 +1508,44 @@ const validateAndNormalizeRules = (
15091508
);
15101509
};
15111510

1511+
const validateTriggers: ValidatorFn = (
1512+
diagnostics,
1513+
triggersFieldName,
1514+
triggersValue
1515+
) => {
1516+
if (triggersValue === undefined || triggersValue === null) {
1517+
return true;
1518+
}
1519+
1520+
if (typeof triggersValue !== "object") {
1521+
diagnostics.errors.push(
1522+
`Expected "${triggersFieldName}" to be of type object but got ${JSON.stringify(
1523+
triggersValue
1524+
)}.`
1525+
);
1526+
return false;
1527+
}
1528+
1529+
let isValid = true;
1530+
1531+
if ("crons" in triggersValue && !Array.isArray(triggersValue.crons)) {
1532+
diagnostics.errors.push(
1533+
`Expected "${triggersFieldName}.crons" to be of type array, but got ${JSON.stringify(triggersValue)}.`
1534+
);
1535+
isValid = false;
1536+
}
1537+
1538+
isValid =
1539+
validateAdditionalProperties(
1540+
diagnostics,
1541+
triggersFieldName,
1542+
Object.keys(triggersValue),
1543+
["crons"]
1544+
) && isValid;
1545+
1546+
return isValid;
1547+
};
1548+
15121549
const validateRules =
15131550
(envName: string): ValidatorFn =>
15141551
(diagnostics, field, envValue, config) => {

packages/wrangler/src/dev/miniflare.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ export async function buildMiniflareOptions(
10271027
internalObjects: CfDurableObject[];
10281028
entrypointNames: string[];
10291029
}> {
1030-
if (config.crons.length > 0 && !config.testScheduled) {
1030+
if (config.crons?.length && !config.testScheduled) {
10311031
if (!didWarnMiniflareCronSupport) {
10321032
didWarnMiniflareCronSupport = true;
10331033
logger.warn(

packages/wrangler/src/triggers/deploy.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default async function triggersDeploy(
3838
): Promise<string[] | void> {
3939
const { config, accountId, name: scriptName } = props;
4040

41-
const triggers = props.triggers || config.triggers?.crons;
41+
const schedules = props.triggers || config.triggers?.crons;
4242
const routes =
4343
props.routes ?? config.routes ?? (config.route ? [config.route] : []) ?? [];
4444
const routesOnly: Array<Route> = [];
@@ -258,17 +258,18 @@ export default async function triggersDeploy(
258258
}
259259

260260
// Configure any schedules for the script.
261-
// TODO: rename this to `schedules`?
262-
if (triggers && triggers.length) {
261+
// If schedules is not defined then we just leave whatever is previously deployed alone.
262+
// If it is an empty array we will remove all schedules.
263+
if (schedules) {
263264
deployments.push(
264265
fetchResult(`${workerUrl}/schedules`, {
265266
// Note: PUT will override previous schedules on this script.
266267
method: "PUT",
267-
body: JSON.stringify(triggers.map((cron) => ({ cron }))),
268+
body: JSON.stringify(schedules.map((cron) => ({ cron }))),
268269
headers: {
269270
"Content-Type": "application/json",
270271
},
271-
}).then(() => triggers.map((trigger) => `schedule: ${trigger}`))
272+
}).then(() => schedules.map((trigger) => `schedule: ${trigger}`))
272273
);
273274
}
274275

0 commit comments

Comments
 (0)