问题
I'm working on the logging aspect of a Laravel application, amd was planning to send the data to an SQS for retrieval at a later time. However, I would like to dispatch the job from my production server to the AWS Queue, but then have a Queue working on a separate Logging server which listens to the Queue.
I understand how to setup the Queue worker to listen to the Queue, however, Laravel's Jobs are self handling. So when the worker on the Logging server retrieves the message from SQS, it will look for a job at the same namespace (with the same Class name) to handle it. Is there any way to handle this differently, or do I just simply need to name the Job Handler on the Logging server, the same as the Job Dispatcher on the Production server.
回答1:
You can create different queues for each specific server and send each Job on one of them depending on where they have to be executed.
Basically, this is how to push a job on a specific queue:
$job = (new SendReminderEmail($user))->onQueue('emails');
And here is the command to process jobs from a specific queue:
php artisan queue:listen --queue=emails
Hope it helps
回答2:
I faced a similar challenge. Since we use docker containers for our application we defined a dedicated worker service which shares a volume with the web app. This worker service uses a very simple image with supervisord to consume the jobs that are pushed by the web app. This way we can share the job classes between both services with the possiblity of individually scaling the worker service.
Here's a sample of the docker-compose file:
version: '2'
services:
web:
image: ${AWS_ACC}.dkr.ecr.us-east-1.amazonaws.com/nginx-php:1.0
env_file:
- "env.list.${APP_ENV}"
ports:
- 80:80
- 443:443
tty: true
volumes:
- ${PWD}:/var/www/html
worker:
image: ${AWS_ACC}.dkr.ecr.us-east-1.amazonaws.com/php-worker:1.0
env_file:
- "env.list.${APP_ENV}"
tty: true
volumes:
- ${PWD}:/var/www/html
depends_on:
- web
...
Another potential solution I had in mind was to use git submodules to share the job classes between two projects. Since laravel simply serializes the Job classes, as long as they match signatures, it should work.
来源:https://stackoverflow.com/questions/38032919/laravel-job-dispatched-on-one-server-handled-on-another