Cloud Scheduler 随机性

Cloud Scheduler randomness

我想在每天下午 1 点到 2 点之间的随机时间 运行 一个 Cloud Function。我使用 Cloud Scheduler (cron) 将消息发布到触发该功能的 PubSub。我想在 Cloud Scheduler 端实现这种随机性,以避免在函数中调用 sleep 。 有办法实现吗?

你不能那样做。你需要一个中间层。最便宜的是用Cloud Workflows.

Cloud Workflow 的强大之处在于,您只需 运行 一个步骤就可以付费,而不是按工作流程持续时间付费。因此你可以睡一会儿,你不会为你不使用的空闲 CPU 付费,当你使用 Cloud Functions 或 Cloud 运行 时发生的事情(当你将 SLEEP 放入你的代码)。

这里是过程:

  • 使用 Cloud Funcions
  • 创建 step that call a URL that generate a random number. If you haven't a public URL that do that, you can use this sample
  • 根据你上一步的随机结果创建下面的step that sleeps
  • 调用您的云函数

而你call your workflow with Cloud Scheduler每天