2016-07-26 182 views
2

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

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

这里是我的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:如果你有更好的想法来处理的情况下,请分享你的看法。

+1

使用事件循环触发每一秒守护进程。您可以使用库[icicle](https://icicle.io/)作为该任务,[supervisord](http://supervisord.org/)作为管理器,如果意外退出,将启动该过程。这可能看起来过于夸张,但某些事情看起来很简单,直到解决问题的核心。如果你需要不断更新,这是一条路。 – Mjh

回答

3

通常,当你想要比1分钟更精细时,你必须编写一个守护进程。

我建议你尝试一下,现在不像以前那么难。只需用CLI命令内一个简单的循环开始:

while (true) { 
    doPeriodicStuff(); 

    sleep(1); 
} 

一件重要的事情:通过supervisord运行的守护进程。你可以看一下关于Laravel的队列监听器设置的文章,它使用相同的方法(守护进程+监督)。一个配置部分可以是这样的:

[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 
-2

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

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

根据您的runProcess()需要多长时间来执行,你可以使用sleep(seconds)有更多的微调。

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

0

每秒可以添加命令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); 
     } 

    } 
}