Skip to content

Commit fb3b0a7

Browse files
colerogersjoehan
andauthored
Fix retry for event triggered functions (#6391)
* linking up retry to the api call * add changelog entry --------- Co-authored-by: joehan <[email protected]>
1 parent c94dca4 commit fb3b0a7

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- Improve performance and reliability when deploying multiple 2nd gen functions using single builds. (#6376)
22
- Fixed an issue where `emulators:export` did not check if the target folder is empty. (#6313)
3+
- Fixed an issue where retry could not be set for event triggered functions. (#6391)
34
- Fixed "Could not find the next executable" on Next.js deployments (#6372)
45
- Fixed issues caused by breaking changes in Next >=v13.5.0. (#6382)

src/gcp/cloudfunctionsv2.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export type FunctionState = "ACTIVE" | "FAILED" | "DEPLOYING" | "DELETING" | "UN
3636
// Values allowed for the operator field in EventFilter
3737
export type EventFilterOperator = "match-path-pattern";
3838

39+
// Values allowed for the event trigger retry policy in case of a function's execution failure.
40+
export type RetryPolicy =
41+
| "RETRY_POLICY_UNSPECIFIED"
42+
| "RETRY_POLICY_DO_NOT_RETRY"
43+
| "RETRY_POLICY_RETRY";
44+
3945
/** Settings for building a container out of the customer source. */
4046
export interface BuildConfig {
4147
runtime: runtimes.Runtime;
@@ -140,6 +146,8 @@ export interface EventTrigger {
140146
// to the defualt compute service account.
141147
serviceAccountEmail?: string;
142148

149+
retryPolicy?: RetryPolicy;
150+
143151
// The name of the channel associated with the trigger in
144152
// `projects/{project}/locations/{location}/channels/{channel}` format.
145153
channel?: string;
@@ -542,6 +550,7 @@ export function functionFromEndpoint(endpoint: backend.Endpoint): InputCloudFunc
542550
if (backend.isEventTriggered(endpoint)) {
543551
gcfFunction.eventTrigger = {
544552
eventType: endpoint.eventTrigger.eventType,
553+
retryPolicy: "RETRY_POLICY_UNSPECIFIED",
545554
};
546555
if (gcfFunction.eventTrigger.eventType === PUBSUB_PUBLISH_EVENT) {
547556
if (!endpoint.eventTrigger.eventFilters?.topic) {
@@ -579,9 +588,10 @@ export function functionFromEndpoint(endpoint: backend.Endpoint): InputCloudFunc
579588
);
580589
proto.copyIfPresent(gcfFunction.eventTrigger, endpoint.eventTrigger, "channel");
581590

582-
if (endpoint.eventTrigger.retry) {
583-
logger.warn("Cannot set a retry policy on Cloud Function", endpoint.id);
584-
}
591+
endpoint.eventTrigger.retry
592+
? (gcfFunction.eventTrigger.retryPolicy = "RETRY_POLICY_RETRY")
593+
: (gcfFunction.eventTrigger!.retryPolicy = "RETRY_POLICY_DO_NOT_RETRY");
594+
585595
// By default, Functions Framework in GCFv2 opts to downcast incoming cloudevent messages to legacy formats.
586596
// Since Firebase Functions SDK expects messages in cloudevent format, we set FUNCTION_SIGNATURE_TYPE to tell
587597
// Functions Framework to disable downcast before passing the cloudevent message to function handler.
@@ -665,7 +675,7 @@ export function endpointFromFunction(gcfFunction: OutputCloudFunction): backend.
665675
trigger = {
666676
eventTrigger: {
667677
eventType: gcfFunction.eventTrigger.eventType,
668-
retry: false,
678+
retry: gcfFunction.eventTrigger.retryPolicy === "RETRY_POLICY_RETRY" ? true : false,
669679
},
670680
};
671681
if (Object.keys(eventFilters).length) {

src/test/gcp/cloudfunctionsv2.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe("cloudfunctionsv2", () => {
108108
resource: "projects/p/regions/r/instances/i",
109109
serviceName: "compute.googleapis.com",
110110
},
111-
retry: false,
111+
retry: true,
112112
channel: "projects/myproject/locations/us-wildwest11/channels/mychannel",
113113
},
114114
};
@@ -126,6 +126,7 @@ describe("cloudfunctionsv2", () => {
126126
value: "compute.googleapis.com",
127127
},
128128
],
129+
retryPolicy: "RETRY_POLICY_RETRY",
129130
channel: "projects/myproject/locations/us-wildwest11/channels/mychannel",
130131
},
131132
serviceConfig: {
@@ -165,6 +166,7 @@ describe("cloudfunctionsv2", () => {
165166
operator: "match-path-pattern",
166167
},
167168
],
169+
retryPolicy: "RETRY_POLICY_DO_NOT_RETRY",
168170
},
169171
serviceConfig: {
170172
...CLOUD_FUNCTION_V2.serviceConfig,
@@ -302,6 +304,7 @@ describe("cloudfunctionsv2", () => {
302304
value: "pubsub.googleapis.com",
303305
},
304306
],
307+
retryPolicy: "RETRY_POLICY_DO_NOT_RETRY",
305308
},
306309
serviceConfig: {
307310
...CLOUD_FUNCTION_V2.serviceConfig,

0 commit comments

Comments
 (0)