Skip to content

Commit 1579024

Browse files
committed
fix: add support for custom config files in commands
1 parent 9e89b19 commit 1579024

14 files changed

+227
-87
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MIT License
22

33
Copyright (c) 2023 Michal Sniatala
4-
Copyright (c) 2023 CodeIgniter Foundation
4+
Copyright (c) 2023-2025 CodeIgniter Foundation
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

docs/commands.md

+16
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Allows you to consume jobs from a specific queue.
6363
* `-priority` - The priority for the jobs from the queue (comma separated). If not provided explicit, will follow the priorities defined in the config via `$queuePriorities` for the given queue. Disabled by default.
6464
* `-tries` - The number of attempts after which the job will be considered as failed. Overrides settings from the Job class. Disabled by default.
6565
* `-retry-after` - The number of seconds after which the job is to be restarted in case of failure. Overrides settings from the Job class. Disabled by default.
66+
* `-config` - The alternative config file to use. Default value relies on `config('Queue')`. Use namespace to define an alternative, e.g. `Acme\\Config\\Queue`.
6667
* `--stop-when-empty` - Stop when the queue is empty.
6768

6869
##### Example
@@ -75,6 +76,10 @@ It will listen for 5 jobs from the `emails` queue and then stop.
7576

7677
It will work the same as the previous command but will first consume jobs from the `emails` queue that were added with the `low` priority.
7778

79+
php spark queue:work email -config Acme\\Config\\Queue
80+
81+
This is how we would use an alternative config file. However, usually, you will not need to specify it. It's recommended to use only a single config file across your application, **but** if you are building a modular system, you can use [Registrars](https://codeigniter.com/user_guide/general/configuration.html#registrars) to update the queue config file from within your module.
82+
7883
### queue:stop
7984

8085
Allows you to stop a specific queue in a safe way. It does this as soon as the job that is running in the queue is completed.
@@ -83,6 +88,10 @@ Allows you to stop a specific queue in a safe way. It does this as soon as the j
8388

8489
* `queueName` - Name of the queue we will work with.
8590

91+
##### Options
92+
93+
* `-config` - The alternative config file to use. Default value relies on `config('Queue')`.
94+
8695
##### Example
8796

8897
php spark queue:stop emails
@@ -106,6 +115,7 @@ Allows you to view all failed jobs. Also only from a specific queue
106115
##### Options
107116

108117
* `-queue` - Queue name.
118+
* `-config` - The alternative config file to use. Default value relies on `config('Queue')`.
109119

110120
##### Example
111121

@@ -124,6 +134,7 @@ Allows you to retry failed jobs back to the queue.
124134
##### Options
125135

126136
* `-queue` - Queue name.
137+
* `-config` - The alternative config file to use. Default value relies on `config('Queue')`.
127138

128139
##### Example
129140

@@ -139,6 +150,10 @@ Allows you to delete the failed job by ID
139150

140151
* `id` - ID of the failed job.
141152

153+
##### Options
154+
155+
* `-config` - The alternative config file to use. Default value relies on `config('Queue')`.
156+
142157
##### Example
143158

144159
php spark queue:forget 123
@@ -151,6 +166,7 @@ Allows you to delete many failed jobs at once. Based on the failed date and queu
151166

152167
* `-hours` - Number of hours.
153168
* `-queue` - Queue name.
169+
* `-config` - The alternative config file to use. Default value relies on `config('Queue')`.
154170

155171
##### Example
156172

src/Commands/QueueClear.php

+22-10
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@
1313

1414
namespace CodeIgniter\Queue\Commands;
1515

16-
use CodeIgniter\CLI\BaseCommand;
1716
use CodeIgniter\CLI\CLI;
17+
use CodeIgniter\Queue\Config\Queue as QueueConfig;
18+
use CodeIgniter\Queue\Exceptions\QueueException;
1819

