Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions spec/common/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ interface TaskTest {

// Runs a TaskTest test.
async function runTaskTest(test: TaskTest): Promise<any> {
const taskQueueFunctionV1 = https.onEnqueueHandler((data, context) => {
const taskQueueFunctionV1 = https.onDispatchHandler((data, context) => {
expect(data).to.deep.equal(test.expectedData);
if (test.taskFunction) {
test.taskFunction(data, context);
Expand All @@ -176,7 +176,7 @@ async function runTaskTest(test: TaskTest): Promise<any> {
const responseV1 = await runHandler(taskQueueFunctionV1, test.httpRequest);
expect(responseV1.status).to.equal(test.expectedStatus);

const taskQueueFunctionV2 = https.onEnqueueHandler((request) => {
const taskQueueFunctionV2 = https.onDispatchHandler((request) => {
expect(request.data).to.deep.equal(test.expectedData);
if (test.taskFunction2) {
test.taskFunction2(request);
Expand Down
8 changes: 4 additions & 4 deletions spec/v1/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('#onEnqueue', () => {
},
invoker: 'private',
})
.onEnqueue(() => {});
.onDispatch(() => {});
expect(result.__trigger).to.deep.equal({
taskQueueTrigger: {
rateLimits: {
Expand All @@ -252,7 +252,7 @@ describe('#onEnqueue', () => {
memory: '256MB',
})
.https.taskQueue({ retryConfig: { maxAttempts: 5 } })
.onEnqueue(() => null);
.onDispatch(() => null);

expect(fn.__trigger).to.deep.equal({
regions: ['us-east1'],
Expand All @@ -275,7 +275,7 @@ describe('#onEnqueue', () => {
},
};
let done = false;
const cf = https.taskQueue().onEnqueue((d, c) => {
const cf = https.taskQueue().onDispatch((d, c) => {
expect(d).to.equal(data);
expect(c).to.deep.equal(context);
done = true;
Expand All @@ -288,7 +288,7 @@ describe('#onEnqueue', () => {
// Regression test for firebase-functions#947
it('should lock to the v1 API even with function.length == 1', async () => {
let gotData: Record<string, any>;
const func = https.taskQueue().onEnqueue((data) => {
const func = https.taskQueue().onDispatch((data) => {
gotData = data;
});

Expand Down
18 changes: 9 additions & 9 deletions spec/v2/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ describe('onTaskEnqueue', () => {
});

it('should return a minimal trigger with appropriate values', () => {
const result = https.onTaskEnqueue(() => {});
const result = https.onTaskDispatched(() => {});
expect(result.__trigger).to.deep.equal({
apiVersion: 2,
platform: 'gcfv2',
Expand All @@ -388,7 +388,7 @@ describe('onTaskEnqueue', () => {
});

it('should create a complex trigger with appropriate values', () => {
const result = https.onTaskEnqueue(
const result = https.onTaskDispatched(
{
...FULL_OPTIONS,
retryConfig: {
Expand Down Expand Up @@ -432,7 +432,7 @@ describe('onTaskEnqueue', () => {
minInstances: 1,
});

const result = https.onTaskEnqueue(
const result = https.onTaskDispatched(
{
region: 'us-west1',
minInstances: 3,
Expand All @@ -459,7 +459,7 @@ describe('onTaskEnqueue', () => {
token: 'token',
},
};
const cf = https.onTaskEnqueue((r) => {
const cf = https.onTaskDispatched((r) => {
expect(r.data).to.deep.equal(request.data);
expect(r.auth).to.deep.equal(request.auth);
});
Expand All @@ -468,7 +468,7 @@ describe('onTaskEnqueue', () => {
});

it('should be an express handler', async () => {
const func = https.onTaskEnqueue((request) => {});
const func = https.onTaskDispatched((request) => {});

const req = new MockRequest(
{
Expand All @@ -487,17 +487,17 @@ describe('onTaskEnqueue', () => {

// These tests pass if the code transpiles
it('allows desirable syntax', () => {
https.onTaskEnqueue<string>((request: https.TaskRequest<string>) => {
https.onTaskDispatched<string>((request: https.TaskRequest<string>) => {
// There should be no lint warnings that data is not a string.
console.log(`hello, ${request.data}`);
});
https.onTaskEnqueue((request: https.TaskRequest<string>) => {
https.onTaskDispatched((request: https.TaskRequest<string>) => {
console.log(`hello, ${request.data}`);
});
https.onTaskEnqueue<string>((request: https.TaskRequest) => {
https.onTaskDispatched<string>((request: https.TaskRequest) => {
console.log(`hello, ${request.data}`);
});
https.onTaskEnqueue((request: https.TaskRequest) => {
https.onTaskDispatched((request: https.TaskRequest) => {
console.log(`Hello, ${request.data}`);
});
});
Expand Down
22 changes: 17 additions & 5 deletions src/common/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,28 @@ export interface CallableRequest<T = any> {

/** How a task should be retried in the event of a non-2xx return. */
export interface TaskRetryConfig {
// If left unspecified, will default to 3
/**
* Maximum number of times a request should be attempted.
* If left unspecified, will default to 3.
*/
maxAttempts?: number;

// If left unspecified will default to 1hr
/**
* The maximum amount of time to wait between attempts.
* If left unspecified will default to 1hr.
*/
maxBackoffSeconds?: number;

// If left unspecified will default to 16
/**
* The maximum number of times to double the backoff between
* retries. If left unspecified will default to 16.
*/
maxDoublings?: number;

// If left unspecified will default to 100ms
/**
* The minimum time to wait between attempts. If left unspecified
* will default to 100ms.
*/
minBackoffSeconds?: number;
}

Expand Down Expand Up @@ -818,7 +830,7 @@ function wrapOnCallHandler<Req = any, Res = any>(
}

/** @internal */
export function onEnqueueHandler<Req = any>(
export function onDispatchHandler<Req = any>(
handler: v1TaskHandler | v2TaskHandler<Req>
): (req: Request, res: express.Response) => Promise<void> {
return async (req: Request, res: express.Response): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion src/function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export class FunctionBuilder {

/**
* Declares a task queue function for clients to call using a Firebase Admin SDK.
* @param handler A method that takes a data and context and returns void or Promise<void>.
* @param options Configurations for the task queue function.
*/
/** @hidden */
taskQueue: (options?: https.TaskQueueOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion src/handler-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class HandlerBuilder {
) => void | Promise<void>
) {
const builder = new https.TaskQueueBuilder();
const func = builder.onEnqueue(handler);
const func = builder.onDispatch(handler);
func.__trigger = {};
return func;
},
Expand Down
6 changes: 3 additions & 3 deletions src/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
FunctionsErrorCode,
HttpsError,
onCallHandler,
onEnqueueHandler,
onDispatchHandler,
Request,
TaskContext,
TaskRateLimits,
Expand Down Expand Up @@ -107,15 +107,15 @@ export class TaskQueueBuilder {
private readonly depOpts?: DeploymentOptions
) {}

onEnqueue(
onDispatch(
handler: (data: any, context: TaskContext) => void | Promise<void>
): TaskQueueFunction {
// onEnqueueHandler sniffs the function length of the passed-in callback
// and the user could have only tried to listen to data. Wrap their handler
// in another handler to avoid accidentally triggering the v2 API
const fixedLen = (data: any, context: TaskContext) =>
handler(data, context);
const func: any = onEnqueueHandler(fixedLen);
const func: any = onDispatchHandler(fixedLen);

func.__trigger = {
...optionsToTrigger(this.depOpts || {}),
Expand Down
10 changes: 5 additions & 5 deletions src/v2/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
FunctionsErrorCode,
HttpsError,
onCallHandler,
onEnqueueHandler,
onDispatchHandler,
Request,
TaskRateLimits,
TaskRequest,
Expand Down Expand Up @@ -227,17 +227,17 @@ export function onCall<T = any, Return = any | Promise<any>>(
}

/** Handle a request sent to a Cloud Tasks queue. */
export function onTaskEnqueue<Args = any>(
export function onTaskDispatched<Args = any>(
handler: (request: TaskRequest<Args>) => void | Promise<void>
): TaskQueueFunction<Args>;

/** Handle a request sent to a Cloud Tasks queue. */
export function onTaskEnqueue<Args = any>(
export function onTaskDispatched<Args = any>(
options: TaskQueueOptions,
handler: (request: TaskRequest<Args>) => void | Promise<void>
): TaskQueueFunction<Args>;

export function onTaskEnqueue<Args = any>(
export function onTaskDispatched<Args = any>(
optsOrHandler:
| TaskQueueOptions
| ((request: TaskRequest<Args>) => void | Promise<void>),
Expand All @@ -256,7 +256,7 @@ export function onTaskEnqueue<Args = any>(
// onEnqueueHandler sniffs the function length to determine which API to present.
// fix the length to prevent api versions from being mismatched.
const fixedLen = (req: TaskRequest<Args>) => handler(req);
const func: any = onEnqueueHandler(fixedLen);
const func: any = onDispatchHandler(fixedLen);

Object.defineProperty(func, '__trigger', {
get: () => {
Expand Down