laravelredislaravel-queue

push a job into laravel redis queue from another Laravel instance on same server?


I have two Laravel projects running on the same server.

The first calls second's api over HTTP, the the second pushes a job to notify some users.

As both projects live on the same server, can't i make the first project push the job to the second one's redis job queue?


Solution

  • I never tried this approach but it should be possible

    You didn't specify what queue connections are your projects using, but let's assume that they use 2 different connections, for example 2 different redis servers

    In your first laravel project config/queue.php add new connection to connections that will point to the queue connection of the second laravel project. Let's name it project-2-connection

    Then you can use dispatching to a particular connection

    ExampleJob::dispatch($data)->onConnection('project-2-connection');
    

    It's important to make sure that the same job class ExampleJob exists in both projects

    To make your life easier you should pass $data as simple array and avoid SerializesModels. If you pass model from project 1 that doesn't exist in project 2 then your job will will fail with a ModelNotFoundException. You could use models but then you would need to have same model in both projects