[IntermediateLaravel] 11-Dispatching-Jobs

Source: Internet
Author: User
[IntermediateLaravel] 11-Dispatching-Jobs

We have learned Events and Listeners before. this time we will learn Jobs.

Jobs creation

Run the command line php artisan make: job CompileReports to create a Job class. The structure of the Job class is simple. Generally, only one handle method is included for the queue to call the job. The code is as follows:

class CompileReports extends Job implements SelfHandling{   /**   * Create a new job instance.   *   * @return void   */  public function __construct()  {    //  }   /**   * Execute the job.   *   * @return void   */  public function handle()  {    //  }} 

You can use php artisan help make: job to observe relevant parameters and execute php artisan make: job CompileReports -- queued. The comparison shows that the ShouldQueue interface is implemented in many differences from the previous class, this means that it can be executed in the queue order.

Add route and controller

Add the following to the default route routes. php:

Route::get('reports', 'ReportsController@index'); 

Run the command line php artisan make: controller ReportsController -- plain to create the ReportsController controller and add the test method:

class ReportsController extends Controller{    //  public function index()  {    $job = new CompileReports();    $this->dispatch($job);    return 'Done';  } } 

In the Controller parent class of the ReportsController, DispatchesJobs is already used by default. The dispatch ($ job) method is defined to schedule tasks. In this case, the page access effect is as follows:

Job parameter processing

Rewrite the CompileReports. php code and the constructor accepts the $ reportId parameter:

class CompileReportsextends Jobimplements SelfHandling, ShouldQueue{   protected $reportId;  /**   * Create a new job instance.   *   * @return void   */  public function __construct($reportId)  {    //    $this->reportId = $reportId;  }   /**   * Execute the job.   *   * @return void   */  public function handle()  {    //    var_dump('Compiling the reports with the id '. $this->reportId .' within the Job class.');  }} 

The parameters in the request are accepted in the ReportsController and forwarded to the Job for processing:

class ReportsController extends Controller{  public function index(Request $request)  {    $job = new CompileReports($request->input('reportId'));    $this->dispatch($job);    return 'Done';  }} 

The page access result is as follows:

Multi-parameter processing in a Job

Instead, call the dispatchFrom method in ReportsController:

class ReportsController extends Controller{    //  public function index(Request $request)  {    //$job = new CompileReports($request->input('reportId'));        // $this->dispatchFrom(CompileReports::class, $request);    $this->dispatchFrom('App\Jobs\CompileReports', $request);    return 'Done';  } } 

DispatchesJobs defines dispatch and dispatchFrom, as follows:

Trait DispatchesJobs {/*** Dispatch a job to its appropriate handler. ** @ param mixed $ job * @ return mixed */protected function dispatch ($ job) {return app ('illuminate \ Contracts \ Bus \ Dispatcher ') -> dispatch ($ job);}/*** handle al a job and dispatch it to its appropriate handler. ** @ param mixed $ job * @ param array $ array * @ return mixed */protected function dispatchFromArray ($ job, array $ array) {return app ('illuminate \ Contracts \ Bus \ Dispatcher ')-> dispatchFromArray ($ job, $ array );} /*** handle al a job and dispatch it to its appropriate handler. * sort the parameter list and pass it to job * @ param mixed $ job * @ param \ ArrayAccess $ source * @ param array $ extras * @ return mixed */protected function dispatchFrom ($ job, arrayAccess $ source, $ extras = []) {return app ('illuminate \ Contracts \ Bus \ Dispatcher ')-> dispatchFrom ($ job, $ source, $ extras );}}

In the CompileReports class, rewrite the input parameters:

class CompileReportsextends Jobimplements SelfHandling, ShouldQueue{   protected $reportId;   protected $type;  /**   * Create a new job instance.   *   * @return void   */  public function __construct($reportId, $type)  {    //    $this->reportId = $reportId;    $this->type = $type;  }   /**   * Execute the job.   *   * @return void   */  public function handle()  {    //    var_dump('Compiling the '. $this->type .' reports with the id '. $this->reportId .' within the Job class.');  }} 

The access effect is as follows:

After adding a DispatchesJobs reference to a Job, you can continue to assign a Job:

/*** Execute the job. ** @ return void */public function handle () {// var_dump ('compiling '. $ this-> type. 'Reports with the ID '. $ this-> reportId. 'within the Job class. '); var_dump (sprintf ('compiling the % s report with the id % s within the Job class', $ this-> type, $ this-> reportId )); // after the DispatchesJobs reference is added, you can continue to assign the job if (true) {$ this-> dispatch (new DoSomethingElse );}}

The access effect is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.