19-
class QueueClear extends BaseCommand
20+
class QueueClear extends QueueCommand
2021
{
21-
/**
22-
* The Command's Group
23-
*
24-
* @var string
25-
*/
26-
protected $group = 'Queue';
27-
2822
/**
2923
* The Command's Name
3024
*
@@ -55,6 +49,15 @@ class QueueClear extends BaseCommand
5549
'queueName' => 'Name of the queue we will work with.',
5650
];
5751

52+
/**
53+
* The Command's Options
54+
*
55+
* @var array<string, string>
56+
*/
57+
protected $options = [
58+
'-config' => 'The alternative config file to use. Default value: relies on config(\'Queue\')',
59+
];
60+
5861
/**
5962
* Actually execute a command.
6063
*/
@@ -68,7 +71,16 @@ public function run(array $params)
6871
return EXIT_ERROR;
6972
}
7073

71-
service('queue')->clear($queue);
74+
try {
75+
/** @var QueueConfig $config */
76+
$config = $this->handleConfig($params);
77+
} catch (QueueException $e) {
78+
CLI::error($e->getMessage());
79+
80+
return EXIT_ERROR;
81+
}
82+
83+
service('queue', $config)->clear($queue);
7284

7385
CLI::print('Queue ', 'yellow');
7486
CLI::print($queue, 'light_yellow');

src/Commands/QueueCommand.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter Queue.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Queue\Commands;
15+
16+
use CodeIgniter\CLI\BaseCommand;
17+
use CodeIgniter\CLI\CLI;
18+
use CodeIgniter\Queue\Config\Queue as QueueConfig;
19+
use CodeIgniter\Queue\Exceptions\QueueException;
20+
21+
abstract class QueueCommand extends BaseCommand
22+
{
23+
/**
24+
* The Command's Group
25+
*
26+
* @var string
27+
*/
28+
protected $group = 'Queue';
29+
30+
/**
31+
* @throws QueueException
32+
*/
33+
protected function handleConfig(array $params)
34+
{
35+
$configName = $params['config'] ?? CLI::getOption('config');
36+
37+
if ($configName !== null) {
38+
$config = config($configName);
39+
40+
if ($config === null) {
41+
throw QueueException::forIncorrectConfigFile();
42+
}
43+
44+
return $config;
45+
}
46+
47+
return config('Queue');
48+
}
49+
50+
protected function getConfigHash(QueueConfig $config): string
51+
{
52+
return md5($config::class);
53+
}
54+
}

src/Commands/QueueFailed.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,12 @@
1313

1414
namespace CodeIgniter\Queue\Commands;
1515

16-
use CodeIgniter\CLI\BaseCommand;
1716
use CodeIgniter\CLI\CLI;
1817
use CodeIgniter\Queue\Config\Queue as QueueConfig;
18+
use CodeIgniter\Queue\Exceptions\QueueException;
1919

