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?
Related
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.
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.
In an application I am working, I've both Job and Event Listener implemented Should Queue. In the queue, I perform a database insert and I want after the queue complete, I want to remove the previous cache. So I use Queue Job Event like this example:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Queue::after(function (JobProcessed $event) {
Log::info('[QUEUE COMPLETE]', $event->job->getName());
});
}
public function register()
{
//
}
}
But the event is never fired and there is no log found in storage/log folder. I use daily logging channel.
Why is it not logging?
Answering my own question after solving this.
All the code is fine, I just needed to stop the queue:work and start it again (restart). After this, the Queue::after event started to fire and all worked perfectly.
I have a job in my Laravel project (v.4.2). which is used for inserting data into database. The class named "ProductUpdate". I use Amazon SQS for queue service.
What makes me confuse now is, when I changed the code in class "ProductUpdate",
it seems that the job is running by using old version of the class.
I even deleted all lines of code in the class but the jobs can still be able to run ( it stills inserts data).
Following is the job class.
The file of this class is at app/jobs/ProductUpdate.php
In my understanding, job class is the only place that will be called from queue, but why it can still be able to run when I deleted all the codes?
<?php
/**
* Here is a class to run a queued item sent from SQS
* Default method to use is fire()
**/
class ProductUpdate
{
public function fire($job, $data)
{
// Disable query log
DB::connection()->disableQueryLog();
// Set the job as a var so it will be used across functions
$this->job = $job;
$product = Product::find($productID);
if($product->product_type != 18) {
// Call the updater from library
$updater = App::make('Product\PriceUpdater');
$updater->update($product);
}
// Done and delete
$this->success();
}
private function success()
{
$this->job->delete();
}
private function fail($messages = array())
{
Log::error('Job processing fail', $messages);
$this->job->delete();
}
}
Your problem is related to cache.
Run this command in terminal to remove all cached data.
php artisan cache:clear
other way:-
Illuminate\Cache\FileStore has the function flush, so you can also use it:
Cache::flush();
This link will also help you :)
When I run the following method, it returns a collection with soft deletes included...and obviously, it shouldn't.
return $twitter_oauth->get();
I think it might be the boot function in my TwitterOAuth model. I use the boot meth below to soft delete relevant models (works as it should).
public static function boot()
{
TwitterOAuth::deleting(function($twitter_oauth) {
$twitter_oauth->posts()->delete();
});
TwitterOAuth::restoring(function($twitter_oauth) {
$twitter_oauth->posts()->withTrashed()->restore();
});
}
Now if I remove the boot method and run the same get query, soft deletes do not appear in the collection. Weird. Anyone have an experience or run into this issue - or see my problem?
I know I could use whereNull in my queries, but that seems like a hack. There must be a better way...
Needed to include parent::boot(); in my boot method. Solved it.
public static function boot()
{
parent::boot();
TwitterOAuth::deleting(function($twitter_oauth) {
$twitter_oauth->posts()->delete();
});
TwitterOAuth::restoring(function($twitter_oauth) {
$twitter_oauth->posts()->withTrashed()->restore();
});
}