-
Notifications
You must be signed in to change notification settings - Fork 1.4k
How to save the failed_at field as ISODate? #2605
-
Hi, Before telling the solution, I must say that if the configuration considered for failed.driver in the queue.php file is database-uuids by default, the value of the failed_at field in Mongo is an empty object and no date is stored in it.
But if we change the driver to mongodb, you will see that the date is stored as a string of the timestamp value in the failed_at field. And this was not what I expected and I wanted the date to be stored as an ISODate.
However, to save the date in the failed_at field as ISODate, you can take the following steps.
Step 1:
Run the following command
php artisan make:provider CustomMongodbQueueServiceProvider
Then rewrite the CustomMongodbQueueServiceProvider.php file as below:
<?php namespace App\Providers; use Jenssegers\Mongodb\MongodbQueueServiceProvider; class CustomMongodbQueueServiceProvider extends MongodbQueueServiceProvider { /** * Create a new MongoDB failed job provider. */ protected function mongoFailedJobProvider(array $config): CustomMongoFailedJobProvider { return new CustomMongoFailedJobProvider($this->app['db'], $config['database'], $config['table']); } }
Step 2:
Run the following command :
php artisan make:provider CustomMongoFailedJobProvider
Then rewrite the CustomMongoFailedJobProvider.php file as below:
<?php namespace App\Providers; use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider; class CustomMongoFailedJobProvider extends MongoFailedJobProvider { public function log($connection, $queue, $payload, $exception): void { $failed_at = new \MongoDB\BSON\UTCDateTime(now()); $exception = (string) $exception; $this->getTable()->insert(compact('connection', 'queue', 'payload', 'failed_at', 'exception')); } }
I think it is generally clear what we did, a simple override of two service providers related to MongoFailedJobProvider, MongodbQueueServiceProvider through which we can rewrite the log method according to our needs.
Step 3:
Now we have to register our service provider in the config/app.php file :
'providers' => [ // Other Service Providers ... App\Providers\CustomMongodbQueueServiceProvider::class, ],
Step 4:
Then run the following command :
composer dump-autoload
Well, our work is finished, now you can test the result. I hope this solution helped you.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
Reading the laravel code, I see that the type of the failed_at field depends on the database type. With SQL databases, it's a datetime. With DynamoDB, it's a timestamp. For MongoDB, it was implemented in #792 with a timestamp.
laravel-mongodb/src/Queue/Failed/MongoFailedJobProvider.php
Lines 25 to 34 in 9bc8999
I agree that an UTCDateTime should be used. Would you like to submit a PR?
Also, it looks like the Job Batching feature implemented in Laravel 8.x needs some update.
Beta Was this translation helpful? Give feedback.
All reactions
-
yes I agree.
Beta Was this translation helpful? Give feedback.