I cannot change the channel name in the event class. Every change I make in the class is not loaded. (I am using laravel-websockets)
/app/Events/BroadcastingModelEvent
class BroadcastingModelEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new Channel('test');
}
}
/routes/web.php:
Route::get('test', function () {
event(new App\Events\BroadcastingModelEvent('Test'));
return "Event has been sent!";
});
VueComponent
Echo.channel('test')
.listen('BroadcastingModelEvent', (e) => {
console.log(e);
});
So this code works, I receive the event in the console.log everything is ok.
But if I change return new Channel('something'); and Echo.channel('something') it stops working.
I tried php artisan route:clear, php artisan cache:clear, php artisan event:clear and nothing works.I stopped the php artisan websockets:serve process and restarted it. That class only works with the 'test' name I first gave and nothing else.
I cannot figure out where is it caching the class because any change I make in BroadcastingModelEvent is not reflected.
For anyone facing this issue, you need to restart the queue worker process when you edit the events.
Related
I want to add some additional functionality when the queue is finished. I am using the queue::after function in the AppServiceProvider but this function is not being triggered. I have tried many solutions which were provided in the same kind of questions on StackOverflow like restarting the queue worker, clearing the cache and composer dump-autoload but didn't get any help.
public function boot()
{
Queue::after(function (JobProcessed $event) {
\Log::debug("Queue after"); // it should be printed in the logger
});
}
Any idea where am I going wrong?
i got a problem when execute my queue. Job doesnt work.
.env:
QUEUE_CONNECTION=database_name
Job:
use App\Models\ProfessorSuscriptionHistory;
class CheckSuscription implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct()
{
}
public function handle()
{
$suscription_history = new ProfessorSuscriptionHistory();
$suscription_history->user_id = 13;
$suscription_history->type = 'trimestral';
$suscription_history->pdf = 'test.pdf';
$suscription_history->ended_at = Carbon::now()->addMonth(3);
$suscription_history->save();
}
}
My controller what dispatch a Job
use App\Jobs\CheckSuscription;
class TestController extends Controller
{
public function index()
{
CheckSuscription::dispatch()->onQueue('processing');
}
}
i used command php artisan queue:work and php artisan queue:listen but nothing appear in cmd. i migrated jobs to my db using php artisan queue:table and nothing appear in jobs table or failed_jobs table too. How can i fix that? thx!!
Put your QUEUE_CONNECTION=database_name to just QUEUE_CONNECTION=database you dont put the database name you just tell Laravel to use a certain queue. database, redis ...
Queue Connection
I am using Laravel 5.8, I configured an email containing the invoice sent to user after they place an order.
Email is delivered when set QUEUE_DRIVER=sync but when I set it to redis/database and I run the php artisan queue:work redis --tries=5 it shows the queue is processed
Processing: Modules\Checkout\Mail\Invoice
Processed: Modules\Checkout\Mail\Invoice
succesfully and no entry in the failed_jobs either.
CODE:
Modules\Checkout\Jobs\SendInvoiceEmail.php
class SendInvoiceEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function handle()
{
Mail::to($this->order->customer_email)
->send(new Invoice($this->order));
}
}
And this is the Modules\Checkout\Mail\Invoice.php
class Invoice extends Mailable
{
use Queueable, SerializesModels;
public $order;
public function __construct($order)
{
$this->order = $order;
}
public function build()
{
return $this->subject('New order: ', ['id' => $this->order->id]))
->view("emails.invoice");
}
}
Then I am dispatching it in the Controller after Order created
SendInvoiceEmail::dispatch($order);
Can anyone point out if I've missed anything and what I am doing wrong?
I've done the php artisan config:cache and clear:cache restarted the server.
This works just as expected if I set QUEUE_DRIVER=sync in .env instead of QUEUE_DRIVER=redis or database
I had that problem too and solve it putting a key in the config/mail.php file
in mailers['smtp'] I add local_domain equals to my domain (example.com)
The reason is the Swift Mailer class can use this key to put in the HELO field of the email. If you use QUEUE=sync the framework uses the domain in env file, but when you use a different queue it doesn't cache the domain.
Thing is .env or config.php both configurations should work normally.
My main issue was the application would save these settings from .env or config.php to database. then for the next time it looked in database for these smtp configs and anything other than sync tends to fail.
I don't know why it was configured so I removed and the issue was resolved
I would like to send mail to user after creating account on my website and I would like to use queues to send them. I'm using PHP Laravel framework.
My controller handles the request after clicking on "Create account":
class LoginController extends Controller
{
...
public function register(Request $request) {
...
$mail = (new RegisterRequest($user))->onConnection("database")->onQueue("emailsQueue");
Mail::queue($mail);
...
}
}
Then I have this RegisterRequest (mailable) class:
class RegisterRequest extends Mailable
{
use Queueable, SerializesModels;
protected $user;
public function __construct($user)
{
$this->user = $user;
}
public function build()
{
return $this->from('user#example.com')
->to($this->user->email)
->subject("Confirm your Email Address")
->view('emails.register.request')
->with("registration_token", $this->user->registration_token);
}
}
As you can see, I am using relational database to store jobs. And really, after calling LoginController's register method, a job is saved to database. But it can't be processed. I also start php artisan queue:work but nothing is done with jobs in database. Any help?
EDIT:
So I just found out that picking jobs from queue is done by SQL selecting the 'default' queue name. But I'm sending mails to queue 'emailsQueue'. So I'm now running Queue Worker like this: php artisan queue:work --queue=emailsQueue and everything's working fine for now. But how can I pick jobs from every queue in database? It's probably not the best attempt, right? It wouldn't make any sense to have named queues, right? But let's say I have one queue for processing register account requests, another queue for changing password requests and so on... So I think it does make sense to process every queue. So how can I do this? Can it be done just by listing the queues like this?
php artisan queue:work --queue=registerAccountEmailQueue,changePasswordEmailQueue...
What exactly does running php artisan queue:work? I thought it's the command to run all queues.
Use queue driver database.
In controller you should write
$this->dispatch(new SendNotifyMail($data));
This will pass your $data to queue. here SendNotifyMail is used as Job Class. So you should also use this in Controller like use App\Jobs\SendNotifyMail;.
Then create a file in Folder Jobs, named SendNotifyMail
<?php
namespace App\Jobs;
use App\Jobs\Job;
use DB;
use Mail;
use Artisan;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendNotifyMail extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public $timeout = 300; // default is 60sec. You may overwrite like this
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function handle(Mailer $mailer)
{
$data = $this->data; // retrieve your passed data to variable
// your mail code here
}
}
In your command you need to write
php artisan queue:listen
or
php artisan queue:work
Then execute the code.
I am developing a web application using Laravel framework. I am trying to trying to use event and listener in my application. But the event was trigged and the listener for the trigged event is not fired.
This is my controller action
public function store(Request $request)
{
//other code
$item = Item::create($request->all())
broadcast(new ItemCreated($item));
return "Item created successfully";
}
This is my Events\ItemCreated.php
class ItemCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $item;
public function __construct($item)
{
$this->item = $item;
}
}
Then I have a listener for that event.
Listeners/EmailSubscribedUsers.php
class EmailSubscribedUsers
{
public function __construct()
{
//this constructor is not triggered
}
public function handle(ItemCreated $event)
{
//This method is not fired
}
}
In the EventServiceProvider I registered the event and the listener like this
protected $listen = [
ItemCreated::class => [
EmailSubscribedUsers::class
]
];
The event is trigged. But the listener is not fired. Why? What is wrong?
I tried the following solutions.
php artisan optimize
composer dumpautoload
php artisan clear-compiled
Sorry everyone. The issue was I was unit testing. In the unit testing if I used Event::fake(), the event listeners are not triggered. I wanted to tested the logic in the event listeners. Therefore, I removed the Event::fake() and tested the logic in the listener instead.
First of all as pointed in comments use
event(new ItemCreated($item));
and not
broadcast(new ItemCreated($item));
In addition make sure you have set QUEUE_CONNECTION to sync in your .env file. If you used some other connection (for example database or Redis) make sure you run in console command:
php artisan queue:work
The last thing - verify your error log in storage/logs directory. You might have some other errors (for example missing import) and that's why your listener fails.
Also make sure in EventServiceProvider that you use valid classes and imported valid namespaces - otherwise listener won't be triggered.