Skip to content

feat(scheduler): add support for attemptDeadline in onSchedule #1723

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions spec/runtime/manifest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ describe("initScheduleTrigger", () => {
expect(st).to.deep.eq({
schedule: "every 30 minutes",
timeZone: RESET_VALUE,
attemptDeadline: RESET_VALUE,
retryConfig: {
retryCount: RESET_VALUE,
maxDoublings: RESET_VALUE,
Expand Down Expand Up @@ -286,6 +287,7 @@ describe("initScheduleTrigger", () => {
expect(st).to.deep.eq({
schedule: "every 30 minutes",
timeZone: RESET_VALUE,
attemptDeadline: RESET_VALUE,
retryConfig: {
retryCount: RESET_VALUE,
maxDoublings: RESET_VALUE,
Expand Down
1 change: 1 addition & 0 deletions spec/v1/cloud-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ describe("makeCloudFunction", () => {
platform: "gcfv1",
scheduleTrigger: {
...schedule,
attemptDeadline: RESET_VALUE,
retryConfig: {
...schedule.retryConfig,
maxBackoffDuration: RESET_VALUE,
Expand Down
1 change: 1 addition & 0 deletions spec/v1/providers/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ export const MINIMAL_SCHEDULE_TRIGGER: ManifestEndpoint["scheduleTrigger"] = {
minBackoffDuration: options.RESET_VALUE,
maxDoublings: options.RESET_VALUE,
},
attemptDeadline: options.RESET_VALUE,
};
16 changes: 12 additions & 4 deletions spec/v1/providers/pubsub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ describe("Pubsub Functions", () => {
expect(result.__endpoint.scheduleTrigger).to.deep.equal({
...MINIMAL_SCHEDULE_TRIGGER,
schedule: "every 5 minutes",
retryConfig,
retryConfig: {
...retryConfig,
},
});
expect(result.__endpoint.labels).to.be.empty;
});
Expand Down Expand Up @@ -249,7 +251,9 @@ describe("Pubsub Functions", () => {
expect(result.__endpoint.scheduleTrigger).to.deep.equal({
...MINIMAL_SCHEDULE_TRIGGER,
schedule: "every 5 minutes",
retryConfig,
retryConfig: {
...retryConfig,
},
timeZone: "America/New_York",
});
expect(result.__endpoint.labels).to.be.empty;
Expand Down Expand Up @@ -341,7 +345,9 @@ describe("Pubsub Functions", () => {
...MINIMAL_SCHEDULE_TRIGGER,
schedule: "every 5 minutes",
timeZone: RESET_VALUE,
retryConfig,
retryConfig: {
...retryConfig,
},
});
expect(result.__endpoint.region).to.deep.equal(["us-east1"]);
expect(result.__endpoint.availableMemoryMb).to.deep.equal(256);
Expand Down Expand Up @@ -382,7 +388,9 @@ describe("Pubsub Functions", () => {
...MINIMAL_SCHEDULE_TRIGGER,
schedule: "every 5 minutes",
timeZone: "America/New_York",
retryConfig,
retryConfig: {
...retryConfig,
},
});
expect(result.__endpoint.region).to.deep.equal(["us-east1"]);
expect(result.__endpoint.availableMemoryMb).to.deep.equal(256);
Expand Down
20 changes: 20 additions & 0 deletions spec/v2/providers/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const MINIMAL_SCHEDULE_TRIGGER: ManifestEndpoint["scheduleTrigger"] = {
maxBackoffSeconds: options.RESET_VALUE,
maxDoublings: options.RESET_VALUE,
},
attemptDeadline: options.RESET_VALUE,
};

describe("schedule", () => {
Expand All @@ -59,6 +60,7 @@ describe("schedule", () => {
minBackoffSeconds: 2,
maxBackoffSeconds: 3,
maxDoublings: 4,
attemptDeadline: "120s",
memory: "128MiB",
region: "us-central1",
};
Expand All @@ -73,6 +75,7 @@ describe("schedule", () => {
maxBackoffSeconds: 3,
maxDoublings: 4,
},
attemptDeadline: "120s",
opts: {
...options,
memory: "128MiB",
Expand Down Expand Up @@ -103,6 +106,20 @@ describe("schedule", () => {
]);
});

it("should create a schedule function with attemptDeadline", () => {
const schfn = schedule.onSchedule(
{
schedule: "* * * * *",
attemptDeadline: "320s",
},
() => undefined
);

expect(schfn.__endpoint.scheduleTrigger?.attemptDeadline).to.equal(
"320s"
);
});

it("should create a schedule function given options", () => {
const schfn = schedule.onSchedule(
{
Expand All @@ -113,6 +130,7 @@ describe("schedule", () => {
minBackoffSeconds: 11,
maxBackoffSeconds: 12,
maxDoublings: 2,
attemptDeadline: "120s",
region: "us-central1",
labels: { key: "val" },
},
Expand All @@ -134,6 +152,7 @@ describe("schedule", () => {
maxBackoffSeconds: 12,
maxDoublings: 2,
},
attemptDeadline: "120s",
},
});
expect(schfn.__requiredAPIs).to.deep.eq([
Expand All @@ -159,6 +178,7 @@ describe("schedule", () => {
scheduleTrigger: {
schedule: "* * * * *",
timeZone: undefined,
attemptDeadline: undefined,
retryConfig: {
retryCount: undefined,
maxRetrySeconds: undefined,
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface ManifestEndpoint {
minBackoffDuration?: string | Expression<string> | ResetValue;
maxBackoffDuration?: string | Expression<string> | ResetValue;
};
attemptDeadline?: string | Expression<string> | ResetValue;
};

blockingTrigger?: {
Expand Down Expand Up @@ -280,7 +281,7 @@ function initScheduleTrigger(
for (const key of Object.keys(resetOptions)) {
scheduleTrigger.retryConfig[key] = RESET_VALUE;
}
scheduleTrigger = { ...scheduleTrigger, timeZone: RESET_VALUE };
scheduleTrigger = { ...scheduleTrigger, timeZone: RESET_VALUE, attemptDeadline: RESET_VALUE };
}
return scheduleTrigger;
}
Expand Down
2 changes: 1 addition & 1 deletion src/v1/cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ export function makeCloudFunction<EventData>({
"maxDoublings",
"maxBackoffDuration",
"maxRetryDuration",
"minBackoffDuration"
"minBackoffDuration",
);
} else {
endpoint.eventTrigger = {
Expand Down
5 changes: 5 additions & 0 deletions src/v1/function-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export interface ScheduleRetryConfig {
* @defaultValue 5
*/
maxDoublings?: number | Expression<number> | ResetValue;

/**
* The deadline for each job attempt, specified as a duration string (e.g. "600s").
*/
attemptDeadline?: string | Expression<string> | ResetValue;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/v2/providers/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ interface SeparatedOpts {
maxBackoffSeconds?: number | Expression<number> | ResetValue;
maxDoublings?: number | Expression<number> | ResetValue;
};
attemptDeadline?: string | Expression<string> | ResetValue;
opts: options.GlobalOptions;
}

Expand All @@ -60,6 +61,7 @@ export function getOpts(args: string | ScheduleOptions): SeparatedOpts {
opts: {} as options.GlobalOptions,
};
}

return {
schedule: args.schedule,
timeZone: args.timeZone,
Expand All @@ -70,6 +72,7 @@ export function getOpts(args: string | ScheduleOptions): SeparatedOpts {
maxBackoffSeconds: args.maxBackoffSeconds,
maxDoublings: args.maxDoublings,
},
attemptDeadline: args.attemptDeadline,
opts: args as options.GlobalOptions,
};
}
Expand Down Expand Up @@ -125,6 +128,12 @@ export interface ScheduleOptions extends options.GlobalOptions {

/** The time between will double max doublings times. */
maxDoublings?: number | Expression<number> | ResetValue;

/**
* The deadline for each job attempt, specified as a duration string (e.g. "600s").
* See: https://cloud.google.com/scheduler/docs/reference/rest/v1/projects.locations.jobs#Job
*/
attemptDeadline?: string | Expression<string> | ResetValue;
}

/**
Expand Down Expand Up @@ -197,14 +206,15 @@ export function onSchedule(
};

copyIfPresent(ep.scheduleTrigger, separatedOpts, "timeZone");
copyIfPresent(ep.scheduleTrigger, separatedOpts, "attemptDeadline");
copyIfPresent(
ep.scheduleTrigger.retryConfig,
separatedOpts.retryConfig,
"retryCount",
"maxRetrySeconds",
"minBackoffSeconds",
"maxBackoffSeconds",
"maxDoublings"
"maxDoublings",
);
func.__endpoint = ep;

Expand Down