2016-10-20 10 views
8

पर्यावरण


विवरण

मैं लैरवेल Task Scheduling का उपयोग करके हर 1 मिनट में कमांड चलाने की कोशिश कर रहा हूं।


प्रयास

मैं अपने क्रॉन टैब फ़ाइल

* * * * * php artisan schedule:run >> /dev/null 2>&1

यहाँ करने के लिए इस लाइन को जोड़ दिया है मेरी /app/Console/Kernel.php

<?php 

namespace App\Console; 

use Illuminate\Console\Scheduling\Schedule; 
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; 

class Kernel extends ConsoleKernel 
{ 
    /** 
    * The Artisan commands provided by your application. 
    * 
    * @var array 
    */ 
    protected $commands = [ 
     \App\Console\Commands\Inspire::class, 
    ]; 

    /** 
    * Define the application's command schedule. 
    * 
    * @param \Illuminate\Console\Scheduling\Schedule $schedule 
    * @return void 
    */ 
    protected function schedule(Schedule $schedule) 
    { 
     $schedule->command('inspire')->hourly(); 
     $schedule->command('echo "Happy New Year!" ')->everyMinute(); //<---- ADD HERE  } 
} 
है

मैंने यह लाइनजोड़ दी है


प्रश्न

मैं यह कैसे परीक्षण करते हैं?

मैं अपने गूंज प्रदर्शित करने के लिए कैसे को गति प्रदान करते हैं?

मुझे कैसे पता चलेगा कि मैंने जो किया वह गलत नहीं है?


मैं इस पल में किसी भी सुझाव के लिए खोलने हूँ।

इस पर कोई संकेत/सुझाव/सहायता की सराहना की जाएगी!

+1

मैं जो कर रहा हूं वह प्रत्येक "कमांड" वास्तव में एक चलने योग्य कारीगर आदेश है। फिर आप यह सुनिश्चित करने के लिए उस आदेश का परीक्षण कर सकते हैं कि वह वही करता है जो आप चाहते हैं। मैं यह जांचने के लिए परेशान नहीं हूं कि शेड्यूलर अपेक्षित बीसी के रूप में काम करता है जो इसे ढांचे द्वारा प्रदान किया जाता है, इसलिए मैं इसे काम कर सकता हूं। एकमात्र चीज कमजोर है यदि आपने टाइपो बनाया है या गलत तरीके से शेड्यूलर सेट अप किया है, लेकिन यदि आप दस्तावेज़ों का पालन करते हैं तो इसका कोई कारण नहीं है कि इसे क्यों काम नहीं करना चाहिए और यह सिर्फ बीसी परीक्षण करने वाला होगा, किसी ने आपको सबकुछ परीक्षण करने के लिए कहा था लेकिन मूल्य न्यूनतम है .. – tam5

उत्तर

6

command() एक कारीगर आदेश चलाता है। ओएस के लिए एक आदेश जारी करने - - आप प्राप्त करना चाहते हैं क्या exec('echo "Happy New Year!"')

परीक्षण द्वारा किया जाता है पर निर्भर करता है कि आप क्या परीक्षण करना चाहते हैं:

  • चाहे अनुसूचक (हर मिनट) काम कर रहा है?

इस मामले में, आप की जरूरत नहीं है। यह मूल रूपरेखा कोड में परीक्षण किया जाता है।

