现在我已经添加了云任务,但我的云功能应用程序无法部署?
My cloud functions app failing to deploy now that i've added cloud tasks?
我想在某些特定时间将一些云任务安排到 运行,但是将云任务添加到我的云功能项目现在导致部署失败,而且我在任何地方都看不到任何错误?
这是我的项目(使用 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);
我在部署时遇到的错误是:
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
项目部署正常,这里没有云任务代码,只有正常的快速路线,有谁知道为什么或在哪里可以找出这里到底出了什么问题?
根据 documentation,您可以通过下面的代码示例 set/change 来自 us-central1 的云函数的默认区域(我不知道您为什么把
regionalFunctions.runWith(expressOpts).https.onRequest(app);
应该是:
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);
};
我认为函数部署失败是因为它在你的函数目录下找不到package.json中的@google-cloud/tasks
,并且区域关注没有问题。如果您的函数中缺少依赖项,则无法将其部署到任何区域。
要修复它并在您的 function/package.json
中添加 @google-cloud/tasks
依赖项,运行 在您的函数目录中执行以下命令:
npm install @google-cloud/tasks
我想在某些特定时间将一些云任务安排到 运行,但是将云任务添加到我的云功能项目现在导致部署失败,而且我在任何地方都看不到任何错误?
这是我的项目(使用 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);
我在部署时遇到的错误是:
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
项目部署正常,这里没有云任务代码,只有正常的快速路线,有谁知道为什么或在哪里可以找出这里到底出了什么问题?
根据 documentation,您可以通过下面的代码示例 set/change 来自 us-central1 的云函数的默认区域(我不知道您为什么把
regionalFunctions.runWith(expressOpts).https.onRequest(app);
应该是:
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);
};
我认为函数部署失败是因为它在你的函数目录下找不到package.json中的@google-cloud/tasks
,并且区域关注没有问题。如果您的函数中缺少依赖项,则无法将其部署到任何区域。
要修复它并在您的 function/package.json
中添加 @google-cloud/tasks
依赖项,运行 在您的函数目录中执行以下命令:
npm install @google-cloud/tasks