定制 jdavidbakr/multi-server-event 二次开发

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

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

jdavidbakr/multi-server-event

最新稳定版本:2.0.5

Composer 安装命令:

composer require jdavidbakr/multi-server-event

包简介

This package extends Laravel's native Command Event class to allow for managing events fired on the same system with multiple servers, preventing an event from firing more than once

README 文档

README

Latest Version on Packagist Software License Total Downloads

This package extends Laravel's native Event class to include the ability to block events from occurring aross multiple servers, as would be the case if you have a laravel instance behind a load balancer in an auto-scaling situation.

It uses a database table to track the currently running process, and each server generates a unique key to lock the command. In order to prevent a condition where a short-running command's lock doesn't last long enough, we are implementing a minimum 10 second break between the completion of the command and its next execution time, so if a command runs every minute but takes between 50 and 59 seconds to complete, the next command will be delayed one more minute.

NOTE: Laravel 5.6 now contains a function onOneServer() that solves the issue that this package was built to fix. Therefore, it is recommended that you use the core function instead of this package.

For Laravel < 5.4, please use version 1.X

Upgrading from version 1.X

If upgrading from version 1.X, please note the change in the defineConsoleSchedule() command in app\Console\Kernel.php.

Installation

$ composer require jdavidbakr/multi-server-event

The new event structure uses a database table to track which server is currently executing an event. You must create the database table using the provided migration. To do this, add the following to the $commands array in \App\Console\Kernel.php:

\jdavidbakr\MultiServerEvent\Commands\MultiServerMigrationService::class,

then perform the migration

php artisan make:migration:multi-server-event
php artisan migrate

Now we want to change the default schedule IoC to use this alternate one. In app\Console\Kernel.php add the following function:

/**
 * Define the application's command schedule.
 *
 * @return void
 */
protected function defineConsoleSchedule()
{
    $this->app->instance(
        Schedule::class,
        $schedule = new \jdavidbakr\MultiServerEvent\Scheduling\Schedule()
    );

    $this->schedule($schedule);
}

Usage

When composing your schedule, simply add "withoutOverlappingMultiServer()" to the command, i.e.

$schedule->command('inspire')
    ->daily()
    ->withoutOverlappingMultiServer();

This will prevent multiple servers from executing the same event at the same time.

When composing your schedule, you can also ensure that cron is not stuck, simply add "ensureFinishedMultiServer()" to the command, i.e.

$schedule->command('inspire')
    ->daily()
    ->withoutOverlappingMultiServer()
    ->ensureFinishedMultiServer(30);

This will prevent from stuck commands during lost connection or deployment. Note, choose time to be enough to mark as "stuck", fox example, long running command should have bigger tolerance time. You can track such events how ofter it happens by binding listeners on EnsureCleanUpExecuted event

Testing

phpunit

统计信息

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

GitHub 信息

  • Stars: 35
  • Watchers: 3
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-11