I want to schedule some cloud tasks to run at some specific times, however adding cloud tasks to my cloud functions project is now resulting in failure to deploy, with no errors anywhere for me to see?
Here is my project (uses express):
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import express = require('express');
import cors = require('cors');
import helmet = require('helmet');
import favicon = require('serve-favicon');
import sgMail = require('@sendgrid/mail');
import { RuntimeOptions } from 'firebase-functions';
import { CloudTasksClient } from '@google-cloud/tasks';
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
export const db = admin.firestore();
sgMail.setApiKey(functions.config().sendgrid.key);
const createTestTask = async (text: string) => {
try {
const client = new CloudTasksClient();
const project = 'my project ID is here';
const queue = 'my-test-queue';
const location = 'europe-west2';
const parent = client.queuePath(project, location, queue);
const url =
'my cloud function url is here';
const payload = JSON.stringify({
user: 'Test',
mode: 'secret mode',
text,
});
const body = Buffer.from(payload).toString('base64');
const task = {
httpRequest: {
httpMethod: 'POST',
url: url,
dispatchDeadline: 30 * 60, // 30 minutes
body: body,
headers: { 'Content-type': 'application/json' },
},
};
// Send create task request.
console.log('Sending task:');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const [response] = await client.createTask({ parent, task });
console.log(`Created task ${response.name}`);
} catch (e) {
functions.logger.error(e);
}
};
// #####################################################################
app.post('/tasks', async (req, res) => {
try {
const { text } = req.body;
createTestTask(text);
res.status(201).send({
success: true,
message: 'Success',
});
} catch (e) {
functions.logger.error(e);
}
});
// #####################################################################
app.post('/tasks/test', async (req, res) => {
try {
const { text } = req.body;
await db.collection('tasks').doc().create({
text,
});
res.status(201).send({
success: true,
message: 'Success',
});
} catch (e) {
functions.logger.error(e);
}
});
// #####################################################################
exports.api = regionalFunctions.runWith(expressOpts).https.onRequest(app);
The errors I get when I deploy are:
Functions deploy had errors with the following functions:
api(europe-west2)
Error: There was an error deploying functions:
- Error Failed to update function api in region europe-west2
The project deploys fine without the cloud tasks code in here and just normal express routes, does anyone know why or where I can find out what exactly is wrong here?
CodePudding user response:
As per the documentation, you can set/change the default region of the cloud functions from us-central1 through the below code example (I don’t know why you had put
regionalFunctions.runWith(expressOpts).https.onRequest(app);
It should be :
const functions = require('firebase-functions');
exports.api = functions
.runWith(expressOpts)
.region('europe-west2')
.https.onRequest(async(req, res) => {
try {
await createMessage();
res.status(200).send("OK");
}
catch (error) {
// Handle the error
console.log(error);
res.status(500).send(error);
};
I think the function deployment failed because it cannot find the @google-cloud/tasks
in package.json in your function directory and there's no problem about region concern. It can't be deployed to any region if there's a missing dependency in your function.
To fix it and add the @google-cloud/tasks
dependency in your function/package.json
, run the following command in your function directory:
npm install @google-cloud/tasks