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

查看:83
本文介绍了如何在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 Scheduling带来的精妙之处.

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 Scheduling使用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 Knownledge Center中找到了此文件:如何创建一个在Elastic 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 Scheduling 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 Scheduler无法访问您的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任务调度的工作

I found the answer to this here: How to Get Laravel Task Scheduling Working on AWS Elastic Beanstalk Cron

向GeorgeBönnisch大喊大叫;我向您致敬,谢谢您的分享!

因此,通过拼图的最后一部分,我终于能够使设置正常运行:

文件结构:

[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天全站免登陆