sissonb
sissonb

Reputation: 3780

Schedule Google Cloud Tasks to Invoke Firebase Function

I am trying to setup a cloud task that calls a cloud function. I am able to setup the cloud task and I have setup some cloud functions, but I can't seem to get the cloud task to invoke the cloud function.

I am creating the task from my localhost by using this code

const serviceAccount = require('./serviceAccount.json');
const {CloudTasksClient} = require('@google-cloud/tasks');
const client = new CloudTasksClient({
    credentials: serviceAccount
});
const parent = client.queuePath("my-firebase-app-name-is-here", "us-central1", "matchmaking-queue-cleanup");
const task = {
    appEngineHttpRequest: {
        httpMethod: 'POST',
        relativeUri: '/leave-queue'
    },
};
task.appEngineHttpRequest.body = Buffer.from(JSON.stringify({
    id: "test"
})).toString('base64');
task.scheduleTime = {
    minutes: 13
};
const request = {parent, task};
client.createTask(request).then((response) => {
    console.log(`created task ${response.name}`);
}).catch(console.log);

This causes the task to show up in my console under cloud tasks.

cloud tasks

The task keeps failing and retrying. I don't see any logs from the cloud function side.

Here's what the cloud function looks like. enter image description here

enter image description here

Any idea what I need to do to get the cloud task to invoke the cloud function?

Upvotes: 1

Views: 1386

Answers (2)

Vikram Shinde
Vikram Shinde

Reputation: 1028

Have you enabled the App Engine API ? The Cloud Function needs this API enabled.

Upvotes: -1

Averi Kitsch
Averi Kitsch

Reputation: 921

To trigger a Cloud Function you must use HTTP targets, not App Engine targets. With App Engine targets the task is trying to find a special route to an App Engine application. You would change this in your task object:

const task = {
    httpRequest: {
        httpMethod: 'POST',
        url: <URL_FOR_FUNCTION>
    },
};

Here's a tutorial on how to use Cloud Tasks with Cloud Functions.

Upvotes: 2

Related Questions