Add environment filter to declarative schedules (#2148)
This commit is contained in:
@@ -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 &&
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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"]
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
@@ -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">;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -58,6 +73,8 @@ 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",
|
||||
@@ -65,6 +82,7 @@ export function task<TIdentifier extends string, TOutput, TInitOutput extends In
|
||||
? {
|
||||
cron: cron,
|
||||
timezone,
|
||||
environments,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
@@ -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) => {},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user