定制 eis3nhorn/laravel-elasticbeanstalk-cron 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eis3nhorn/laravel-elasticbeanstalk-cron

Composer 安装命令:

composer require eis3nhorn/laravel-elasticbeanstalk-cron

包简介

Ensure only one Laravel instance is running CRON jobs in an EB environment

README 文档

README

*** This Fork uses getenv to get vars (like USE_CRON) defined in Elastic Beanstalk Environment instead of using config to read them as it seems to fail on Elastic Beanstalk as of December 4 2018, it has also been updated to work with 6.0 branch Ensure one instance in an Elastic Beanstalk environment is running Laravel's Scheduler

A common problem many people have encountered with Amazon's Elastic Beanstalk is maintaining a single instance in an environment that runs Laravel's Task Scheduler. Difficulties arise because auto-scaling does not guarantee any instance is run indefinitely and there are no "master-slave" relationships within an environment to differentiate one instance from the rest.

Although Amazon has provided a solution it involves setting up a worker tier and then, potentially, creating new routes/methods for implementing the tasks that need to be run. Yuck!

This package provides a simple, zero-setup solution for maintaining one instance within an Elastic Beanstalk environment that runs the Task Scheduler.

How Does It Work?

Glad you asked! The below process is completely automated and only requires that you publish the .ebextensions folder to the root of your application.

1. Use Elastic Beanstalk's Advanced Configuration to run CRON setup commands

EB applications can contain a folder that provides advanced configuration for an EB environment, called .ebextensions.

This package provides a configuration file that runs two commands on deployment (every instance initialization) that setup the conditions needed to run the Task Schedler on one instance:

2. Run system:start:leaderselection

This is the first command that is run on deployment. It configures the instance's Cron to run Leader Selection at a configured interval (default = 5 minutes)

3. Run Leader Selection aws:configure:leader

This is the Leader Selection command. It does the following:

  • Get the Id of the Instance this deployment is running on
  • Get the EnvironmentName of this Instance. (When running in an EB environment all EC2 instances have the same EnvironmentName)
  • Get all running EC2 instances with that EnvironmentName
  • Find the earliest launched instance

If this instance is the earliest launched then it is deemed the Leader and runs system:start:cron

4. Run system:start:cron

This command is run only if the current instance running Leader Selection is the Leader. It inserts another entry in the instance's Cron to run Laravel's Scheduler.

That's it!

Now only one instance, the earliest launched, will have the scheduler inserted into its Cron. If that instance is terminated by auto-scaling a new Leader will be chosen within 5 minutes (or the configured interval) from the remaining running instances.

Installation

Require this package

composer require "eis3nhorn/laravel-elasticbeanstalk-cron"

After adding the package, add the ServiceProvider to the providers array in config/app.php

\eis3nhorn\LaravelElasticBeanstalkCron\ElasticBeanstalkCronProvider::class

Then, publish the .ebextensions folder and configuration file.

php artisan vendor:publish --tag=ebcron

Configuration

In order for Leader Selection to run a few environmental variables must be present:

  • USE_CRON = true -- Must be set in order for Leader Selection to occur. (This can be used to prevent Selection from occurring on undesired environments IE Workers, etc.)
  • AWS_ACCESS_KEY_ID -- Needed for read-only access to ec2 client
  • AWS_SECRET_ACCESS_KEY -- Needed for read-only access to ec2 client
  • AWS_REGION -- Sets which AWS region when looking using the ec2 client, defaults to us-east-1 if not set.

These can be included in your .env or, for EB, in the environment's configuration section.

Contributing

Make a PR for some extra functionality and I will happily accept it :)

License

This package is licensed under the MIT license.

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固