Skip to content

Cloud Tasks emulator does not accept all possible formats for getFunctions().taskQueue() #2725

@BenJackGill

Description

@BenJackGill

[REQUIRED] Environment info

firebase-tools: 13.20.2

Platform: macOS

[REQUIRED] Test case

Trying to use the new Cloud Tasks emulator.

The docs say that getFunctions().taskQueue(targetUri) should accept three formats:

  1. A fully qualified function resource name: projects/{project}/locations/{location}/functions/{functionName}
  2. A partial resource name with location and function name, in which case the runtime project ID is used: locations/{location}/functions/{functionName}
  3. A partial function name, in which case the runtime project ID and the default location, us-central1, is used: {functionName}

But I have found the Cloud Tasks emulator only works with format number 3 (functionName). All the other formats throw a 404 error.

Here is the example code:

import * as admin from "firebase-admin";
import { onRequest } from "firebase-functions/v2/https";
import { getFunctions } from "firebase-admin/functions";
import { onTaskDispatched } from "firebase-functions/v2/tasks";
import * as logger from "firebase-functions/logger";

// Initialize the Firebase app
admin.initializeApp();

// The task function
export const testOnRequest = onRequest(async (request, response) => {
  const taskPayload = {
    foo: "bar",
  };
  const targetUri = "testOnTaskDispatched"; // <-- This works :)
  // const targetUri = "locations/us-central1/functions/testOnTaskDispatched"; // <-- This does NOT work :(
  // const targetUri =
  //  "projects/demo-project/locations/us-central1/functions/testOnTaskDispatched"; // <-- This does NOT work :(
  const queue = getFunctions().taskQueue(targetUri);

  try {
    await queue.enqueue(taskPayload);
  } catch (error) {
    console.error("Error scheduling task", error);
    response.status(500).send("Error scheduling task");
    return;
  }
  response.send("Hello from HTTP ON REQUEST!");
});

// The http functions
export const testOnTaskDispatched = onTaskDispatched((request) => {
  logger.info("Hello logs from TASKS ON TASK DISPATCHED!", {
    foo: request.data,
  });
});

Here is the error that is thrown inside the emaultor:

Error scheduling task FirebaseFunctionsError: Unexpected response with status: 404 and body: <!DOCTYPE html>
10:55:50
I
function[us-central1-testOnRequest]
<html lang="en">
10:55:50
I
function[us-central1-testOnRequest]
<head>
10:55:50
I
function[us-central1-testOnRequest]
<meta charset="utf-8">
10:55:50
I
function[us-central1-testOnRequest]
<title>Error</title>
10:55:50
I
function[us-central1-testOnRequest]
</head>
10:55:50
I
function[us-central1-testOnRequest]
<body>
10:55:50
I
function[us-central1-testOnRequest]
<pre>Cannot POST /projects/demo-project/locations/us-central1/queues/locations/us-central1/functions/testOnTaskDispatched/tasks</pre>
10:55:50
I
function[us-central1-testOnRequest]
</body>
10:55:50
I
function[us-central1-testOnRequest]
</html>
10:55:50
I
function[us-central1-testOnRequest]
>  
10:55:50
I
function[us-central1-testOnRequest]
    at FunctionsApiClient.toFirebaseError (/Users/BenJackGill/Dev/fire/functions/node_modules/firebase-admin/lib/functions/functions-api-client-internal.js:313:20)
10:55:50
I
function[us-central1-testOnRequest]
    at FunctionsApiClient.enqueue (/Users/BenJackGill/Dev/fire/functions/node_modules/firebase-admin/lib/functions/functions-api-client-internal.js:149:32)
10:55:50
I
function[us-central1-testOnRequest]
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
10:55:50
I
function[us-central1-testOnRequest]
    at async /Users/BenJackGill/Dev/fire/functions/lib/index.js:22:9
10:55:50
I
function[us-central1-testOnRequest]
    at async runFunction (/Users/BenJackGill/.nvm/versions/node/v20.11.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:506:9)
10:55:50
I
function[us-central1-testOnRequest]
    at async runHTTPS (/Users/BenJackGill/.nvm/versions/node/v20.11.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:531:5)
10:55:50
I
function[us-central1-testOnRequest]
    at async /Users/BenJackGill/.nvm/versions/node/v20.11.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:694:21 {
10:55:50
I
function[us-central1-testOnRequest]
  errorInfo: {
10:55:50
I
function[us-central1-testOnRequest]
    code: 'functions/unknown-error',
10:55:50
I
function[us-central1-testOnRequest]
    message: 'Unexpected response with status: 404 and body: <!DOCTYPE html>\n' +
10:55:50
I
function[us-central1-testOnRequest]
      '<html lang="en">\n' +
10:55:50
I
function[us-central1-testOnRequest]
      '<head>\n' +
10:55:50
I
function[us-central1-testOnRequest]
      '<meta charset="utf-8">\n' +
10:55:50
I
function[us-central1-testOnRequest]
      '<title>Error</title>\n' +
10:55:50
I
function[us-central1-testOnRequest]
      '</head>\n' +
10:55:50
I
function[us-central1-testOnRequest]
      '<body>\n' +
10:55:50
I
function[us-central1-testOnRequest]
      '<pre>Cannot POST /projects/demo-project/locations/us-central1/queues/locations/us-central1/functions/testOnTaskDispatched/tasks</pre>\n' +
10:55:50
I
function[us-central1-testOnRequest]
      '</body>\n' +
10:55:50
I
function[us-central1-testOnRequest]
      '</html>\n'
10:55:50
I
function[us-central1-testOnRequest]
  },
10:55:50
I
function[us-central1-testOnRequest]
  codePrefix: 'functions'
10:55:50
I
function[us-central1-testOnRequest]
}

[REQUIRED] Steps to reproduce

  1. Download this repo: https://github.com/BenJackGill/firebase-tasks-uri-bug
  2. Change into the "functions" directory: cd functions
  3. Install the packages: npm i
  4. Run the emulators: npm run serve
  5. Visit the http on Request function, which in turn triggers the task function: http://127.0.0.1:5001/demo-project/us-central1/testOnRequest
  6. See the emulator logs for the error
  7. Check the comments in this file to see which uri versions work and which do not: https://github.com/BenJackGill/firebase-tasks-uri-bug/blob/main/functions/src/index.ts

[REQUIRED] Expected behavior

The Cloud Task emulator should run the task function without error.

[REQUIRED] Actual behavior

It produces a 404 error.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions