Run Queue Laravel Guide

User
run queue laravel
Assistant
To run a queue in Laravel, follow these steps:

### 1. **Set Up the Queue Driver**
Ensure your `QUEUE_CONNECTION` in the `.env` file is set up. For example:

– **Sync (default)**: Runs the job immediately.
– **Database**: Stores jobs in the database for later processing.
– **Redis, SQS, etc.**: For more advanced setups.

Example `.env`:

QUEUE_CONNECTION=database

If you're using the `database` driver, you need to create the queue table:

php artisan queue:table
php artisan migrate

### 2. **Dispatch Jobs**
Ensure you have a job to dispatch. For example:

MyJob::dispatch($data);

### 3. **Run the Queue Worker**
Use the `queue:work` command to process jobs:

php artisan queue:work

For continuous processing, you might want to add options like:

php artisan queue:work --queue=default --tries=3 --timeout=90

### 4. **Queue Daemon (Supervisor)**
For production, use a process manager like **Supervisor** to manage the queue worker. Example configuration:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path-to-your-project/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=your-user
numprocs=3
redirect_stderr=true
stdout_logfile=/path-to-your-project/worker.log

Restart Supervisor to apply changes:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*

### 5. **Monitor Jobs**
Use the `queue:failed` and `queue:retry` commands to handle failed jobs:

php artisan queue:failed
php artisan queue:retry {id}
php artisan queue:retry all

### 6. **Running in Docker**
If using Docker, ensure the queue worker runs in a container. Example `docker-compose.yml` service:

queue:
  image: your-laravel-image
  command: php artisan queue:work
  depends_on:
    - app
  restart: always

Let me know if you encounter any issues!

Leave a Comment

Your email address will not be published. Required fields are marked *