这是我的cron.yaml
:
cron:
- description: 'cron trigger create email'
url: /cron/events/createEmail
schedule: every 1 hours
target: cron-jobs-background-cloud-function
- description: 'cron trigger create user'
url: /cron/events/createUser
schedule: every 1 hours
target: cron-jobs-background-cloud-function
server.js
:
function taskHandler() {}
app.get('/cron/events/createEmail', (req, res) => {
const topicName = req.path.split('/').slice(-1)[0];
console.log('topicName: ', topicName);
taskHandler(topicName);
res.sendStatus(200);
});
app.get('/cron/events/createUser', (req, res) => {
const topicName = req.path.split('/').slice(-1)[0];
console.log('topicName: ', topicName);
taskHandler(topicName);
res.sendStatus(200);
});
cron.yaml
和server.js
都是重复的。
cron service
是否支持这样的路径模式:
cron.yaml
:
cron:
- description: 'cron-jobs-background-cloud-function'
url: /cron/events/*
schedule: every 1 hours
target: cron-jobs-background-cloud-function
server.js
:
app.get('/cron/events/*', (req, res) => {
const topicName = req.path.split('/').slice(-1)[0];
console.log('topicName: ', topicName);
taskHandler(topicName);
res.sendStatus(200);
});
答案 0 :(得分:1)
我怀疑您不能在cron.yaml
中使用通配符,但是可以在app.yaml
中使用通配符,这应该可以实现您想要的操作。
像这样保持您的cron.yaml
:
cron:
- description: 'cron trigger create email'
url: /cron/events/createEmail
schedule: every 1 hours
target: cron-jobs-background-cloud-function
- description: 'cron trigger create user'
url: /cron/events/createUser
schedule: every 1 hours
target: cron-jobs-background-cloud-function
像这样设置app.yaml
:
handlers:
- url: /cron/events/*
script: [PATH TO APP]
然后,您可以像在问题中一样使用一个处理程序来处理所有cron作业。