Skip to content

Commit 3de8412

Browse files
committed
Add getUTCDateTime using Carbon::now
1 parent a56dd57 commit 3de8412

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/Bus/MongoBatchRepository.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use MongoDB\Operation\FindOneAndUpdate;
2121
use Override;
2222

23-
use function date_default_timezone_get;
2423
use function is_string;
2524
use function serialize;
2625
use function unserialize;
@@ -66,6 +65,9 @@ public function find(string $batchId): ?Batch
6665
$batch = $this->collection->findOne(
6766
['_id' => $batchId],
6867
[
68+
// If the select query is executed faster than the database replication takes place,
69+
// then no batch is found. In that case an exception is thrown because jobs are added
70+
// to a null batch.
6971
'readPreference' => new ReadPreference(ReadPreference::PRIMARY),
7072
'typeMap' => ['root' => 'array', 'array' => 'array', 'document' => 'array'],
7173
],
@@ -85,7 +87,7 @@ public function store(PendingBatch $batch): Batch
8587
'failed_job_ids' => [],
8688
// Serialization is required for Closures
8789
'options' => serialize($batch->options),
88-
'created_at' => new UTCDateTime(Carbon::now()),
90+
'created_at' => $this->getUTCDateTime(),
8991
'cancelled_at' => null,
9092
'finished_at' => null,
9193
]);
@@ -161,7 +163,7 @@ public function markAsFinished(string $batchId): void
161163
$batchId = new ObjectId($batchId);
162164
$this->collection->updateOne(
163165
['_id' => $batchId],
164-
['$set' => ['finished_at' => new UTCDateTime(Carbon::now())]],
166+
['$set' => ['finished_at' => $this->getUTCDateTime()]],
165167
);
166168
}
167169

@@ -173,8 +175,8 @@ public function cancel(string $batchId): void
173175
['_id' => $batchId],
174176
[
175177
'$set' => [
176-
'cancelled_at' => new UTCDateTime(Carbon::now()),
177-
'finished_at' => new UTCDateTime(Carbon::now()),
178+
'cancelled_at' => $this->getUTCDateTime(),
179+
'finished_at' => $this->getUTCDateTime(),
178180
],
179181
],
180182
);
@@ -194,16 +196,14 @@ public function transaction(Closure $callback): mixed
194196
return $this->connection->transaction(fn () => $callback());
195197
}
196198

197-
/**
198-
* Rollback the last database transaction for the connection.
199-
*/
199+
/** Rollback the last database transaction for the connection. */
200200
#[Override]
201201
public function rollBack(): void
202202
{
203203
$this->connection->rollBack();
204204
}
205205

206-
/** Mark the batch that has the given ID as finished. */
206+
/** Prune the entries older than the given date. */
207207
#[Override]
208208
public function prune(DateTimeInterface $before): int
209209
{
@@ -258,13 +258,19 @@ protected function toBatch($batch): Batch
258258
);
259259
}
260260

261+
private function getUTCDateTime(): UTCDateTime
262+
{
263+
// Using Carbon so the current time can be modified for tests
264+
return new UTCDateTime(Carbon::now());
265+
}
266+
261267
/** @return ($date is null ? null : CarbonImmutable) */
262268
private function toCarbon(?UTCDateTime $date): ?CarbonImmutable
263269
{
264270
if ($date === null) {
265271
return null;
266272
}
267273

268-
return CarbonImmutable::createFromTimestamp((string) $date, date_default_timezone_get());
274+
return CarbonImmutable::createFromTimestampMsUTC((string) $date);
269275
}
270276
}

0 commit comments

Comments
 (0)