Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Job dispatched on one server, handled on another

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.

like image 881
djt Avatar asked Jun 25 '16 20:06

djt


Video Answer


2 Answers

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

like image 86
Atrakeur Avatar answered Oct 24 '22 22:10

Atrakeur


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.

like image 3
Patrick.SE Avatar answered Oct 24 '22 20:10

Patrick.SE