你如何 运行 一个间隔的云函数?
How do you run a cloud function at an interval?
我一直在尝试按照这些思路做一些事情:
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun(() => helloWorld());
export const helloWorld = functions.https.......
我想每分钟 运行 helloWorld
云函数,但似乎无法弄清楚。
谢谢。
您似乎混淆了基于 HTTP 的 Cloud Functions 和预定的 Cloud Functions。它们彼此独立。根据 helloWorld()
的功能,前进的方向不同。
HTTPS 可调用函数
如果您现有的函数是 HTTPS 可调用函数,则看起来像:
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun(() => helloWorld());
export const helloWorld = functions.https.onCall((data, context) => {
// do the task
// make sure to return a Promise
});
你会把它编辑成:
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun((context) => {
// do the task
// make sure to return a Promise
});
如果您希望您的函数可调用并且 运行 按计划进行,您可以改用:
function handleHelloWorldTask(data, context) {
// do the task
// make sure to return a Promise
}
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun((context) => handleHelloWorldTask({ scheduled: true }, context));
export const helloWorld = functions.https.onCall(handleHelloWorldTask);
HTTPS 请求处理程序
如果您现有的函数是 HTTPS 请求处理程序,您将使用:
const FIREBASE_PROJECT_ID = JSON.parse(process.env.FIREBASE_CONFIG).projectId;
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun(async (context) => {
const response = await fetch(`https://us-central1-${FIREBASE_PROJECT_ID}.cloudfunctions.net/helloWorld`);
if (response.ok) {
console.log('Triggered helloWorld successfully');
} else {
throw new Error(`Unexpected status code ${response.status} from helloWorld: ${await response.text()}`);
}
});
export const helloWorld = functions.https.onRequest((req, res) => {
// do the task
// make sure to call res.end(), res.send() or res.json()
});
我一直在尝试按照这些思路做一些事情:
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun(() => helloWorld());
export const helloWorld = functions.https.......
我想每分钟 运行 helloWorld
云函数,但似乎无法弄清楚。
谢谢。
您似乎混淆了基于 HTTP 的 Cloud Functions 和预定的 Cloud Functions。它们彼此独立。根据 helloWorld()
的功能,前进的方向不同。
HTTPS 可调用函数
如果您现有的函数是 HTTPS 可调用函数,则看起来像:
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun(() => helloWorld());
export const helloWorld = functions.https.onCall((data, context) => {
// do the task
// make sure to return a Promise
});
你会把它编辑成:
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun((context) => {
// do the task
// make sure to return a Promise
});
如果您希望您的函数可调用并且 运行 按计划进行,您可以改用:
function handleHelloWorldTask(data, context) {
// do the task
// make sure to return a Promise
}
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun((context) => handleHelloWorldTask({ scheduled: true }, context));
export const helloWorld = functions.https.onCall(handleHelloWorldTask);
HTTPS 请求处理程序
如果您现有的函数是 HTTPS 请求处理程序,您将使用:
const FIREBASE_PROJECT_ID = JSON.parse(process.env.FIREBASE_CONFIG).projectId;
export const refreshJob = functions.pubsub
.schedule("every 1 minutes")
.onRun(async (context) => {
const response = await fetch(`https://us-central1-${FIREBASE_PROJECT_ID}.cloudfunctions.net/helloWorld`);
if (response.ok) {
console.log('Triggered helloWorld successfully');
} else {
throw new Error(`Unexpected status code ${response.status} from helloWorld: ${await response.text()}`);
}
});
export const helloWorld = functions.https.onRequest((req, res) => {
// do the task
// make sure to call res.end(), res.send() or res.json()
});