20-
class QueueFailed extends BaseCommand
20+
class QueueFailed extends QueueCommand
2121
{
22-
/**
23-
* The Command's Group
24-
*
25-
* @var string
26-
*/
27-
protected $group = 'Queue';
28-
2922
/**
3023
* The Command's Name
3124
*
@@ -53,7 +46,8 @@ class QueueFailed extends BaseCommand
5346
* @var array<string, string>
5447
*/
5548
protected $options = [
56-
'-queue' => 'Queue name.',
49+
'-queue' => 'Queue name.',
50+
'-config' => 'The alternative config file to use. Default value: relies on config(\'Queue\')',
5751
];
5852

5953
/**
@@ -64,10 +58,16 @@ public function run(array $params)
6458
// Read params
6559
$queue = $params['queue'] ?? CLI::getOption('queue');
6660

67-
/** @var QueueConfig $config */
68-
$config = config('Queue');
61+
try {
62+
/** @var QueueConfig $config */
63+
$config = $this->handleConfig($params);
64+
} catch (QueueException $e) {
65+
CLI::error($e->getMessage());
66+
67+
return EXIT_ERROR;
68+
}
6969

70-
$results = service('queue')->listFailed($queue);
70+
$results = service('queue', $config)->listFailed($queue);
7171

7272
$thead = ['ID', 'Connection', 'Queue', 'Class', 'Failed At'];
7373
$tbody = [];

src/Commands/QueueFlush.php

+16-12
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@
1313

1414
namespace CodeIgniter\Queue\Commands;
1515

16-
use CodeIgniter\CLI\BaseCommand;
1716
use CodeIgniter\CLI\CLI;
17+
use CodeIgniter\Queue\Config\Queue as QueueConfig;
18+
use CodeIgniter\Queue\Exceptions\QueueException;
1819

19-
class QueueFlush extends BaseCommand
20+
class QueueFlush extends QueueCommand
2021
{
21-
/**
22-
* The Command's Group
23-
*
24-
* @var string
25-
*/
26-
protected $group = 'Queue';
27-
2822
/**
2923
* The Command's Name
3024
*
@@ -52,8 +46,9 @@ class QueueFlush extends BaseCommand
5246
* @var array<string, string>
5347
*/
5448
protected $options = [
55-
'-hours' => 'Number of hours.',
56-
'-queue' => 'Queue name.',
49+
'-hours' => 'Number of hours.',
50+
'-queue' => 'Queue name.',
51+
'-config' => 'The alternative config file to use. Default value: relies on config(\'Queue\')',
5752
];
5853

5954
/**
@@ -65,11 +60,20 @@ public function run(array $params)
6560
$hours = $params['hours'] ?? CLI::getOption('hours');
6661
$queue = $params['queue'] ?? CLI::getOption('queue');
6762

63+
try {
64+
/** @var QueueConfig $config */
65+
$config = $this->handleConfig($params);
66+
} catch (QueueException $e) {
67+
CLI::error($e->getMessage());
68+
69+
return EXIT_ERROR;
70+
}
71+
6872
if ($hours !== null) {
6973
$hours = (int) $hours;
7074
}
7175

72-
service('queue')->flush($hours, $queue);
76+
service('queue', $config)->flush($hours, $queue);
7377

7478
if ($hours === null) {
7579
CLI::write(sprintf('All failed jobs has been removed from the queue %s', $queue), 'green');

src/Commands/QueueForget.php

+22-10
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@
1313

1414
namespace CodeIgniter\Queue\Commands;
1515

16-
use CodeIgniter\CLI\BaseCommand;
1716
use CodeIgniter\CLI\CLI;
17+
use CodeIgniter\Queue\Config\Queue as QueueConfig;
18+
use CodeIgniter\Queue\Exceptions\QueueException;
1819

19-
class QueueForget extends BaseCommand
20+
class QueueForget extends QueueCommand
2021
{
21-
/**
22-
* The Command's Group
23-
*
24-
* @var string
25-
*/
26-
protected $group = 'Queue';
27-
2822
/**
2923
* The Command's Name
3024
*
@@ -55,6 +49,15 @@ class QueueForget extends BaseCommand
5549
'id' => 'ID of the failed job.',
5650
];
5751

52+
/**
53+
* The Command's Options
54+
*
55+
* @var array<string, string>
56+
*/
57+
protected $options = [
58+
'-config' => 'The alternative config file to use. Default value: relies on config(\'Queue\')',
59+
];
60+
5861
/**
5962
* Actually execute a command.
6063
*/
@@ -68,7 +71,16 @@ public function run(array $params)
6871
return EXIT_ERROR;
6972
}
7073

71-
if (service('queue')->forget((int) $id)) {
74+
try {
75+
/** @var QueueConfig $config */
76+
$config = $this->handleConfig($params);
77+
} catch (QueueException $e) {
78+
CLI::error($e->getMessage());
79+
80+
return EXIT_ERROR;
81+
}
82+
83+
if (service('queue', $config)->forget((int) $id)) {
7284
CLI::write(sprintf('Failed job with ID %s has been removed.', $id), 'green');
7385
} else {
7486
CLI::write(sprintf('Could not find the failed job with ID %s', $id), 'red');

src/Commands/QueuePublish.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313

1414
namespace CodeIgniter\Queue\Commands;
1515

16-
use CodeIgniter\CLI\BaseCommand;
1716
use CodeIgniter\CLI\CLI;
1817
use CodeIgniter\Publisher\Publisher;
1918
use Throwable;
2019

21-
class QueuePublish extends BaseCommand
20+
class QueuePublish extends QueueCommand
2221
{
23-
protected $group = 'Queue';
2422
protected $name = 'queue:publish';
2523
protected $description = 'Publish Queue config file into the current application.';
2624

0 commit comments

Comments
 (0)