Skip to content

Commit 0e1a85d

Browse files
committed
more changes to the handler and interface
1 parent 76b8df9 commit 0e1a85d

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

src/Commands/QueueForget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function run(array $params)
5656
return EXIT_ERROR;
5757
}
5858

59-
if (service('queue')->forget($id, true)) {
59+
if (service('queue')->forget($id)) {
6060
CLI::write(sprintf('Failed job with ID %s has been removed.', $id), 'green');
6161
} else {
6262
CLI::write(sprintf('Could not find the failed job with ID %s', $id), 'red');

src/Config/Queue.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class Queue extends BaseConfig
2626
public array $database = [
2727
'dbGroup' => 'default',
2828
'getShared' => true,
29-
'table' => 'queue_jobs',
3029
];
3130

3231
/**

src/Handlers/DatabaseHandler.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function __construct(protected QueueConfig $config)
2323
{
2424
$connection = db_connect($config->database['dbGroup'], $config->database['getShared']);
2525
$this->jobModel = model(QueueJobModel::class, true, $connection);
26-
$this->jobModel->setTable($config->database['table']);
2726
}
2827

2928
/**
@@ -55,7 +54,17 @@ public function push(string $queue, string $job, array $data): bool
5554
*/
5655
public function pop(string $queue): ?QueueJob
5756
{
58-
return $this->jobModel->getFromQueue($queue);
57+
$queueJob = $this->jobModel->getFromQueue($queue);
58+
59+
if ($queueJob === null) {
60+
return null;
61+
}
62+
63+
// Set the actual status as in DB.
64+
$queueJob->status = Status::RESERVED->value;
65+
$queueJob->syncOriginal();
66+
67+
return $queueJob;
5968
}
6069

6170
/**
@@ -96,11 +105,13 @@ public function failed(QueueJob $queueJob, Throwable $err, bool $keepJob): bool
96105

97106
/**
98107
* Change job status to DONE od delete it.
108+
*
109+
* @throws ReflectionException
99110
*/
100111
public function done(QueueJob $queueJob, bool $keepJob): bool
101112
{
102113
if ($keepJob) {
103-
return $this->jobModel->update($queueJob->id, ['status' => Status::DONE]);
114+
return $this->jobModel->update($queueJob->id, ['status' => Status::DONE->value]);
104115
}
105116

106117
return $this->jobModel->delete($queueJob->id);
@@ -120,6 +131,9 @@ public function clear(?string $queue = null): bool
120131

121132
/**
122133
* Retry failed job.
134+
* ∂
135+
*
136+
* @throws ReflectionException
123137
*/
124138
public function retry(?int $id, ?string $queue): int
125139
{
@@ -145,17 +159,24 @@ public function retry(?int $id, ?string $queue): int
145159
/**
146160
* Delete failed job by ID.
147161
*/
148-
public function forget(int $id, bool $affectedRows = false): bool
162+
public function forget(int $id): bool
149163
{
150-
return model(QueueJobFailedModel::class)->delete($id)
151-
&& (! $affectedRows || model(QueueJobFailedModel::class)->affectedRows() > 0);
164+
if (model(QueueJobFailedModel::class)->delete($id)) {
165+
return model(QueueJobFailedModel::class)->affectedRows() > 0;
166+
}
167+
168+
return false;
152169
}
153170

154171
/**
155172
* Delete many failed jobs at once.
156173
*/
157174
public function flush(?int $hours, ?string $queue): bool
158175
{
176+
if ($hours === null && $queue === null) {
177+
return model(QueueJobFailedModel::class)->truncate();
178+
}
179+
159180
return model(QueueJobFailedModel::class)
160181
->when(
161182
$hours !== null,

src/Interfaces/QueueInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public function done(QueueJob $queueJob, bool $keepJob);
1919

2020
public function clear(?string $queue = null);
2121

22-
public function retry(?int $id, ?string $queue);
22+
public function retry(?int $id, ?string $queue);
2323

24-
public function forget(int $id, bool $affectedRows = false);
24+
public function forget(int $id);
2525

2626
public function flush(?int $hours, ?string $queue);
2727

src/Models/QueueJobModel.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class QueueJobModel extends Model
1212
{
13-
protected $table = '';
13+
protected $table = 'queue_jobs';
1414
protected $primaryKey = 'id';
1515
protected $useAutoIncrement = true;
1616
protected $returnType = QueueJob::class;
@@ -37,8 +37,12 @@ class QueueJobModel extends Model
3737
*/
3838
public function getFromQueue(string $name): ?QueueJob
3939
{
40-
// Make sure we still have the connection
41-
$this->db->reconnect();
40+
// For SQLite3 memory database this will cause problems
41+
// so check if we're not in the testing environment first.
42+
if (ENVIRONMENT !== 'testing' && $this->db->database !== ':memory:') {
43+
// Make sure we still have the connection
44+
$this->db->reconnect();
45+
}
4246
// Start transaction
4347
$this->db->transStart();
4448

@@ -53,6 +57,9 @@ public function getFromQueue(string $name): ?QueueJob
5357
->getCompiledSelect();
5458

5559
$query = $this->db->query($this->skipLocked($sql));
60+
if ($query === false) {
61+
return null;
62+
}
5663
/** @var QueueJob $row */
5764
$row = $query->getCustomRowObject(0, QueueJob::class);
5865

0 commit comments

Comments
 (0)