denismilovanov/laravel-single-instance-command

此包已被废弃且不再维护。未建议替代包。

0.3 2016-11-27 10:15 UTC

This package is not auto-updated.

Last update: 2018-12-10 16:01:52 UTC


README

此包允许您保存和控制 artisan 命令的 pids,以便组织多进程,支持长时间计算,防止同时运行多个进程等。

我只使用此包进行向后兼容。

安装

composer require 'denismilovanov/laravel-single-instance-command: 0.1.*@dev'

使用方法

在 artisan 中

$app->register('LaravelSingleInstanceCommand\ServiceProvider');

在命令中

use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;

class MyCommand extends \LaravelSingleInstanceCommand\Command
{
    public $name = 'my-command';
     
    public function run(InputInterface $input, OutputInterface $output)
    {
        $this->checkInstance($input);
        
        // long living code here:
        // Queue::subscribe('queue', function ($job) {
        //
        // });
    }
}

在 shell(或 crontab)中

  • 启动进程 php artisan my-command

  • 启动另一个进程 php artisan my-command p=2

  • 停止进程 php artisan my-command p=2 stop

  • 停止所有 php artisan pids:stop-all

  • 删除过期的 pids 文件 php artisan pids:remove

注意

Pids 保存到 /tmp/{APP_ENV}/ 目录。
停止进程是通过向其发送 SIGTERM 实现的。