diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a14a3c87f2..398cb139bd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ - Improve performance and reliability when deploying multiple 2nd gen functions using single builds. (#6376) - Fixed an issue where `emulators:export` did not check if the target folder is empty. (#6313) +- Fixed an issue where retry could not be set for event triggered functions. (#6391) - Fixed "Could not find the next executable" on Next.js deployments (#6372) - Fixed issues caused by breaking changes in Next >=v13.5.0. (#6382) diff --git a/src/gcp/cloudfunctionsv2.ts b/src/gcp/cloudfunctionsv2.ts index 8098e3d9794..4f4912c0426 100644 --- a/src/gcp/cloudfunctionsv2.ts +++ b/src/gcp/cloudfunctionsv2.ts @@ -36,6 +36,12 @@ export type FunctionState = "ACTIVE" | "FAILED" | "DEPLOYING" | "DELETING" | "UN // Values allowed for the operator field in EventFilter export type EventFilterOperator = "match-path-pattern"; +// Values allowed for the event trigger retry policy in case of a function's execution failure. +export type RetryPolicy = + | "RETRY_POLICY_UNSPECIFIED" + | "RETRY_POLICY_DO_NOT_RETRY" + | "RETRY_POLICY_RETRY"; + /** Settings for building a container out of the customer source. */ export interface BuildConfig { runtime: runtimes.Runtime; @@ -140,6 +146,8 @@ export interface EventTrigger { // to the defualt compute service account. serviceAccountEmail?: string; + retryPolicy?: RetryPolicy; + // The name of the channel associated with the trigger in // `projects/{project}/locations/{location}/channels/{channel}` format. channel?: string; @@ -542,6 +550,7 @@ export function functionFromEndpoint(endpoint: backend.Endpoint): InputCloudFunc if (backend.isEventTriggered(endpoint)) { gcfFunction.eventTrigger = { eventType: endpoint.eventTrigger.eventType, + retryPolicy: "RETRY_POLICY_UNSPECIFIED", }; if (gcfFunction.eventTrigger.eventType === PUBSUB_PUBLISH_EVENT) { if (!endpoint.eventTrigger.eventFilters?.topic) { @@ -579,9 +588,10 @@ export function functionFromEndpoint(endpoint: backend.Endpoint): InputCloudFunc ); proto.copyIfPresent(gcfFunction.eventTrigger, endpoint.eventTrigger, "channel"); - if (endpoint.eventTrigger.retry) { - logger.warn("Cannot set a retry policy on Cloud Function", endpoint.id); - } + endpoint.eventTrigger.retry + ? (gcfFunction.eventTrigger.retryPolicy = "RETRY_POLICY_RETRY") + : (gcfFunction.eventTrigger!.retryPolicy = "RETRY_POLICY_DO_NOT_RETRY"); + // By default, Functions Framework in GCFv2 opts to downcast incoming cloudevent messages to legacy formats. // Since Firebase Functions SDK expects messages in cloudevent format, we set FUNCTION_SIGNATURE_TYPE to tell // Functions Framework to disable downcast before passing the cloudevent message to function handler. @@ -665,7 +675,7 @@ export function endpointFromFunction(gcfFunction: OutputCloudFunction): backend. trigger = { eventTrigger: { eventType: gcfFunction.eventTrigger.eventType, - retry: false, + retry: gcfFunction.eventTrigger.retryPolicy === "RETRY_POLICY_RETRY" ? true : false, }, }; if (Object.keys(eventFilters).length) { diff --git a/src/test/gcp/cloudfunctionsv2.spec.ts b/src/test/gcp/cloudfunctionsv2.spec.ts index ca7092fed40..ce9724d8969 100644 --- a/src/test/gcp/cloudfunctionsv2.spec.ts +++ b/src/test/gcp/cloudfunctionsv2.spec.ts @@ -108,7 +108,7 @@ describe("cloudfunctionsv2", () => { resource: "projects/p/regions/r/instances/i", serviceName: "compute.googleapis.com", }, - retry: false, + retry: true, channel: "projects/myproject/locations/us-wildwest11/channels/mychannel", }, }; @@ -126,6 +126,7 @@ describe("cloudfunctionsv2", () => { value: "compute.googleapis.com", }, ], + retryPolicy: "RETRY_POLICY_RETRY", channel: "projects/myproject/locations/us-wildwest11/channels/mychannel", }, serviceConfig: { @@ -165,6 +166,7 @@ describe("cloudfunctionsv2", () => { operator: "match-path-pattern", }, ], + retryPolicy: "RETRY_POLICY_DO_NOT_RETRY", }, serviceConfig: { ...CLOUD_FUNCTION_V2.serviceConfig, @@ -302,6 +304,7 @@ describe("cloudfunctionsv2", () => { value: "pubsub.googleapis.com", }, ], + retryPolicy: "RETRY_POLICY_DO_NOT_RETRY", }, serviceConfig: { ...CLOUD_FUNCTION_V2.serviceConfig,