2016-07-26 138 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); 
     } 

    } 
}