云任务未按时运行

时间:2019-12-07 06:38:16

标签: google-cloud-platform google-cloud-functions google-cloud-tasks

我想做的是在计划的时间使用云功能和云任务更改Firestore中的数据。但是云任务不能按时运行。添加任务后立即执行。

我的代码是这样的。

index.js

exports.addTasks = functions.https.onCall((data, context) => {
  const client = new tasks.CloudTasksClient()

  const projectId = ...
  const queue = ...
  const location = ...

  const parent = client.queuePath(projectId, location, queue)

  const url = ... .cloudfunctions.net/doSomething?docId=' + docId

  const task = {
    httpRequest: {
      httpMethod: 'POST',
      url: url,
      scheduleTime: {
        seconds: 3 * 60 + Date.now() / 1000,
      }, 
    }
  }

  const request = {
    parent: parent,
    task: task,
  }

  client.createTask(request)
})

exports.doSomething = functions.https.onRequest((req, res) => {
  var db = admin.firestore()
  var docId = req.query.docId
  var docRef = db.collection('people').doc(docId)

  docRef.update({
    changeHere: true,
  })
})

我想在执行doSomething后三分钟运行addTasks函数。我这是怎么了?

1 个答案:

答案 0 :(得分:1)

scheduleTime是任务对象的属性,而不是httpRequest的属性。

  const task = {
    httpRequest: {
      httpMethod: 'POST',
      url: url,
    },
    scheduleTime: {
      seconds: 3 * 60 + Date.now() / 1000,
    }, 
  }

这是reference docsample code上显示的内容。