Alireza
Alireza

Reputation: 215

Cannot run jobs in asynchronous in laravel queue

I'm want to run some tasks asynchronously in php 5.5 and laravel. I've googled and found out that the laravel queue can do such thing. I found this link for help : https://laravel.com/docs/5.1/queues. I've installed the dependencies :

Amazon SQS: aws/aws-sdk-php ~3.0

Beanstalkd: pda/pheanstalk ~3.0

IronMQ: iron-io/iron_mq ~2.0|~4.0

Redis: predis/predis ~1.0

and I've added the queue tables in laravel.Here is my Job class

class SearchFromSource extends Job implements SelfHandling{
    use InteractsWithQueue, SerializesModels;
    private $provider;
    private $query;
    private $pageToken;
    private $parameters;
    private $needDetails;

    public function __construct()
    {
    }

    public function handle()
    {
        sleep(5);
    }
}

and here is where I dispatch the job:

class SearchController extends Controller{
     ...
     public function prepareResults($query, $page, $ip){
        $job = (new SearchFromSource($curVideoProvider,$this->query,$curVideoProvider->getDefaultParameters(),$curPageToken,$curVideoProvider->needDetails()))->onQueue("q1");
        $this->dispatch($job);
        $job2 = (new SearchFromSource($curVideoProvider,$this->query,$curVideoProvider->getDefaultParameters(),$curPageToken,$curVideoProvider->needDetails()))->onQueue("q2");
        $this->dispatch($job2);
    }

}

The problem is that the laravel doesn't run the jobs asynchronous. What is the problem.

Upvotes: 1

Views: 1455

Answers (1)

Gkalligeros
Gkalligeros

Reputation: 48

  1. First you must define a QUEUE_DRIVER (sync,redis,beanstalk etc) in you .env file
  2. Then you must have a a queue listener running you run php artisan queue:listen inside you project directory
  3. Your job should Implement shouldQUeue interface

Upvotes: 1

Related Questions