Laravel调度程序:每秒执行一次命令

时间:2016-07-26 09:48:31

标签: php laravel cron

我有一个需要通过WebSockets连续发送通知的项目。它应该连接到以字符串格式返回整体状态的设备。系统处理它,然后根据各种条件发送通知。

由于调度程序可以在一分钟内重复执行任务,因此我需要找到一种每秒执行该功能的方法。

这是我的app/Console/Kernel.php

<?php    
  ...    
class Kernel extends ConsoleKernel
{
    ...
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function(){
            // connect to the device and process its response
        })->everyMinute();
    }
}
PS:如果您有更好的想法来处理这种情况,请分享您的想法。

4 个答案:

答案 0 :(得分:4)

通常,当您想要比1分钟更多的粒度时,您必须编写一个守护进程。

我建议你试试,现在它并不像几年前那么难。只需从CLI命令中的一个简单循环开始:

while (true) {
    doPeriodicStuff();

    sleep(1);
}

一件重要的事情:通过supervisord运行守护程序。您可以查看有关Laravel的队列侦听器设置的文章,它使用相同的方法(守护进程+ supervisord)。配置部分可能如下所示:

[program:your_daemon]
command=php artisan your:command --env=your_environment
directory=/path/to/laravel
stdout_logfile=/path/to/laravel/app/storage/logs/your_command.log
redirect_stderr=true
autostart=true
autorestart=true

答案 1 :(得分:1)

$schedule->call(function(){
    while (some-condition) {
        runProcess();
    }
})->name("someName")->withoutOverlapping();

根据runProcess()执行的时间长短,您可以使用sleep(seconds)进行更多微调。

some-condition通常是一个标志,您可以随时更改以控制无限循环。例如您可以在需要时使用file_exists(path-to-flag-file)手动启动或停止该过程。

答案 2 :(得分:1)

每秒您可以将命令添加到cron作业

*   *   *   *   *   /usr/local/php56/bin/php56 /home/hamshahr/domains/hamshahrapp.com/project/artisan taxis:beFreeTaxis 1>> /dev/null 2>&1

并在命令中:

<?php

namespace App\Console\Commands;

use App\Contracts\Repositories\TaxiRepository;
use App\Contracts\Repositories\TravelRepository;
use App\Contracts\Repositories\TravelsRequestsDriversRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;

class beFreeRequest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'taxis:beFreeRequest';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'change is active to 0 after 1 min if yet is 1';

    /**
     * @var TravelsRequestsDriversRepository
     */
    private $travelsRequestsDriversRepository;

    /**
     * Create a new command instance.
     * @param TravelsRequestsDriversRepository $travelsRequestsDriversRepository
     */
    public function __construct(
        TravelsRequestsDriversRepository $travelsRequestsDriversRepository
    )
    {
        parent::__construct();
        $this->travelsRequestsDriversRepository = $travelsRequestsDriversRepository;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $count = 0;
        while ($count < 59) {
            $startTime =  Carbon::now();

            $this->travelsRequestsDriversRepository->beFreeRequestAfterTime();

            $endTime = Carbon::now();
            $totalDuration = $endTime->diffInSeconds($startTime);
            if($totalDuration > 0) {
                $count +=  $totalDuration;
            }
            else {
                $count++;
            }
            sleep(1);
        }

    }
}

答案 3 :(得分:-2)

您可以尝试使用sleep(1)每秒复制作业* 60次。

相关问题