2016-07-26 1 views
2

웹 소켓을 통해 알림을 지속적으로 보내야하는 프로젝트가 있습니다. 전체 상태를 문자열 형식으로 반환하는 장치에 연결해야합니다. 시스템은이 정보를 처리 한 후 다양한 조건에 따라 알림을 전송합니다.Laravel schedular : 매초마다 명령 실행

스케줄러는 일찍 작업을 반복 할 수 있기 때문에 매 초마다이 기능을 실행할 방법을 찾아야합니다.

<?php  
    ...  
class Kernel extends ConsoleKernel 
{ 
    ... 
    protected function schedule(Schedule $schedule) 
    { 
     $schedule->call(function(){ 
      // connect to the device and process its response 
     })->everyMinute(); 
    } 
} 

PS :

여기 내 app/Console/Kernel.php 당신이 상황을 처리하는 더 나은 아이디어가 있다면, 귀하의 의견을 공유하시기 바랍니다.

+1

초마다 트리거하는 이벤트 루프를 사용하는 데몬. 이 작업에는 [icicle] (https://icicle.io/)과 같은 라이브러리를 사용할 수 있으며 예기치 않게 종료되면 프로세스를 부팅하는 관리자로 [supervisord] (http://supervisord.org/)를 사용할 수 있습니다 . 이는 과잉으로 보일 수 있지만 문제의 핵심에 도달하기까지는 특정 사항이 단순 해 보입니다. 지속적인 업데이트가 필요한 경우 이것이 방법입니다. – Mjh

답변

3

일반적으로 1 분 이상으로 세분화하려면 데몬을 작성해야합니다.

몇 년 전처럼 열심히 노력하지 말고 시도해 보시기 바랍니다.

while (true) { 
    doPeriodicStuff(); 

    sleep(1); 
} 

한 가지 중요한 일을 : 그냥 CLI 명령을 내 간단한 루프로 시작 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

sleep (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

초 당은 크론 작업

* * * * * /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); 
     } 

    } 
}