Why BroadCastEvent are queued in Laravel? How to stop that? - php

I am working on the project which need to broadcast latitude and longitude on realtime
I have something like below
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Redis;
class TrackersBroadcast extends Event implements ShouldBroadcast
{
public $lat, $lng,$imei,$date_time
use SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(
$lat,
$lng,
$imei,
$date_time
)
{
$this->lat = $lat;
$this->lng = $lng;
$this->imei = $imei;
$this->date_time = $date_time;
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return ['tracker-channel'];
}
}
In some case I need to trigger real time email , so I decided to implement laravel message queue like below
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mail;
class SendAlertEmail extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public $data;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($data)
{
$this->data=$data;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
//
Mail::send('emails.test', ['testVar' => $this->data], function($message) {
$message->from('no-reply#sample.com', 'Website Name');
$message->to('developer#gmail.com')->subject('A simple test');
});
}
}
whenever I run php artisan queue:listen database it will queue my broadcasting event too . I dont want to queue the broadcast event . How to do that?

Because Laravel Event Broadcasting queued by default if you extend ShouldBroadcast interface. If you don't want Event Broadcasting queued, you should extend ShouldBroadcastNow interface.
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class TrackersBroadcast implements ShouldBroadcastNow
{
......
}
So It means your Event Broadcasting will using sync queue driver.

In Laravel all event broadcasting is queued by default.
Before broadcasting events, you will also need to configure and run a queue listener. All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected.
In you case you gonna need two queue drivers. One for event broadcasting with real time support. One for handling emails with queue support. For this you gonna need two queue drivers. You can define them in config/queue.php
In your SendAlertEmail class do this.
public $broadcastQueue = 'queue-name-for-handling-emails';
In your TrackersBroadcast state the real time queue. This is your redis queue driver.
public $broadcastQueue = 'real-time-queue';
Refer Broadcast queue under Defining broadcast events
Then you can listen for your two queues like this
$php artisan queue:listen --queue="queue-name-for-handling-emails"
$php artisan queue:listen --queue="real-time-queue"
Refer Why event broadcast is queued ?
Hope you find this useful.

May be you can mention queue name in broadcast event like this and don't listen that queue incase you don't need to process
//php artisan queue:listen --queue=broadcast-queue
/**
* #return string
*/
public function onQueue()
{
return 'broadcast-queue';
}

Related

Laravel job pushed to queue event

In Laravel documentation there is Queue::before and Queue::after events. What I'm looking for is "job pushed to queue" or "job created" event, something like Queue::pushed. Is that exists or how can I trigger that event?
In this answer I saw JobPushed event but it's from Laravel Horizon. Is there a way without Horizon?
Queue::before(function (JobProcessing $event) {
// $event->connectionName
// $event->job
// $event->job->payload()
});
Queue::after(function (JobProcessed $event) {
// $event->connectionName
// $event->job
// $event->job->payload()
});
you can use your job class cunstructor function as a job created event. a job object constructor is executed when you create a new instance of it. but other functions (for example: handle() and ...) would execute when job is running in queue.
also for something near job pushed to queue you can override the dispatch function of you job class. job objects have an static function dispatch that pushes a new job object into queue.
so your edited job class would be like these:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SampleJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
$this->created();
// rest of code
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
}
public static function dispatch($job)
{
SampleJob::dispatch($job);
$job->pushedToQueue();
}
// events
public function created()
{
// event codes
}
public function pushedToQueue()
{
// event codes
}
}

Laravel scheduler run queue

Hello i have a laravel queue for save operations to do later
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use SumoCoders\Teamleader;
use Illuminate\Notifications\Messages\SlackMessage;
use Monolog\Handler\Slack;
use Illuminate\Support\Facades\Mail;
/**
* Class ProcessNotifications
*
* #package App\Jobs
* Worker for send the notifications slack / mail / teamleader in asincr way
*/
class ProcessNotifications implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $arrayDataNotification ;
protected $typeNotification;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($arrayDataNotification , $typeNotification)
{
$this->typeNotification = $typeNotification;
$this->arrayDataNotification = $arrayDataNotification;
\Log::debug('createNotif',$arrayDataNotification);
}
/**
* Execute the job.
*
* #return void
*/
public function handle($arrayDataNotification , $typeNotification)
{
//first get type of notification
\Log::debug('debug handle ',$this);
\Log::info('into handle '.$this->typeNotification);
if($this->typeNotification=='mail'){
//mail internal
}
if ($this->typeNotification=='slack'){
//notifications slack
}
if($this->typeNotification=='teamleader'){
//teamleader connection
}
}
}
For send a a new job to the queue i am using dispatch method :
$this->dispatch(new ProcessNotifications(['data1', 'data2', 'data3'], 'slack'));
I have all in ddbb in the job table , then params are ok
i setted my crontab by run schedule:run each 5 minutes, is launched ok , but on method schedule , when the method handle is called , the params are lost , and i have this in the function scheduler:
protected function schedule(Schedule $schedule)
{
Log::debug('running scheduler '.date("d-m-Y H:i:s"));
$schedule->job(ProcessNotifications::dispatch());
}
Then , the params in this point is lost, same if i run in console php artisan queue work i have :
Too few arguments to function App\Jobs`\ProcessNotifications::__construct()`
in my ddbb i have all params, but i dont know how recover or if this is the good way to call the queue ?
Ok i found, parameters under function handle not need parameters, this is serialized / unserialized automatically for the queue

