skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. Set-up a queue management server and this can receive of point your jobs into queues from even multiple servers. A simple code which might help is below;

    namespace AppHttpControllers;
     
    use AppHttpControllersController;
    use AppJobsProcessPodcast;
    use AppModelsPodcast;
    use IlluminateHttpRequest;
     
    class PodcastController extends Controller
    {
        /**
         * Store a new podcast.
         *
         * @param  IlluminateHttpRequest  $request
         * @return IlluminateHttpResponse
         */
        public function store(Request $request)
        {
            $podcast = Podcast::create(/* ... */);
     
            // Create podcast...
     
            ProcessPodcast::dispatch($podcast)->onQueue('processing');
        }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search