To efficiently manage Horizon queues for a Laravel application running inside a Docker container, follow these steps:


1. Access the Docker Instance

First, enter the relevant Docker container that is running the Laravel application:

bash
docker exec -it $(docker ps | grep wesapi | awk '{print $1}') bash

This command locates and opens a bash shell in the container associated with the wesapi service.


2. Pause Horizon

Before clearing queues, pause Horizon to prevent new jobs from being processed:

bash
echo "Pausing Horizon..."
php artisan horizon:pause


3. Define the List of Queues

Specify the queues that need to be cleared. For example:

bash
QUEUES=(
default
prod
project_0
project_1
project_2
project_3
project_4
project_5
project_6
project_7
project_8
project_9
project_10
project_11
project_12
project_13
project_14
project_15
project_16
project_17
project_18
project_19
)


4. Clear Each Queue

Loop through each queue and clear it using the following script:

bash
echo "Clearing queues..."

for QUEUE in "${QUEUES[@]}"
do
echo "Clearing $QUEUE"
php artisan horizon:clear --queue=$QUEUE
done

This command will clear all jobs from each specified queue.


5. Restart Horizon

After clearing the queues, restart Horizon to resume normal operation:

bash
echo "Restarting Horizon..."
php artisan horizon:terminate


6. Confirmation

Upon completion, you should see a "Done." message, confirming that all steps were executed successfully.


Example: Complete Script

bash

Enter the docker instance

docker exec -it $(docker ps | grep wesapi | awk '{print $1}') bash

Pause Horizon

echo "Pausing Horizon..."
php artisan horizon:pause

List of queues

QUEUES=(
default
prod
project_0
project_1
project_2
project_3
project_4
project_5
project_6
project_7
project_8
project_9
project_10
project_11
project_12
project_13
project_14
project_15
project_16
project_17
project_18
project_19
)

echo "Clearing queues..."

for QUEUE in "${QUEUES[@]}"
do
echo "Clearing $QUEUE"
php artisan horizon:clear --queue=$QUEUE
done

echo "Restarting Horizon..."
php artisan horizon:terminate

echo "Done."


Note: Ensure you have appropriate permissions before executing these commands in a production environment.