如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度? [英] How do I setup and use Laravel Scheduling on AWS Elastic Beanstalk?

查看:27
本文介绍了如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As a fairly new user of Laravel and Elastic Beanstalk I soon found my self in the need to schedule operations, like most of us do.

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

In the past I had always used simple crontab scheduling for this. So now I stood before a list of questions:

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

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

Finding the individual answers to these questions weren't that hard. Combining them and actually getting it all to work however turned out to be a bit tricky, which is why I've decided to share the solution here for others struggling with getting this to work properly.

  • Laravel 5.6
  • PHP 7.1

推荐答案

TL;DR:

请参阅答案末尾的有效 .ebextentions 配置.

  • Laravel 5.6
  • PHP 7.1

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

The answers to this question is of course the most obvious and if you're even the slightest in to Laravel you surely know the answer: Scheduling!

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

I won't bore you with explaining the brilliant thing that is Laravel Scheduling since you can read about it in the documentation yourself.

但我们需要随身携带的关键是 Laravel 调度使用 crontab 来执行,如文档中所述:

But the key thing we need to take with us is that Laravel Scheduling uses crontab to execute, as described in the documentation:

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

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

乍一看,这个问题的答案似乎很简单.我在 AWS 知识中心找到了这个:How do I create aElastic Beanstalk 环境中 EC2 实例上的 cron 作业?

At first glance the answer to this question may seem pretty straight forward. I found this in the AWS Knownledge Center: How do I create a cron job on EC2 instances in an Elastic Beanstalk environment?

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

Here they describe how to setup a cron job on your Elastic Beanstalk EC2 machine using .ebextentions. In short what it does is creating a new file in the directory /etc/cron.d/ in which we put our desired cron job.

此目录中的文件随后由 crontab 作为 root 用户处理.正如我在下面评论的那样,我走进了一些陷阱:

Files in this directory is then processed by crontab as the root user. There are some of the traps I walked in to, as I've commented below:

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 工作了.应该是这样的:

Once we have stayed clear all of those traps, it's time to put in our Laravel Scheduling cron job from above. That should look something like this:

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 变量,而且必须明显不能访问您的数据库设置.

This won't really work in most cases though. That's because the Laravel Scheduler won't have access to your ENV variables and must noticeably not your database settings.

我在这里找到了答案:如何获取在 AWS Elastic Beanstalk Cron 上工作的 Laravel 任务调度

向 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 调度中的新功能:在一台服务器上运行任务.

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

In many cases you don't want your cron job to be executed on more than just one server. For example if you have a scheduled command for sending emails you don't want those sent multiple times.

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

NOTE: This requires that you use memcached or redis as your cache engine, as stated in the documentation. If you don't, have a look at the AWS service Elasticache.

注意 2: 使用 onOneServer() 时,您必须使用 name() 方法为计划任务命名(在调用 <代码>onOneServer()).像这样:

NOTE 2: When using onOneServer() you must give the scheduled task a name using the name() method (before calling onOneServer()). Like so:

$schedule->command('my:task')
    ->name('my:task')
    ->daily()
    ->onOneServer();

这篇关于如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