Skip to content

Commit 7d20dc2

Browse files
committed
Add environment filter to declarative schedules
1 parent 47f4726 commit 7d20dc2

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

apps/webapp/app/v3/services/createBackgroundWorker.server.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,18 @@ export async function syncDeclarativeSchedules(
521521
for (const task of tasksWithDeclarativeSchedules) {
522522
if (task.schedule === undefined) continue;
523523

524+
// Check if this schedule should be created in the current environment
525+
if (task.schedule.environments && task.schedule.environments.length > 0) {
526+
if (!task.schedule.environments.includes(environment.type)) {
527+
logger.debug("Skipping schedule creation due to environment filter", {
528+
taskId: task.id,
529+
environmentType: environment.type,
530+
allowedEnvironments: task.schedule.environments,
531+
});
532+
continue;
533+
}
534+
}
535+
524536
const existingSchedule = existingDeclarativeSchedules.find(
525537
(schedule) =>
526538
schedule.taskIdentifier === task.id &&

packages/core/src/v3/schemas/schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export type QueueManifest = z.infer<typeof QueueManifest>;
176176
export const ScheduleMetadata = z.object({
177177
cron: z.string(),
178178
timezone: z.string(),
179+
environments: z.array(EnvironmentType).optional(),
179180
});
180181

181182
const taskMetadata = {

packages/trigger-sdk/src/v3/schedules/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ export type ScheduleOptions<
2828
* "0 0 * * *"
2929
* ```
3030
*
31-
* 2. Or an object with a pattern and an optional timezone (default is "UTC")
31+
* 2. Or an object with a pattern, optional timezone, and optional environments
3232
* ```ts
3333
* {
3434
* pattern: "0 0 * * *",
35-
* timezone: "America/Los_Angeles"
35+
* timezone: "America/Los_Angeles",
36+
* environments: ["PRODUCTION", "STAGING"]
3637
* }
3738
* ```
3839
*
@@ -43,6 +44,20 @@ export type ScheduleOptions<
4344
| {
4445
pattern: string;
4546
timezone?: string;
47+
/** You can optionally specify which environments this schedule should run in.
48+
* When not specified, the schedule will run in all environments.
49+
*
50+
* @example
51+
* ```ts
52+
* environments: ["PRODUCTION", "STAGING"]
53+
* ```
54+
*
55+
* @example
56+
* ```ts
57+
* environments: ["PRODUCTION"] // Only run in production
58+
* ```
59+
*/
60+
environments?: Array<"DEVELOPMENT" | "STAGING" | "PRODUCTION" | "PREVIEW">;
4661
};
4762
};
4863

@@ -58,13 +73,16 @@ export function task<TIdentifier extends string, TOutput, TInitOutput extends In
5873
: undefined;
5974
const timezone =
6075
(params.cron && typeof params.cron !== "string" ? params.cron.timezone : "UTC") ?? "UTC";
76+
const environments =
77+
params.cron && typeof params.cron !== "string" ? params.cron.environments : undefined;
6178

6279
resourceCatalog.updateTaskMetadata(task.id, {
6380
triggerSource: "schedule",
6481
schedule: cron
6582
? {
6683
cron: cron,
6784
timezone,
85+
environments,
6886
}
6987
: undefined,
7088
});

references/v3-catalog/src/trigger/scheduled.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { logger, schedules, task } from "@trigger.dev/sdk/v3";
22

33
export const firstScheduledTask = schedules.task({
44
id: "first-scheduled-task",
5-
//every other minute
6-
// cron: "0 */2 * * *",
5+
//every other minute - only run in production and staging environments (skip development)
6+
cron: {
7+
pattern: "0 */2 * * *",
8+
environments: ["PRODUCTION", "STAGING"],
9+
},
710
run: async (payload, { ctx }) => {
811
const distanceInMs =
912
payload.timestamp.getTime() - (payload.lastTimestamp ?? new Date()).getTime();
@@ -22,10 +25,11 @@ export const firstScheduledTask = schedules.task({
2225

2326
export const secondScheduledTask = schedules.task({
2427
id: "second-scheduled-task",
25-
// cron: {
26-
// pattern: "0 5 * * *",
27-
// timezone: "Asia/Tokyo",
28-
// },
28+
cron: {
29+
pattern: "0 5 * * *",
30+
timezone: "Asia/Tokyo",
31+
environments: ["PRODUCTION"], // Only run in production
32+
},
2933
run: async (payload) => {},
3034
});
3135

0 commit comments

Comments
 (0)