BroadcastOn not firing in laravel 5.3?

So, I am trying to make use of the broadcasting functionality in laravel to broadcast notifications on certain events.
<?php
namespace Uppdragshuset\AO\Tenant\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class ImportSuccess implements ShouldBroadcast
{
use InteractsWithSockets, SerializesModels;
public $job_id;
public $patent_id;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($job_id, $patent_id)
{
$this->job_id = $job_id;
$this->patent_id = $patent_id;
}
/**
* Get the channels the event should broadcast on.
*
* #return Channel|array
*/
public function broadcastOn()
{
dd(5);
return [];
}
}
I have tried dying and dumping even tried logging using the Log facade but the broadcastOn method is not firing. I am not sure what I am doing wrong here? I am coming from an old version of laravel and updated it recently so I installed the BroadcastServiceProvider manually and registered it in the app.php too. So what am I missing?
I can use Pusher class to fire using the Pusher class but not via broadcasting.

Laravel database queuing jobs attempts

I implemented a laravel queue with the database driver, in an Ubuntu 14.04 server. I execute this code
php /path to app/artisan queue:listen --tries=3 --env=local
It says tries=3. But when I see the jobs table I see jobs with 22 attempts, how is this possible? it should try 3 times and then add it to failed_jobs table.
Also, what does reserved_at means in the jobs table?.
Thank you
Here is the job that, by the way, it works perfectly
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Notifiers\Im_Notification;
use App\Notifiers\Notification;
use App\Notifiers\Mail_Notification;
use App\Reservation;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class NotifyPlaceReservationStatus extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
protected $notification;
protected $reservation;
protected $info_changed;
public function __construct(Notification $notification,Reservation $reservation)
{
$this->reservation = $reservation;
$this->notification = $notification;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$this->notification->notifyPlaceReservationStatus($this->reservation);
}
public function failed()
{
error_log('Job failed');
}
}
When you provide the number of tries in the CLI, Laravel worker is going to apply the tries limit only for those jobs currently in the queue (or in your unexecuted job table if you prefer). In order to make every execution of a job have a try limit, you should put a public $tries attribute in your Job class as stated in the Laravel documentation on Dispatching Jobs (for Laravel 5.4)
public $tries = 3;
protected $notification;
protected $reservation;
protected $info_changed;

Laravel add multiple Jobs at a time

For a user action, I need to send email to all of his subscribers. In this case, the emails should be queued to send later.
I used jobs for that, which can accept single user instance at a time (followed Laravel Doc) and it inserts a job in job table. This is fine.
Now, as the subscribers number is more that one, how can I add multiple user instance or jobs at a time in the jobs table? In Laravel 5.2, how can I achieve that?
I'm not sure if i'm missing something here by reading your question, but if you are implementing your own job queue, couldnt you just change the constructor to accept an collection (array) of users instead and in the handle method simply run a foreach which would email them?
Example in Laravel docs modified to accept a collection of users instead of single user:
<?php
namespace App\Jobs;
use App\User;
use App\Jobs\Job;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendReminderEmail extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $users = [];
/**
* Create a new job instance.
*
* #param User $user
* #return void
*/
public function __construct($users) //Pass in an array of your user objects
{
$this->users = $users;
}
/**
* Execute the job.
*
* #param Mailer $mailer
* #return void
*/
public function handle(Mailer $mailer)
{
foreach($users as $currentUser){
$mailer->send('emails.reminder', ['user' => $currentUser], function ($){
//code here
});
$currentUser->reminders()->create(...);
}
}
}

Categories