Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -265,6 +265,7 @@ describe("initScheduleTrigger", () => {
maxRetryDuration: RESET_VALUE,
minBackoffDuration: RESET_VALUE,
maxBackoffDuration: RESET_VALUE,
attemptDeadline: RESET_VALUE,
},
});
});
Expand Down Expand Up @@ -292,6 +293,7 @@ describe("initScheduleTrigger", () => {
maxRetrySeconds: RESET_VALUE,
minBackoffSeconds: RESET_VALUE,
maxBackoffSeconds: RESET_VALUE,
attemptDeadline: 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 @@ -212,6 +212,7 @@ describe("makeCloudFunction", () => {
maxDoublings: RESET_VALUE,
maxRetryDuration: RESET_VALUE,
minBackoffDuration: RESET_VALUE,
attemptDeadline: RESET_VALUE,
},
},
labels: {},
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 @@ -46,5 +46,6 @@ export const MINIMAL_SCHEDULE_TRIGGER: ManifestEndpoint["scheduleTrigger"] = {
maxBackoffDuration: options.RESET_VALUE,
minBackoffDuration: options.RESET_VALUE,
maxDoublings: options.RESET_VALUE,
attemptDeadline: options.RESET_VALUE,
},
};
20 changes: 16 additions & 4 deletions spec/v1/providers/pubsub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ describe("Pubsub Functions", () => {
expect(result.__endpoint.scheduleTrigger).to.deep.equal({
...MINIMAL_SCHEDULE_TRIGGER,
schedule: "every 5 minutes",
retryConfig,
retryConfig: {
...retryConfig,
attemptDeadline: RESET_VALUE,
},
});
expect(result.__endpoint.labels).to.be.empty;
});
Expand Down Expand Up @@ -249,7 +252,10 @@ describe("Pubsub Functions", () => {
expect(result.__endpoint.scheduleTrigger).to.deep.equal({
...MINIMAL_SCHEDULE_TRIGGER,
schedule: "every 5 minutes",
retryConfig,
retryConfig: {
...retryConfig,
attemptDeadline: RESET_VALUE,
},
timeZone: "America/New_York",
});
expect(result.__endpoint.labels).to.be.empty;
Expand Down Expand Up @@ -341,7 +347,10 @@ describe("Pubsub Functions", () => {
...MINIMAL_SCHEDULE_TRIGGER,
schedule: "every 5 minutes",
timeZone: RESET_VALUE,
retryConfig,
retryConfig: {
...retryConfig,
attemptDeadline: RESET_VALUE,
},
});
expect(result.__endpoint.region).to.deep.equal(["us-east1"]);
expect(result.__endpoint.availableMemoryMb).to.deep.equal(256);
Expand Down Expand Up @@ -382,7 +391,10 @@ describe("Pubsub Functions", () => {
...MINIMAL_SCHEDULE_TRIGGER,
schedule: "every 5 minutes",
timeZone: "America/New_York",
retryConfig,
retryConfig: {
...retryConfig,
attemptDeadline: RESET_VALUE,
},
});
expect(result.__endpoint.region).to.deep.equal(["us-east1"]);
expect(result.__endpoint.availableMemoryMb).to.deep.equal(256);
Expand Down
16 changes: 16 additions & 0 deletions spec/v2/providers/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const MINIMAL_SCHEDULE_TRIGGER: ManifestEndpoint["scheduleTrigger"] = {
minBackoffSeconds: options.RESET_VALUE,
maxBackoffSeconds: options.RESET_VALUE,
maxDoublings: options.RESET_VALUE,
attemptDeadline: options.RESET_VALUE,
},
};

Expand Down Expand Up @@ -103,6 +104,20 @@ describe("schedule", () => {
]);
});

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

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

