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?
CodePudding user response:
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
CodePudding user response:
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 App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Jobs\ProcessPodcast;
use App\Models\Podcast;
use Illuminate\Http\Request;
class PodcastController extends Controller
{
/**
* Store a new podcast.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$podcast = Podcast::create(/* ... */);
// Create podcast...
ProcessPodcast::dispatch($podcast)->onQueue('processing');
}
}