Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to save the failed_at field as ISODate? #2605

Closed Unanswered
BehroozBvk asked this question in Show and tell
Discussion options

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.

photo_2023年09月06日_17-01-47

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

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.

public function log($connection, $queue, $payload, $exception)
{
$this->getTable()->insert([
'connection' => $connection,
'queue' => $queue,
'payload' => $payload,
'failed_at' => Carbon::now()->getTimestamp(),
'exception' => (string) $exception,
]);
}

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.

https://jira.mongodb.org/browse/PHPORM-87

You must be logged in to vote
1 reply
Comment options

yes I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /