* Executes a taskQueue lifecycle hook by enqueuing a task in Cloud Tasks.
(
taskHook: { function: string; body?: Record<string, unknown> },
wantBackend: backend.Backend,
)
| 89 | * Executes a taskQueue lifecycle hook by enqueuing a task in Cloud Tasks. |
| 90 | */ |
| 91 | async function executeTaskQueueHook( |
| 92 | taskHook: { function: string; body?: Record<string, unknown> }, |
| 93 | wantBackend: backend.Backend, |
| 94 | ): Promise<void> { |
| 95 | const targetEndpoint = findTargetEndpoint(wantBackend, taskHook.function); |
| 96 | if (!targetEndpoint) { |
| 97 | throw new FirebaseError( |
| 98 | `Target endpoint "${taskHook.function}" not found in backend for lifecycle hook.`, |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | if (!backend.isTaskQueueTriggered(targetEndpoint)) { |
| 103 | throw new FirebaseError(`Target endpoint "${taskHook.function}" is not a task queue function.`); |
| 104 | } |
| 105 | |
| 106 | const queueName = cloudtasks.queueNameForEndpoint(targetEndpoint); |
| 107 | const bodyStr = taskHook.body ? JSON.stringify(taskHook.body) : ""; |
| 108 | const body = bodyStr ? Buffer.from(bodyStr).toString("base64") : undefined; |
| 109 | |
| 110 | const url = targetEndpoint.uri; |
| 111 | if (!url) { |
| 112 | throw new FirebaseError(`Target endpoint "${taskHook.function}" does not have a trigger URI.`); |
| 113 | } |
| 114 | |
| 115 | const projectMetadata = await getProject(targetEndpoint.project); |
| 116 | const projectNumber = projectMetadata.projectNumber; |
| 117 | const sa = |
| 118 | targetEndpoint.serviceAccount || (await computeEngine.getDefaultServiceAccount(projectNumber)); |
| 119 | |
| 120 | const task: cloudtasks.Task = { |
| 121 | httpRequest: { |
| 122 | url, |
| 123 | httpMethod: "POST", |
| 124 | headers: { |
| 125 | "Content-Type": "application/json", |
| 126 | }, |
| 127 | oidcToken: { |
| 128 | serviceAccountEmail: sa, |
| 129 | audience: url, |
| 130 | }, |
| 131 | }, |
| 132 | }; |
| 133 | if (body) { |
| 134 | task.httpRequest.body = body; |
| 135 | } |
| 136 | |
| 137 | try { |
| 138 | await cloudtasks.enqueueTask(queueName, task); |
| 139 | logLabeledSuccess( |
| 140 | "functions", |
| 141 | `Successfully queued task for lifecycle hook ${taskHook.function} in queue ${queueName}.`, |
| 142 | ); |
| 143 | logLabeledBullet( |
| 144 | "functions", |
| 145 | `View logs for ${taskHook.function} at: ${getCloudConsoleLogUrl(targetEndpoint)}`, |
| 146 | ); |
| 147 | } catch (err: unknown) { |
| 148 | // We treat lifecycle hook failures as warnings. We don't want to fail |
no test coverage detected
searching dependent graphs…