How to use job(queue) with redis server in laravel 5.2:
For using jobs in Laravel 5.2 with redis server, we have to do following steps:
1. First to install redis server on your machine for this, run this command on your terminal
sudo apt install redis-server
It will install your redis server. For start redis server run this command
redis-server
2. Now install the dependency in your project. For this write this in your composer.json
predis/predis ~1.0
and update composer via
composer update
3. Now do some settings
(a) In .env file change QUEUE_DRIVER=sync to QUEUE_DRIVER=redis
(b) In config/queue.php file change 'default' => env('QUEUE_DRIVER', 'redis')
(c) In config/session.php file change 'driver' => env('SESSION_DRIVER', 'redis')
4. Now run this command on terminal for create a new queue
php artisan make:job InsertFeedData
5. Now go to this file job/InsertFeedData.php and write your code in handle function which you want to run as a job.
class InsertFeedData extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $user;
protected $video;
protected $type;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($user, $video, $type)
{
$this->user = $user;
$this->video = $video;
$this->type = $type;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$followers = Follower::where('user_id', $this->user)->pluck('follower_id');
Feed::create(['video_id' => $this->video, 'follower_id' => $this->user, 'type' => $this->type]);
foreach ($followers as $follower) {
$feed = Feed::create(['video_id' => $this->video, 'follower_id' => $follower, 'type' => $this->type]);
print_r($feed);
}
}
}
6. Now you can use this class in your controller via
7. You can check it via running this command on terminal
php artisan queue:listen
Thanks
For using jobs in Laravel 5.2 with redis server, we have to do following steps:
1. First to install redis server on your machine for this, run this command on your terminal
sudo apt install redis-server
It will install your redis server. For start redis server run this command
redis-server
2. Now install the dependency in your project. For this write this in your composer.json
predis/predis ~1.0
and update composer via
composer update
3. Now do some settings
(a) In .env file change QUEUE_DRIVER=sync to QUEUE_DRIVER=redis
(b) In config/queue.php file change 'default' => env('QUEUE_DRIVER', 'redis')
(c) In config/session.php file change 'driver' => env('SESSION_DRIVER', 'redis')
4. Now run this command on terminal for create a new queue
php artisan make:job InsertFeedData
5. Now go to this file job/InsertFeedData.php and write your code in handle function which you want to run as a job.
class InsertFeedData extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $user;
protected $video;
protected $type;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($user, $video, $type)
{
$this->user = $user;
$this->video = $video;
$this->type = $type;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$followers = Follower::where('user_id', $this->user)->pluck('follower_id');
Feed::create(['video_id' => $this->video, 'follower_id' => $this->user, 'type' => $this->type]);
foreach ($followers as $follower) {
$feed = Feed::create(['video_id' => $this->video, 'follower_id' => $follower, 'type' => $this->type]);
print_r($feed);
}
}
}
6. Now you can use this class in your controller via
$this->dispatch(new InsertFeedData(Utilities::getUserId(), $video->id, 'share'));
7. You can check it via running this command on terminal
php artisan queue:listen
Thanks