### Directory layout for the end-to-end example Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Shows the expected directory layout for the end-to-end example, with both repositories checked out as siblings. ```text your-projects/ ├── laravel-queue-worker/ └── laravel-queue-consumer/ (you are here) ``` -------------------------------- ### Install laravel-queue-consumer via Composer Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/README.md Install the package via Composer. Laravel's auto-discovery registers the service provider automatically. ```bash composer require jeffersongoncalves/laravel-queue-consumer ``` -------------------------------- ### Start the demo container Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Starts the throwaway container and opens a shell inside it. The container is the closest thing to a shared host filesystem without provisioning real servers. ```bash docker compose -f example/docker-compose.yml up -d docker compose -f example/docker-compose.yml exec demo bash ``` -------------------------------- ### Run hub migrations, serve, and queue:work Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Runs the hub's database migrations, starts the HTTP server on port 8000, and starts the queue worker in the background. ```bash php artisan migrate php artisan serve --host=0.0.0.0 --port=8000 & php artisan queue:work & ``` -------------------------------- ### Install Composer and PHP extensions Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Installs Composer and the PHP extensions Laravel needs inside the container, because the base image only ships the bare minimum. ```bash apt-get update && apt-get install -y unzip libzip-dev git docker-php-ext-install mbstring zip curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer ``` -------------------------------- ### Environment setup for hub queue connection Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/README.md Environment variables to set the queue connection to 'hub' and configure the hub URL, token, and timeout. QUEUE_CONSUMER_SLUG is optional and defaults to the folder name of the current environment. ```env QUEUE_CONNECTION=hub QUEUE_CONSUMER_HUB_URL=https://hub.internal.example QUEUE_CONSUMER_TOKEN=some-shared-secret-token QUEUE_CONSUMER_TIMEOUT=5 ``` -------------------------------- ### Job forwarding request body to {hub_url}/api/jobs Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/README.md Example of the JSON body sent in the POST request to {hub_url}/api/jobs. The payload field contains Laravel's serialized job payload untouched. The request must include an X-Laravel-Queue-Token header with the configured token; a successful response is 202 with {"id": ""}. ```json { "slug": "app-feature-1234", "path": "/srv/environments/app-feature-1234", "queue": "default", "delay": 0, "payload": "" } ``` -------------------------------- ### Wire the environment app to laravel-queue-consumer Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Wires the environment app to the laravel-queue-consumer package via a path repository and publishes its configuration. ```bash cd /workspace/apps/app-feature-1234 composer config repositories.queue-consumer path /workspace/packages/laravel-queue-consumer composer require jeffersongoncalves/laravel-queue-consumer:@dev --no-interaction php artisan vendor:publish --tag=queue-consumer-config ``` -------------------------------- ### Publish queue-consumer configuration Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/README.md Publish the configuration file to config/queue-consumer.php. ```bash php artisan vendor:publish --tag=queue-consumer-config ``` -------------------------------- ### Scaffold the hub and environment apps Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Scaffolds the hub app and one ephemeral environment app. The directory name of the environment app is also its slug. ```bash cd /workspace/apps composer create-project laravel/laravel hub --prefer-dist --no-interaction composer create-project laravel/laravel app-feature-1234 --prefer-dist --no-interaction ``` -------------------------------- ### Environment .env configuration Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Environment variables for the environment app. QUEUE_CONSUMER_TOKEN is a demo-only value; see the Security section in the package README before configuring a real hub. ```text QUEUE_CONNECTION=hub QUEUE_CONSUMER_HUB_URL=http://127.0.0.1:8000 QUEUE_CONSUMER_TOKEN=demo-token QUEUE_CONSUMER_SLUG=app-feature-1234 ``` -------------------------------- ### Wire the hub app to laravel-queue-worker Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Wires the hub app to the laravel-queue-worker package via a path repository and publishes its configuration. ```bash cd /workspace/apps/hub composer config repositories.queue-worker path /workspace/packages/laravel-queue-worker composer require jeffersongoncalves/laravel-queue-worker:@dev --no-interaction php artisan vendor:publish --tag=queue-worker-config ``` -------------------------------- ### queue-consumer.php configuration Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/README.md Configuration file for the queue consumer. The 'slug' defaults to the basename of the base path, and 'timeout' defaults to 5 seconds. ```php return [ 'hub_url' => env('QUEUE_CONSUMER_HUB_URL'), 'token' => env('QUEUE_CONSUMER_TOKEN'), 'slug' => env('QUEUE_CONSUMER_SLUG', basename(base_path())), 'timeout' => env('QUEUE_CONSUMER_TIMEOUT', 5), ]; ``` -------------------------------- ### Hub .env configuration Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Environment variables for the hub app. QUEUE_WORKER_TOKEN and QUEUE_WORKER_ALLOWED_ROOT are demo-only values; see the Security section in the package README before configuring a real hub. ```text QUEUE_CONNECTION=database QUEUE_WORKER_TOKEN=demo-token QUEUE_WORKER_ALLOWED_ROOT=/workspace/apps ``` -------------------------------- ### Run tests with composer test Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/README.md Command to run the test suite for the package. ```bash composer test ``` -------------------------------- ### Clean up the demo container Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Exits the container shell and tears down the demo container, removing volumes. ```bash exit docker compose -f example/docker-compose.yml down -v ``` -------------------------------- ### Map PHP binary in config/queue-worker.php Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Maps the container's own PHP binary in config/queue-worker.php. Confirm the PHP version with php -v before using. ```php 'php_binary_map' => [ '8.3' => '/usr/local/bin/php', ], ``` -------------------------------- ### Dispatch a job from the environment Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Dispatches a closure job from the environment app. The closure writes a timestamp to a file, proving the job travelled to the hub and back. ```bash php artisan tinker --execute="dispatch(fn () => file_put_contents('/tmp/ran-from-app-feature-1234', now()));" ``` -------------------------------- ### Confirm the job ran Source: https://github.com/jeffersongoncalves/laravel-queue-consumer/blob/main/EXAMPLE.md Confirms the job ran by checking the file created by the dispatched closure. A timestamp proves the job travelled from the environment to the hub and back. ```bash cat /tmp/ran-from-app-feature-1234 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.