在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):dusterio/laravel-aws-worker开源软件地址(OpenSource Url):https://github.com/dusterio/laravel-aws-worker开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):laravel-aws-workerRun Laravel (or Lumen) tasks and queue listeners inside of AWS Elastic Beanstalk workers OverviewLaravel documentation recommends to use supervisor for queue workers and *IX cron for scheduled tasks. However, when deploying your application to AWS Elastic Beanstalk, neither option is available. This package helps you run your Laravel (or Lumen) jobs in AWS worker environments. Dependencies
Scheduled tasks - option 1Option one is to use Kernel.php as the schedule and run Laravel schedule runner every minute.
You remember how Laravel documentation advised you to invoke the task scheduler? Right, by running * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1 AWS doesn't allow you to run *IX commands or to add cron tasks directly. Instead, you have to make regular HTTP (POST, to be precise) requests to your worker endpoint. Add cron.yaml to the root folder of your application (this can be a part of your repo or you could add this file right before deploying to EB - the important thing is that this file is present at the time of deployment): version: 1
cron:
- name: "schedule"
url: "/worker/schedule"
schedule: "* * * * *" From now on, AWS will do POST /worker/schedule to your endpoint every minute - kind of the same effect we achieved when editing a UNIX cron file. The important difference here is that the worker environment still has to run a web process in order to execute scheduled tasks.
Behind the scenes it will do something very similar to a built-in Your scheduled tasks should be defined in protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->everyMinute();
} Scheduled tasks - option 2Option two is to use AWS schedule defined in the cron.yml: version: 1
cron:
- name: "run:command"
url: "/worker/schedule"
schedule: "0 * * * *"
- name: "do:something --param=1 -v"
url: "/worker/schedule"
schedule: "*/5 * * * *" Note that AWS will use UTC timezone for cron expressions. With the above example,
AWS will hit /worker/schedule endpoint every hour with Pick whichever option is better for you! Queued jobs: SQSNormally Laravel has to poll SQS for new messages, but in case of AWS Elastic Beanstalk messages will come to us – inside of POST requests from the AWS daemon. Therefore, we will create jobs manually based on SQS payload that arrived, and pass that job to the framework's default worker. From this point, the job will be processed the way it's normally processed in Laravel. If it's processed successfully, our controller will return a 200 HTTP status and AWS daemon will delete the job from the queue. Again, we don't need to poll for jobs and we don't need to delete jobs - that's done by AWS in this case. If you dispatch jobs from another instance of Laravel or if you are following Laravel's payload format Configuring the queueEvery time you create a worker environment in AWS, you are forced to choose two SQS queues – either automatically generated ones or some of your existing queues. One of the queues will be for the jobs themselves, another one is for failed jobs – AWS calls this queue a dead letter queue. You can set your worker queues either during the environment launch or anytime later in the settings: Don't forget to set the HTTP path to You have to tell Laravel about this queue. First set your queue driver to SQS in
Then go to ...
'sqs' => [
'driver' => 'sqs',
'key' => 'your-public-key',
'secret' => 'your-secret-key',
'prefix' => 'https://sqs.us-east-1.amazonaws.com/your-account-id',
'queue' => 'your-queue-name',
'region' => 'us-east-1',
],
... To generate key and secret go to Identity and Access Management in the AWS console. It's better to create a separate user that ONLY has access to SQS. Installation via ComposerTo install simply run:
Or add it to {
"require": {
"dusterio/laravel-aws-worker": "~0.1"
}
} Usage in Laravel 5// Add in your config/app.php
'providers' => [
'...',
'Dusterio\AwsWorker\Integrations\LaravelServiceProvider',
]; After adding service provider, you should be able to see two special routes that we added: $ php artisan route:list
+--------+----------+-----------------+------+----------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-----------------+------+----------------------------------------------------------+------------+
| | POST | worker/queue | | Dusterio\AwsWorker\Controllers\WorkerController@queue | |
| | POST | worker/schedule | | Dusterio\AwsWorker\Controllers\WorkerController@schedule | |
+--------+----------+-----------------+------+----------------------------------------------------------+------------+ Environment variable This variable is set to So that's it - if you (or AWS) hits Usage in Lumen 5// Add in your bootstrap/app.php
$app->register(Dusterio\AwsWorker\Integrations\LumenServiceProvider::class); Errors and exceptionsPlease make sure that two special routes are not mounted behind a CSRF middleware. Our POSTs are not real web forms and CSRF is not necessary here. If you have a global CSRF middleware, add these routes to exceptions, or otherwise apply CSRF to specific routes or route groups. If your job fails, we will throw a ToDo
Video tutorialsI've just started a educational YouTube channel that will cover top IT trends in software development and DevOps: config.sys Also I'm glad to announce a new cool tool of mine – GrammarCI, an automated typo/grammar checker for developers, as a part of the CI/CD pipeline. ImplicationsNote that AWS cron doesn't promise 100% time accuracy. Since cron tasks share the same queue with other jobs, your scheduled tasks may be processed later than expected. Post scriptumI wrote a blog post explaining how this actually works. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论