it("should create a schedule function given options", () => {
const schfn = schedule.onSchedule(
{
Expand Down Expand Up @@ -133,6 +148,7 @@ describe("schedule", () => {
minBackoffSeconds: 11,
maxBackoffSeconds: 12,
maxDoublings: 2,
attemptDeadline: options.RESET_VALUE,
},
},
});
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface ManifestEndpoint {
maxRetryDuration?: string | Expression<string> | ResetValue;
minBackoffDuration?: string | Expression<string> | ResetValue;
maxBackoffDuration?: string | Expression<string> | ResetValue;
attemptDeadline?: string | Expression<string> | ResetValue;
};
};

Expand Down Expand Up @@ -254,6 +255,7 @@ const RESETTABLE_V1_SCHEDULE_OPTIONS: Omit<
maxRetryDuration: null,
maxBackoffDuration: null,
minBackoffDuration: null,
attemptDeadline: null,
};

const RESETTABLE_V2_SCHEDULE_OPTIONS: Omit<
Expand All @@ -265,6 +267,7 @@ const RESETTABLE_V2_SCHEDULE_OPTIONS: Omit<
maxRetrySeconds: null,
minBackoffSeconds: null,
maxBackoffSeconds: null,
attemptDeadline: null,
};

function initScheduleTrigger(
Expand Down
3 changes: 2 additions & 1 deletion src/v1/cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ export function makeCloudFunction<EventData>({
"maxDoublings",
"maxBackoffDuration",
"maxRetryDuration",
"minBackoffDuration"
"minBackoffDuration",
"attemptDeadline"
);
} 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
30 changes: 22 additions & 8 deletions src/v2/providers/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface SeparatedOpts {
minBackoffSeconds?: number | Expression<number> | ResetValue;
maxBackoffSeconds?: number | Expression<number> | ResetValue;
maxDoublings?: number | Expression<number> | ResetValue;
attemptDeadline?: string | Expression<string> | ResetValue;
};
opts: options.GlobalOptions;
}
Expand All @@ -60,16 +61,22 @@ export function getOpts(args: string | ScheduleOptions): SeparatedOpts {
opts: {} as options.GlobalOptions,
};
}
const retryConfig: any = {
retryCount: args.retryCount,
maxRetrySeconds: args.maxRetrySeconds,
minBackoffSeconds: args.minBackoffSeconds,
maxBackoffSeconds: args.maxBackoffSeconds,
maxDoublings: args.maxDoublings,
};

if (args.attemptDeadline !== undefined) {
retryConfig.attemptDeadline = args.attemptDeadline;
}

return {
schedule: args.schedule,
timeZone: args.timeZone,
retryConfig: {
retryCount: args.retryCount,
maxRetrySeconds: args.maxRetrySeconds,
minBackoffSeconds: args.minBackoffSeconds,
maxBackoffSeconds: args.maxBackoffSeconds,
maxDoublings: args.maxDoublings,
},
retryConfig,
opts: args as options.GlobalOptions,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for constructing retryConfig can be simplified. The current implementation uses a temporary variable with type any and an if condition to add attemptDeadline.

Since copyIfPresent is used later in onSchedule and it already handles undefined values, you can simplify this by directly constructing the retryConfig object in the return statement. This will make the code more concise and improve type safety by avoiding any.

  return {
    schedule: args.schedule,
    timeZone: args.timeZone,
    retryConfig: {
      retryCount: args.retryCount,
      maxRetrySeconds: args.maxRetrySeconds,
      minBackoffSeconds: args.minBackoffSeconds,
      maxBackoffSeconds: args.maxBackoffSeconds,
      maxDoublings: args.maxDoublings,
      attemptDeadline: args.attemptDeadline,
    },
    opts: args as options.GlobalOptions,
  };

}
Expand Down Expand Up @@ -125,6 +132,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 @@ -204,7 +217,8 @@ export function onSchedule(
"maxRetrySeconds",
"minBackoffSeconds",
"maxBackoffSeconds",
"maxDoublings"
"maxDoublings",
"attemptDeadline"
);
func.__endpoint = ep;

Expand Down