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

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

Closed
BenJackGill opened this issue Oct 7, 2024 · 2 comments · Fixed by #2726
Closed

Comments

@BenJackGill
Copy link

BenJackGill commented Oct 7, 2024

[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.

@aalej
Copy link

aalej commented Oct 7, 2024

Hey @BenJackGill, thanks for raising this and for creating a thorough report! This is appreciated. I’m able to reproduce the error using the mcve and steps you provided. Let me notify our engineering team about this.

@blidd-google blidd-google self-assigned this Oct 8, 2024
@blidd-google blidd-google transferred this issue from firebase/firebase-tools Oct 8, 2024
@google-oss-bot
Copy link

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants