gpt4 book ai didi

laravel - 如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度?

转载 作者:行者123 更新时间:2023-12-04 11:17:19 25 4
gpt4 key购买 nike

设想

作为 Laravel 和 Elastic Beanstalk 的新用户,我很快发现自己需要像我们大多数人一样安排操作。

过去,我一直为此使用简单的 crontab 调度。所以现在我站在一系列问题面前:

  • 如何使用 crontab 运行 Laravel 代码?
  • 如何在 Elastic Beanstalk 环境中设置 crontab?

  • 找到这些问题的个别答案并不难。将它们结合起来并真正让它全部工作但结果证明有点棘手,这就是为什么我决定在这里为其他努力使其正常工作的人分享解决方案。

    环境
  • Laravel 5.6
  • PHP 7.1
  • 最佳答案

    特尔;博士:

    见工作.ebextentions答案末尾的配置。

    环境

  • Laravel 5.6
  • PHP 7.1


  • 如何使用 crontab 运行 Laravel 代码?

    这个问题的答案当然是最明显的,如果你对 Laravel 有一点点了解,你肯定知道答案: Scheduling !

    我不会让你厌烦解释 Laravel 调度的绝妙之处,因为你可以自己在文档中阅读它。

    但我们需要随身携带的关键是 Laravel 调度使用 crontab 来执行,如文档中所述:
    * * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

    这将我们带到了下一个更棘手的问题......

    如何在 Elastic Beanstalk 环境中设置 crontab?

    乍一看,这个问题的答案似乎很简单。我在 AWS 知识中心找到了这个: How do I create a cron job on EC2 instances in an Elastic Beanstalk environment?

    在这里,他们描述了如何使用 .ebextentions 在您的 Elastic Beanstalk EC2 机器上设置 cron 作业。简而言之,它的作用是在目录 /etc/cron.d/ 中创建一个新文件。我们在其中放置了我们想要的 cron 作业。

    然后这个目录中的文件被 crontab 处理为 root用户。正如我在下面评论的那样,我走进了一些陷阱:
    files:

    # The name of the file should not contain any dot (.) or dash (-), this can
    # cause the script not to run. Underscore (_) is OK.
    "/etc/cron.d/mycron":

    # This permissions is important so that root user can run the script.
    mode: "000644"

    # As the file is run by the root user it needs to be the owner of the file.
    owner: root

    # For consistency it's a good idea to have root as the group aswell.
    group: root

    # NOTE: We need to explicitly tell the cron job to be run as the root user!
    content: |
    * * * * * root /usr/local/bin/myscript.sh

    # There need to be a new line after the actual cron job in the file.

    一旦我们清除了所有这些陷阱,就该从上面开始我们的 Laravel 调度 cron 工作了。那应该是这样的:
    files:
    "/etc/cron.d/schedule_run":
    mode: "000644"
    owner: root
    group: root
    content: |
    * * * * * root php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

    尽管在大多数情况下这不会真正起作用 .那是因为 Laravel 调度器将无法访问您的 ENV 变量,并且必须明显不能访问您的数据库设置。

    我在这里找到了答案: How to Get Laravel Task Scheduling Working on AWS Elastic Beanstalk Cron

    所以向 George Bonnisch 大声喊叫;我向你先生致敬,分享这个!

    因此,通过这最后一块拼图,我终于能够使设置正常工作:

    工作解决方案

    文件结构:
    [Project root]
    |-- .ebextensions
    | |-- cronjob.config

    cronjob.config:
    files:
    "/etc/cron.d/schedule_run":
    mode: "000644"
    owner: root
    group: root
    content: |
    * * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1

    commands:
    remove_old_cron:
    command: "rm -f /etc/cron.d/*.bak"

    在 AWS Elastic Beanstalk 上使用 Laravel 调度时的提示!

    由于 Elastic Beanstalk 的主要功能之一是它可以在需要时自动扩展和添加更多服务器,因此您可能需要查看 Laravel 调度中的新功能: Running Tasks On One Server .

    在许多情况下,您不希望您的 cron 作业不仅仅在一台服务器上执行。例如,如果您有一个发送电子邮件的预定命令,则您不希望多次发送这些命令。

    注意:这要求您使用 memcached 或 redis 作为缓存引擎,如文档中所述。如果没有,请查看 AWS 服务 Elasticache .

    注 2:使用时 onOneServer()您必须使用 name() 为计划任务命名。方法(在调用 onOneServer() 之前)。像这样:
    $schedule->command('my:task')
    ->name('my:task')
    ->daily()
    ->onOneServer();

    关于laravel - 如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51096921/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com