-
Notifications
You must be signed in to change notification settings - Fork 417
feat(functions): Implement TaskQueue Scopes #3210
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
base: main
Are you sure you want to change the base?
Changes from all commits
3f12b70
8387aad
e7f717b
fa363cc
d36ba25
94f6d08
fb67f7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,9 +24,17 @@ import { FirebaseError, toHttpResponse } from '../utils/error'; | |
| import { FirebaseFunctionsError, FunctionsErrorCode, FUNCTIONS_ERROR_CODE_MAPPING } from './error'; | ||
| import * as utils from '../utils/index'; | ||
| import * as validator from '../utils/validator'; | ||
| import { TaskOptions } from './functions-api'; | ||
| import { TaskOptions, FunctionScope } from './functions-api'; | ||
| import { ApplicationDefaultCredential } from '../app/credential-internal'; | ||
|
|
||
| export type InternalFunctionScope = FunctionScope | { | ||
| scope: 'kit'; | ||
| instance: string; | ||
| } | { | ||
| scope: 'extensionOrKit'; | ||
| instance: string; | ||
| }; | ||
|
|
||
| const CLOUD_TASKS_API_RESOURCE_PATH = 'projects/{projectId}/locations/{locationId}/queues/{resourceId}/tasks'; | ||
| const CLOUD_TASKS_API_URL_FORMAT = 'https://cloudtasks.googleapis.com/v2/' + CLOUD_TASKS_API_RESOURCE_PATH; | ||
| const FIREBASE_FUNCTION_URL_FORMAT = 'https://{locationId}-{projectId}.cloudfunctions.net/{resourceId}'; | ||
|
|
@@ -66,9 +74,13 @@ export class FunctionsApiClient { | |
| * | ||
| * @param id - The ID of the task to delete. | ||
| * @param functionName - The function name of the queue. | ||
| * @param extensionId - Optional canonical ID of the extension. | ||
| * @param scope - Optional FunctionScope configuration. | ||
| */ | ||
| public async delete(id: string, functionName: string, extensionId?: string): Promise<void> { | ||
| public async delete( | ||
| id: string, | ||
| functionName: string, | ||
| scope?: InternalFunctionScope | ||
| ): Promise<void> { | ||
| if (!validator.isNonEmptyString(functionName)) { | ||
| throw new FirebaseFunctionsError({ | ||
| code: 'invalid-argument', | ||
|
|
@@ -101,13 +113,13 @@ export class FunctionsApiClient { | |
| message: 'No valid function name specified to enqueue tasks for.' | ||
| }); | ||
| } | ||
| if (typeof extensionId !== 'undefined' && validator.isNonEmptyString(extensionId)) { | ||
| resources.resourceId = `ext-${extensionId}-${resources.resourceId}`; | ||
| } | ||
|
|
||
| const { resourceId } = this.resolveResourceId(resources.resourceId, scope); | ||
| const targetResources = { ...resources, resourceId }; | ||
|
|
||
| try { | ||
| const serviceUrl = tasksEmulatorUrl(resources, this.emulatorHost)?.concat('/', id) | ||
| ?? await this.getUrl(resources, CLOUD_TASKS_API_URL_FORMAT.concat('/', id)); | ||
| const serviceUrl = tasksEmulatorUrl(targetResources, this.emulatorHost)?.concat('/', id) | ||
| ?? await this.getUrl(targetResources, CLOUD_TASKS_API_URL_FORMAT.concat('/', id)); | ||
| const request: HttpRequestConfig = { | ||
| method: 'DELETE', | ||
| url: serviceUrl, | ||
|
|
@@ -116,10 +128,6 @@ export class FunctionsApiClient { | |
| await this.httpClient.send(request); | ||
| } catch (err: unknown) { | ||
| if (err instanceof RequestResponseError) { | ||
| if (err.response.status === 404) { | ||
| // if no task with the provided ID exists, then ignore the delete. | ||
| return; | ||
| } | ||
| throw this.toFirebaseError(err); | ||
| } else { | ||
| throw err; | ||
|
|
@@ -132,10 +140,15 @@ export class FunctionsApiClient { | |
| * | ||
| * @param data - The data payload of the task. | ||
| * @param functionName - The functionName of the queue. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be either Better yet, more words might make it all more clear. Is it the name of the function associated with the queue, or some even better verb than "associate?" |
||
| * @param extensionId - Optional canonical ID of the extension. | ||
| * @param scope - Optional FunctionScope configuration. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above; literal? |
||
| * @param opts - Optional options when enqueuing a new task. | ||
| */ | ||
| public async enqueue(data: any, functionName: string, extensionId?: string, opts?: TaskOptions): Promise<void> { | ||
| public async enqueue( | ||
| data: any, | ||
| functionName: string, | ||
| scope?: InternalFunctionScope, | ||
| opts?: TaskOptions | ||
| ): Promise<void> { | ||
| if (!validator.isNonEmptyString(functionName)) { | ||
| throw new FirebaseFunctionsError({ | ||
| code: 'invalid-argument', | ||
|
|
@@ -161,17 +174,17 @@ export class FunctionsApiClient { | |
| message: 'No valid function name specified to enqueue tasks for.' | ||
| }); | ||
| } | ||
| if (typeof extensionId !== 'undefined' && validator.isNonEmptyString(extensionId)) { | ||
| resources.resourceId = `ext-${extensionId}-${resources.resourceId}`; | ||
| } | ||
|
|
||
| const task = this.validateTaskOptions(data, resources, opts); | ||
| const { resourceId, extensionOrKitId } = this.resolveResourceId(resources.resourceId, scope); | ||
| const targetResources = { ...resources, resourceId }; | ||
|
|
||
| try { | ||
| const task = this.validateTaskOptions(data, targetResources, opts); | ||
| const serviceUrl = | ||
| tasksEmulatorUrl(resources, this.emulatorHost) ?? | ||
| await this.getUrl(resources, CLOUD_TASKS_API_URL_FORMAT); | ||
| tasksEmulatorUrl(targetResources, this.emulatorHost) ?? | ||
| await this.getUrl(targetResources, CLOUD_TASKS_API_URL_FORMAT); | ||
|
|
||
| const taskPayload = await this.updateTaskPayload(task, resources, extensionId); | ||
| const taskPayload = await this.updateTaskPayload(task, targetResources, extensionOrKitId); | ||
| const request: HttpRequestConfig = { | ||
| method: 'POST', | ||
| url: serviceUrl, | ||
|
|
@@ -199,6 +212,51 @@ export class FunctionsApiClient { | |
| } | ||
| } | ||
|
|
||
| private resolveResourceId( | ||
| resourceId: string, | ||
| scope: InternalFunctionScope = { scope: 'current' } | ||
| ): { resourceId: string; extensionOrKitId?: string } { | ||
|
|
||
| switch (scope.scope) { | ||
| case 'current': { | ||
| const extInstanceId = process.env.EXT_INSTANCE_ID; | ||
| if (validator.isNonEmptyString(extInstanceId)) { | ||
| return { | ||
| resourceId: `ext-${extInstanceId}-${resourceId}`, | ||
| extensionOrKitId: extInstanceId, | ||
| }; | ||
| } | ||
| const kitInstanceId = process.env.FIREBASE_KIT_INSTANCE_ID; | ||
| if (validator.isNonEmptyString(kitInstanceId)) { | ||
| return { | ||
| resourceId: `kit-${kitInstanceId}-${resourceId}`, | ||
| extensionOrKitId: kitInstanceId, | ||
| }; | ||
| } | ||
| return { resourceId }; | ||
| } | ||
| case 'global': | ||
| return { resourceId }; | ||
| case 'extension': | ||
| return { | ||
| resourceId: `ext-${scope.instance}-${resourceId}`, | ||
| extensionOrKitId: scope.instance, | ||
| }; | ||
| case 'kit': // kit scope is secretly accepted for forward compatibility | ||
| return { | ||
| resourceId: `kit-${scope.instance}-${resourceId}`, | ||
| extensionOrKitId: scope.instance, | ||
| }; | ||
| case 'extensionOrKit': | ||
| return { | ||
| resourceId: `ext-${scope.instance}-${resourceId}`, | ||
| extensionOrKitId: scope.instance, | ||
| }; | ||
| default: | ||
| return { resourceId }; | ||
| } | ||
| } | ||
|
|
||
| private getUrl(resourceName: utils.ParsedResource, urlFormat: string): Promise<string> { | ||
| let { locationId } = resourceName; | ||
| const { projectId, resourceId } = resourceName; | ||
|
|
@@ -349,7 +407,11 @@ export class FunctionsApiClient { | |
| return task; | ||
| } | ||
|
|
||
| private async updateTaskPayload(task: Task, resources: utils.ParsedResource, extensionId?: string): Promise<Task> { | ||
| private async updateTaskPayload( | ||
| task: Task, | ||
| resources: utils.ParsedResource, | ||
| extensionOrKitId?: string | ||
| ): Promise<Task> { | ||
| const defaultUrl = this.emulatorHost ? | ||
| '' | ||
| : await this.getUrl(resources, FIREBASE_FUNCTION_URL_FORMAT); | ||
|
|
@@ -360,7 +422,7 @@ export class FunctionsApiClient { | |
|
|
||
| task.httpRequest.url = functionUrl; | ||
| // When run from a deployed extension, we should be using ComputeEngineCredentials | ||
| if (validator.isNonEmptyString(extensionId) && this.app.options.credential | ||
| if (validator.isNonEmptyString(extensionOrKitId) && this.app.options.credential | ||
| instanceof ApplicationDefaultCredential && await this.app.options.credential.isComputeEngineCredential()) { | ||
| const idToken = await this.app.options.credential.getIDToken(functionUrl); | ||
| task.httpRequest.headers = { ...task.httpRequest.headers, 'Authorization': `Bearer ${idToken}` }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we get a TW review on these new docstrings, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a literal,
FunctionScope. Though, would just "function scope" work as a common noun/general term?See bottom note here: https://docs.google.com/presentation/d/1Ezb4sp4XD6ItHISccnME-EVH_oJj59R5Naq0IskLrAo/edit?slide=id.g1cd7eabb964_0_10#slide=id.g1cd7eabb964_0_10