运行 每个实例上的计划 Azure PHP 脚本

Run a scheduled Azure PHP script on each instance

我们有一个网站运行在多个 Azure 实例上运行——通常在 2 到 5 个之间。

有一个 PHP 脚本,我想在每个实例上每隔几分钟安排一次 运行。 (它只是从无法处理我们所有用户实时访问的负载的系统中制作数据的本地副本。)

如果只是一个实例,那会很容易 - 我会使用 Azure Scheduler 调用 www.example.com/my-scheduled-task.php 每 5 分钟一次。

但是脚本需要 运行 在 每个 实例上,以便每个实例都有一个合理的最新数据副本。你将如何实现这一目标?我无法确定它是否在 Azure Scheduler 中,或者我是否应该查看某种启动脚本?

您可以为此使用连续的网络作业。

只需调整您的 php 脚本使其具有循环并在 运行 代码之间添加几分钟的休眠。

持续的网络作业将 运行 在您的所有实例上,即使出现故障,它也会重新启动。

根据我的经验,正如@AmitApple 所说,每个 webapp 实例上的 PHP webjob 运行 是很好的解决方案。但是,我认为您可以尝试使用带有 CRON 表达式的预定 webjob 来确保开始时间,而不是带有睡眠时间的连续 webjob。并且请确保脚本可以在间隔时间内完成。

您可以参考the section Create a scheduled WebJob using a CRON expression of the doc Run Background tasks with WebJobs了解如何开始。

请参阅 Create a continuously running WebJob https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/#CreateContinuous 部分的注释。

Note:

If your web app runs on more than one instance, a continuously running WebJob will run on all of your instances. On-demand and scheduled WebJobs run on a single instance selected for load balancing by Microsoft Azure.

For Continuous WebJobs to run reliably and on all instances, enable the Always On* configuration setting for the web app otherwise they can stop running when the SCM host site has been idle for too long.