Skip to content

Add environment filter to declarative schedules #2148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/webapp/app/v3/services/createBackgroundWorker.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,18 @@ export async function syncDeclarativeSchedules(
for (const task of tasksWithDeclarativeSchedules) {
if (task.schedule === undefined) continue;

// Check if this schedule should be created in the current environment
if (task.schedule.environments && task.schedule.environments.length > 0) {
if (!task.schedule.environments.includes(environment.type)) {
logger.debug("Skipping schedule creation due to environment filter", {
taskId: task.id,
environmentType: environment.type,
allowedEnvironments: task.schedule.environments,
});
continue;
}
}

const existingSchedule = existingDeclarativeSchedules.find(
(schedule) =>
schedule.taskIdentifier === task.id &&
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/v3/schemas/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export type QueueManifest = z.infer<typeof QueueManifest>;
export const ScheduleMetadata = z.object({
cron: z.string(),
timezone: z.string(),
environments: z.array(EnvironmentType).optional(),
});

const taskMetadata = {
Expand Down
22 changes: 20 additions & 2 deletions packages/trigger-sdk/src/v3/schedules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export type ScheduleOptions<
* "0 0 * * *"
* ```
*
* 2. Or an object with a pattern and an optional timezone (default is "UTC")
* 2. Or an object with a pattern, optional timezone, and optional environments
* ```ts
* {
* pattern: "0 0 * * *",
* timezone: "America/Los_Angeles"
* timezone: "America/Los_Angeles",
* environments: ["PRODUCTION", "STAGING"]
* }
* ```
*
Expand All @@ -43,6 +44,20 @@ export type ScheduleOptions<
| {
pattern: string;
timezone?: string;
/** You can optionally specify which environments this schedule should run in.
* When not specified, the schedule will run in all environments.
*
* @example
* ```ts
* environments: ["PRODUCTION", "STAGING"]
* ```
*
* @example
* ```ts
* environments: ["PRODUCTION"] // Only run in production
* ```
*/
environments?: Array<"DEVELOPMENT" | "STAGING" | "PRODUCTION" | "PREVIEW">;
};
};

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

resourceCatalog.updateTaskMetadata(task.id, {
triggerSource: "schedule",
schedule: cron
? {
cron: cron,
timezone,
environments,
}
: undefined,
});
Expand Down
16 changes: 10 additions & 6 deletions references/v3-catalog/src/trigger/scheduled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { logger, schedules, task } from "@trigger.dev/sdk/v3";

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

export const secondScheduledTask = schedules.task({
id: "second-scheduled-task",
// cron: {
// pattern: "0 5 * * *",
// timezone: "Asia/Tokyo",
// },
cron: {
pattern: "0 5 * * *",
timezone: "Asia/Tokyo",
environments: ["PRODUCTION"], // Only run in production
},
run: async (payload) => {},
});

Expand Down