  • क्या आदेश सफल होता है?

ठीक है, आप मैन्युअल रूप से चलाने के लिए और php artisan schedule:run उत्पादन देख सकते हैं।

शेड्यूलर डिफ़ॉल्ट रूप से किसी भी आउटपुट का उत्पादन नहीं करता है (>> /dev/null 2>&1)। हालांकि, आप writeOutputTo() या appendOutputTo() (https://laravel.com/docs/5.1/scheduling#task-output) को चेन करके किसी भी फ़ाइल में रन किए गए स्क्रिप्ट के आउटपुट को रीडायरेक्ट कर सकते हैं।


अधिक जटिल तर्क के लिए, बजाय एक कंसोल आदेश लिखना (https://laravel.com/docs/5.1/artisan#writing-commands) और command() का उपयोग करें - इस तरह से आप अच्छा, परीक्षण योग्य कोड लिख सकते हैं।

+0

से असहमत होने के लिए स्वतंत्र महसूस करें मैंने अपनी लाइन '$ शेड्यूल-> कमांड ('curl http://wttr.in/01851')->everyMinute()->sendOutputTo(public_path().// अपडेट किया है कार्य/log.txt ');' और मेरा लॉग निर्यात करने के लिए मिलता है, लेकिन मेरे log.txt में, मैं इसे खोलता हूं, मुझे लगता है कि '[अवैध अर्ग्यूमेंट अपवाद] कमांड "कर्ल" परिभाषित नहीं किया गया है। 'मैंने संभवतः गलत क्या किया? – ihue

+0

@ihue कमांड फ़ंक्शन कारीगर आदेश चलाता है, ओएस – tam5

+0

पर टर्मिनल कमांड नहीं, जैसा कि मैंने उपरोक्त लिखा है, 'आदेश' ('' – nXu

2

यदि आप ईवेंट की शेड्यूलिंग का परीक्षण करना चाहते हैं तो आप इस उदाहरण का उपयोग कर सकते हैं। यह डिफ़ॉल्ट रूप पर आधारित है को प्रेरित आदेश:

public function testIsAvailableInTheScheduler() 
{ 
    /** @var \Illuminate\Console\Scheduling\Schedule $schedule */ 
    $schedule = app()->make(\Illuminate\Console\Scheduling\Schedule::class); 

    $events = collect($schedule->events())->filter(function (\Illuminate\Console\Scheduling\Event $event) { 
     return stripos($event->command, 'YourCommandHere'); 
    }); 

    if ($events->count() == 0) { 
     $this->fail('No events found'); 
    } 

    $events->each(function (\Illuminate\Console\Scheduling\Event $event) { 
     // This example is for hourly commands. 
     $this->assertEquals('0 * * * * *', $event->expression); 
    }); 
} 
0

Michiel के जवाब पर बिल्डिंग, मैं तरीकों Illuminate\Console\Scheduling\Event में निहित है, तो घटना किसी निश्चित तिथि के लिए चलाने की वजह से है का उपयोग किया है परीक्षण करने के लिए।

मैंने Carbon::setTestNow() का उपयोग कर वर्तमान दिनांक का मज़ाक उड़ाया है ताकि when() और skip() फ़िल्टर में कोई भी दिनांक आधारित तर्क अपेक्षित व्यवहार करेगा।

use Tests\TestCase; 
use Illuminate\Console\Scheduling\Schedule; 
use Illuminate\Console\Scheduling\Event; 

use Cron\CronExpression; 
use Carbon\Carbon; 


class ScheduleTest extends TestCase { 


    public function testCompanyFeedbackSchedule() 
    { 
     $event = $this->getCommandEvent('your-command-signature'); 

     $test_date = Carbon::now()->startOfDay()->addHours(8); 

     for ($i=0; $i < 356; $i++) { 
      $test_date->addDay(); 
      Carbon::setTestNow($test_date); 

      // Run the when() & skip() filters 
      $filters_pass = $event->filtersPass($this->app); 
      // Test that the Cron expression passes 
      $date_passes = $this->isEventDue($event); 
      $will_run = $filters_pass && $date_passes; 

      // Should only run on first friday of month 
      if ($test_date->format('l') === 'Friday' && $test_date->weekOfMonth === 1) { 
       $this->assertTrue($will_run, 'Task should run on '. $test_date->toDateTimeString()); 
      } else { 
       $this->assertFalse($will_run, 'Task should not run on '. $test_date->toDateTimeString()); 
      } 
     } 
    } 


    /** 
    * Get the event matching the given command signature from the scheduler 
    * 
    * @param string $command_signature 
    * 
    * @return Illuminate\Console\Scheduling\Event 
    */ 
    private function getCommandEvent($command_signature) 
    { 
     $schedule = app()->make(Schedule::class); 

     $event = collect($schedule->events())->first(function (Event $event) use ($command_signature) { 
      return stripos($event->command, $command_signature); 
     }); 

     if (!$event) { 
      $this->fail('Event for '. $command_signature .' not found'); 
     } 

     return $event; 
    } 


    /** 
    * Determine if the Cron expression passes. 
    * 
    * Copied from the protected method Illuminate\Console\Scheduling\[email protected] 
    * 
    * @return bool 
    */ 
    private function isEventDue(Event $event) 
    { 
     $date = Carbon::now(); 

     if ($event->timezone) { 
      $date->setTimezone($event->timezone); 
     } 

     return CronExpression::factory($event->expression)->isDue($date->toDateTimeString()); 
    } 
} 
संबंधित मुद्दे