Skip to content

Changed failed_at field as ISODate #2607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/Queue/Failed/MongoFailedJobProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Exception;
use Illuminate\Queue\Failed\DatabaseFailedJobProvider;

use MongoDB\BSON\UTCDateTime;
use function array_map;

class MongoFailedJobProvider extends DatabaseFailedJobProvider
Expand All @@ -24,13 +25,15 @@ class MongoFailedJobProvider extends DatabaseFailedJobProvider
*/
public function log($connection, $queue, $payload, $exception)
{
$this->getTable()->insert([
'connection' => $connection,
'queue' => $queue,
'payload' => $payload,
'failed_at' => Carbon::now()->getTimestamp(),
'exception' => (string) $exception,
]);
$this->getTable()->insert(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you revert this unrelated style changes please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used phpcs.xml.dist in the package exactly this time. I hope everything is ok.

./vendor/bin/phpcbf --standard=phpcs.xml.dist src/Queue/Failed/MongoFailedJobProvider.php 
./vendor/bin/phpcbf --standard=phpcs.xml.dist tests/QueueTest.php

[
'connection' => $connection,
'queue' => $queue,
'payload' => $payload,
'failed_at' => new UTCDateTime(Carbon::now()),
'exception' => (string) $exception,
]
);
}

/**
Expand All @@ -42,11 +45,13 @@ public function all()
{
$all = $this->getTable()->orderBy('_id', 'desc')->get()->all();

$all = array_map(function ($job) {
$job['id'] = (string) $job['_id'];
$all = array_map(
function ($job) {
$job['id'] = (string) $job['_id'];

return (object) $job;
}, $all);
return (object) $job;
}, $all
);

return $all;
}
Expand All @@ -62,7 +67,7 @@ public function find($id)
{
$job = $this->getTable()->find($id);

if (! $job) {
if (!$job) {
return;
}

Expand Down
50 changes: 36 additions & 14 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Str;
use Mockery;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Laravel\Queue\Failed\MongoFailedJobProvider;
use MongoDB\Laravel\Queue\MongoJob;
use MongoDB\Laravel\Queue\MongoQueue;
Expand All @@ -31,9 +32,11 @@ public function testQueueJobLifeCycle(): void
{
$uuid = Str::uuid();

Str::createUuidsUsing(function () use ($uuid) {
return $uuid;
});
Str::createUuidsUsing(
function () use ($uuid) {
return $uuid;
},
);

$id = Queue::push('test', ['action' => 'QueueJobLifeCycle'], 'test');
$this->assertNotNull($id);
Expand All @@ -42,17 +45,21 @@ public function testQueueJobLifeCycle(): void
$job = Queue::pop('test');
$this->assertInstanceOf(MongoJob::class, $job);
$this->assertEquals(1, $job->isReserved());
$this->assertEquals(json_encode([
'uuid' => $uuid,
'displayName' => 'test',
'job' => 'test',
'maxTries' => null,
'maxExceptions' => null,
'failOnTimeout' => false,
'backoff' => null,
'timeout' => null,
'data' => ['action' => 'QueueJobLifeCycle'],
]), $job->getRawBody());
$this->assertEquals(
json_encode(
[
'uuid' => $uuid,
'displayName' => 'test',
'job' => 'test',
'maxTries' => null,
'maxExceptions' => null,
'failOnTimeout' => false,
'backoff' => null,
'timeout' => null,
'data' => ['action' => 'QueueJobLifeCycle'],
]
), $job->getRawBody()
);

// Remove reserved job
$job->delete();
Expand Down Expand Up @@ -185,4 +192,19 @@ public function testQueueDeleteAndRelease(): void

$mock->deleteAndRelease($queue, $job, $delay);
}

public function testFailedJobLogging()
{
Carbon::setTestNow('2019-01-01 00:00:00');
$provider = app('queue.failer');
$provider->log('test_connection', 'test_queue', 'test_payload', new \Exception('test_exception'));

$failedJob = Queue::getDatabase()->table(Config::get('queue.failed.table'))->first();

$this->assertSame('test_connection', $failedJob['connection']);
$this->assertSame('test_queue', $failedJob['queue']);
$this->assertSame('test_payload', $failedJob['payload']);
$this->assertEquals(new UTCDateTime(now()), $failedJob['failed_at']);
$this->assertStringStartsWith('Exception: test_exception in ', $failedJob['exception']);
}
}