Skip to content
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

feat(functions): Add features to task queue functions #2216

Merged
merged 13 commits into from
Jul 10, 2023

Conversation

blidd-google
Copy link
Contributor

@blidd-google blidd-google commented Jun 14, 2023

Since CF3 released support for Task Queue Functions at Google I/O 2022, users have made several feature requests. This PR enhances the Task Queue functions API to enable the following:

  • Headers passed to TaskQueue HTTPS handlers are now available to the context/event.
  • Users can name their tasks by including an id to TaskOptions when enqueueing tasks.
  • Users can cancel their tasks by providing the task id to the new delete API.

See firebase/firebase-functions#1423 for the sister changes in the functions SDK.

RELEASE NOTES: Headers passed to TaskQueue HTTPS handlers are now available in the context/event. Added the ability to name the tasks by including an id when enqueueing tasks. Added the ability to delete an enqueued task if it has not yet completed.

@@ -58,6 +60,38 @@ export type TaskOptions = DeliverySchedule & TaskOptionsExperimental & {
* The default is 10 minutes. The deadline must be in the range of 15 seconds and 30 minutes.
*/
dispatchDeadlineSeconds?: number;
/**
Copy link
Member

Choose a reason for hiding this comment

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

nit: newline between commented fields (e.g. here and headers)

});
}

for (const invalidOption of [null, 'abc', '', [], true, 102, 1.2]) {
it(`should throw if options is ${invalidOption}`, () => {
expect(() => apiClient.enqueue({}, FUNCTION_NAME, '', invalidOption as any))
.to.throw('TaskOptions must be a non-null object');
expect(apiClient.enqueue({}, FUNCTION_NAME, '', invalidOption as any))
Copy link
Member

Choose a reason for hiding this comment

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

Here and elsewhere, you need to make the test case async and then await this expectation or no check will actually be performed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm I deliberately chose to use chai's eventually to wait on the Promise returned by enqueue calls instead of async/await, to align better with the existing implementation which generally avoids async/await. Should I go ahead and refactor the unit tests to use async/await instead?

Copy link
Member

Choose a reason for hiding this comment

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

Since we don't make any changes to the package.json in this PR, let's revert the lock file changes and let dependabot take care of the package-lock.json updates.

@lahirumaramba lahirumaramba changed the title Add features to task queue functions feat(functions): Add features to task queue functions Jun 27, 2023
Copy link
Member

@lahirumaramba lahirumaramba left a comment

Choose a reason for hiding this comment

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

Thank you for the PR!
Please get a TW review on the documentation changes before merging this.

src/utils/validator.ts Outdated Show resolved Hide resolved
src/functions/functions-api.ts Show resolved Hide resolved
Copy link
Contributor

@egilmorez egilmorez left a comment

Choose a reason for hiding this comment

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

I got a certain way through this and GH told me my diff was outdated . . . submitting these comments so far and will look again, thanks!

src/functions/functions-api-client-internal.ts Outdated Show resolved Hide resolved
src/functions/functions-api.ts Outdated Show resolved Hide resolved
src/functions/functions-api.ts Outdated Show resolved Hide resolved
* If not provided, one will be automatically generated.
* If provided explicitly specifying a task ID enables task de-duplication. If a task's ID is
* identical to that of an existing task or a task that was deleted or executed recently then
* the call will throw a TaskAlreadyExists error. Another task with the same id can't be
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest TaskAlreadyExists and capped "ID" in the next line.

Copy link
Contributor Author

@blidd-google blidd-google Jun 29, 2023

Choose a reason for hiding this comment

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

Actually wanted to get your thoughts on how to document this — instead of throwing a TaskAlreadyExists error, we changed the behavior to throw a FirebaseFunctionsError with code "functions/task-already-exists". So if a user wanted to check if a thrown error is due to the task already existing, they would check like so:

try {
  await queue.enqueue(...);
} catch (err: unknown) {
  if (err.code === "functions/task-already-exists") {
    ...
  }
}

Is this comment in the functions API sufficient to document this behavior?

src/functions/functions-api.ts Outdated Show resolved Hide resolved
src/functions/functions-api.ts Outdated Show resolved Hide resolved
Copy link
Member

@lahirumaramba lahirumaramba left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you!

@blidd-google blidd-google merged commit b9eaae6 into master Jul 10, 2023
lahirumaramba added a commit that referenced this pull request Jul 12, 2023
lahirumaramba added a commit that referenced this pull request Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants