Skip to content

Commit ea113fc

Browse files
committed
feat: complete queues docs
1 parent c1307d3 commit ea113fc

File tree

1 file changed

+71
-58
lines changed

1 file changed

+71
-58
lines changed

src/docs/utils/queues.md

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Some tasks, like processing large CSV uploads, can slow down your app and hurt t
4545
link="https://www.youtube.com/embed/GsdfZ5TfGPw"
4646
/> -->
4747

48+
Leaf queues have three parts: the queue (stores jobs), the job (task to run), and the worker (processes jobs). You push jobs to the queue, and the worker processes them.
49+
4850
## Installation
4951

5052
To get started with Queues in Leaf, you need to install the `leaf/queue` package:
@@ -76,11 +78,11 @@ After installing the package, you need to register the Leaf Queue commands in th
7678
|
7779
*/
7880
Leaf\Core::loadConsole([
79-
Leaf\Redis::commands() // [!code ++]
81+
Leaf\Queue::commands() // [!code ++]
8082
]);
8183
```
8284

83-
This gives you access to new commands which you can use to manage your queues and workers.
85+
This adds commands for managing your queues and workers to the Leaf MVC console.
8486

8587
<!-- This should give you access to the following commands:
8688
@@ -90,13 +92,8 @@ This gives you access to new commands which you can use to manage your queues an
9092
- `php leaf queue:install` - Generate and run a schema file for the queue table.
9193
- `php leaf queue:run` - Start the queue worker. -->
9294

93-
Leaf queues have three parts: the queue (stores jobs), the job (task to run), and the worker (processes jobs). You push jobs to the queue, and the worker processes them.
9495

95-
By default, Leaf MVC uses your database as the queue backend, a new table called `leaf_php_jobs` is created to store your jobs. If you are okay with these defaults, you can start using the queue right away without any additional configuration by starting your worker:
96-
97-
```bash:no-line-numbers
98-
php leaf queue:work
99-
```
96+
By default, Leaf MVC uses your database as the queue backend, storing jobs in a `leaf_php_jobs` table. If you're fine with these defaults, just restart your server—Leaf will detect the queue setup and automatically start processing jobs alongside the PHP and Vite servers.
10097

10198
## Creating a job
10299

@@ -133,18 +130,28 @@ class SendEmailJob extends Job
133130

134131
## Dispatching a job
135132

136-
After creating a job, you can dispatch it to the queue using the `dispatch()` method:
133+
After creating a job, you can dispatch it to the queue using the global `dispatch()` function:
137134

138135
```php:no-line-numbers
139136
dispatch(SendEmailJob::class);
140137
```
141138

142-
Some jobs like the send email job above may require some data to be passed to the job. You can pass data to the job using the `with()` method:
139+
Some jobs like the send email job above may require some data to be passed to the job. You can pass data to the job using the `with()` method on the job:
143140

144141
```php:no-line-numbers
145142
dispatch(SendEmailJob::with($userId));
146143
```
147144

145+
You can also dispatch multiple jobs at once:
146+
147+
```php:no-line-numbers
148+
dispatch([
149+
SendEmailJob::with($userId),
150+
SendReminderJob::with($userId),
151+
ScheduleFlightJob::with($userId),
152+
]);
153+
```
154+
148155
## Specifying options for a job
149156

150157
In the Worker config, you can specify default options for your jobs. However, you can also specify options for a job when dispatching it to the queue. For example, if you want to delay a job for 5 minutes, you can do so by passing the `delay` option to the `dispatch()` method:
@@ -167,12 +174,6 @@ The available options are:
167174
| timeout | The number of seconds a child process can run before being killed. |
168175
| tries | The maximum number of times a job may be attempted. |
169176

170-
Remember to start your worker to process the job:
171-
172-
```bash:no-line-numbers
173-
php leaf queue:work
174-
```
175-
176177
Without a worker running, your jobs will just sit in the queue without being processed.
177178

178179
## Batching Jobs
@@ -219,6 +220,14 @@ You can then dispatch the batch to the queue:
219220
dispatch(ProcessPodcastBatch::class);
220221
```
221222

223+
## Limitations of Queues/Workers
224+
225+
Just as with every other aspect of Leaf, we try to set everything up for you so you can get started right after running `leaf install`. This makes Leaf's queue system very easy to use, but it also comes with some limitations:
226+
227+
- Queues are completely stateless. This means that you can't access the current request, user, session, or any other stateful data in your jobs. If you need to access the current request, you should pass the necessary data to the job as a parameter.
228+
- Leaf's queue system is not designed for long-running tasks like video encoding. We are working on a separate system for long-running tasks.
229+
- Due to its simplicity, Leaf's queue system is not as feature-rich as other queue systems like Laravel's. We are working on adding more features to the queue system.
230+
222231
## Configuration
223232

