QUEUE_CONNECTION =database
php artisan make:job CalculateDataJob
<?php
namespace App \Jobs ;
use Illuminate \Bus \Queueable ;
use Illuminate \Contracts \Queue \ShouldBeUnique ;
use Illuminate \Contracts \Queue \ShouldQueue ;
use Illuminate \Foundation \Bus \Dispatchable ;
use Illuminate \Queue \InteractsWithQueue ;
use Illuminate \Queue \SerializesModels ;
class CalculateDataJob implements ShouldQueue
{
use Dispatchable , InteractsWithQueue , Queueable , SerializesModels ;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct ()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle ()
{
for ($ x = 1 ; $ x <= 10 ; $ x ++) {
sleep (2 );
}
return 0 ;
}
}
Step5 - Create Command DemoCron
php artisan make:command DemoCron
<?php
namespace App \Console \Commands ;
use Illuminate \Console \Command ;
use Illuminate \Support \Facades \Http ;
use App \Models \User ;
use App \Jobs \CalculateDataJob ;
class DemoCron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $ signature = 'demo:cron ' ;
/**
* The console command description.
*
* @var string
*/
protected $ description = 'Example cron ' ;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct ()
{
parent ::__construct ();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle ()
{
CalculateDataJob ::dispatch ();
return 0 ;
}
}
Step5 - Run Queues and Command