如何使用同一处理程序为多个cron作业设置cron.yaml?

时间:2018-10-30 09:43:47

标签: google-app-engine

这是我的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.yamlserver.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);
});

1 个答案:

答案 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作业。