224233
If you want to change the default config for the queues and workers, you need to publish the queue config file using the MVC console:
@@ -397,42 +406,6 @@ return [
397406
];
398407
```
399408

400-
## Configuration options
401-
402-
There are several configuration options available to you in the `config/queue.php` configuration file. These options are used to determine the connection information for your queues, as well as various other options such as queue retry settings, queue logging, queue worker sleep durations, and more.
403-
404-
### Adapter
405-
406-
The `adapter` option specifies the system that will be used to run your queues. Leaf supports `redis` and `db` as queue adapters. The `redis` adapter uses Redis as a queue backend, while the `db` adapter uses your database as a queue backend.
407-
408-
### Default
409-
410-
The `default` option specifies which of the queue connections found in your config should be used as the default connection for all queue operations. Leaf supports `redis`, `sqlite`, `mysql`, `pgsql`, and `sqlsrv` as queue connections. You can also specify a custom connection by providing the name of a connection that is defined in the `connections` array of your `config/queue.php` file.
411-
412-
### Connections
413-
414-
The `connections` option contains an array of all of the queue connections defined for your application. Each connection corresponds to a queue adapter supported by Leaf. For example, the following configuration defines a connection named `redis` that uses the `redis` adapter to connect to a Redis server:
415-
416-
```php
417-
'connections' => [
418-
'redis' => [
419-
'host' => _env('REDIS_HOST', '127.0.0.1'),
420-
'port' => _env('REDIS_PORT', '6379'),
421-
'password' => _env('REDIS_PASSWORD', ''),
422-
'dbname' => _env('REDIS_DB', 0),
423-
],
424-
425-
...
426-
```
427-
428-
### Queue table
429-
430-
If you are using the `db` adapter, you will need to configure a database table to store your jobs. You can use the `table` option to specify the name of the table. By default, Leaf will use the `leafphp_main_jobs` table that is already included with your application. If you would like to use a different table, you should create the table and specify its name in your `config/queue.php` configuration file:
431-
432-
```php:no-line-numbers
433-
'table' => 'leaf_php_jobs',
434-
```
435-
436409
### Worker Config
437410

438411
Worker config includes the default settings used by your worker when executing a job. These settings can be specified when dispatching a job, but if not specified, the worker will use these settings instead.
@@ -449,13 +422,53 @@ Worker config includes the default settings used by your worker when executing a
449422
| timeout | The number of seconds a child process can run before being killed. |
450423
| tries | The maximum number of times a job may be attempted. |
451424

452-
## Connecting to your queue
425+
## Deployment
453426

454-
As mentioned above, Leaf queue only supports `redis` and `db` as queue adapters. To connect to your queue, you need to specify the adapter and connection you want to use. You can do this by specifying the adapter and connection in the `config/queue.php` file:
427+
When deploying your application with queues, Leaf takes care of setting up the necessary files and commands based on your chosen queue driver. However, once deployed, you’ll need to set up your server to keep your workers running continuously.
455428

456-
```php
457-
'default' => 'redis', // the connection to use for your queues and workers by default
458-
'adapter' => 'redis',
429+
For smaller applications, you can keep the queue worker running in the background with:
430+
431+
```bash:no-line-numbers
432+
php leaf queue:work &
433+
```
434+
435+
This command will set up your queue and start a worker to process jobs. Leaf includes safeguards to prevent excessive memory usage, long-running processes, or crashes from failed jobs. However, for larger applications, this setup may not be enough. In such cases, using a process manager like Supervisor is recommended to ensure your workers run smoothly and restart automatically if needed:
436+
437+
```bash:no-line-numbers
438+
sudo apt update && sudo apt install supervisor -y
439+
```
440+
441+
Create a new configuration file for your Leaf queue worker:
442+
443+
```bash:no-line-numbers
444+
sudo nano /etc/supervisor/conf.d/leaf-queue.conf
445+
```
446+
447+
Add your Supervisor configuration:
448+
449+
```ini:no-line-numbers
450+
[program:leaf-queue]
451+
process_name=%(program_name)s_%(process_num)02d
452+
command=php leaf queue:work
453+
autostart=true
454+
autorestart=true
455+
numprocs=1
456+
redirect_stderr=true
457+
stdout_logfile=/var/log/leaf-queue.log
458+
```
459+
460+
Save and exit, then update Supervisor and start the worker:
461+
462+
```bash:no-line-numbers
463+
sudo supervisorctl reread
464+
sudo supervisorctl update
465+
sudo supervisorctl start leaf-queue
466+
```
467+
468+
This will start a worker that will process jobs in the queue. You can check the status of the worker using the following command:
469+
470+
```bash:no-line-numbers
471+
sudo supervisorctl status leaf-queue
459472
```
460473

461-
<!-- ## Deployment -->
474+
And that's it! Your worker is now running and processing jobs in the queue.

0 commit comments

Comments
 (0)