I want to send a Laravel queue job from a Python app. How this can be achievable?
11
-
@adyson I corrected my question as you mentioned, i want to trigger a job in laravel from my python app. I was thinking to craft payload in such way that, laravel job will pick it up, once i push it my redis queueTibin– Tibin2024-08-20 08:46:54 +00:00Commented Aug 20, 2024 at 8:46
-
1@Tibin Why, though? When Laravel pushes a job to a queue, the payload is serialised with a lot of Laravel- and PHP-specific details. You’re going to struggle to emulate that from a Python application. So please explain the problem you’re trying to solve; not the attempt solution. Why are you trying to trigger a Laravel queue job from a Python application, and not the Laravel application itself?Martin Bean– Martin Bean2024-08-20 08:48:38 +00:00Commented Aug 20, 2024 at 8:48
-
1@Tibin you could try that, but I suspect it will be tricky to get it right. Why not just have python talk to Laravel as I suggested, and as the documentation gives you an example of? Then Laravel can trigger the job correctly, and your python app doesn't have to maintain all that knowledge about how to put the right payload in the right queue etc. All it needs to know is how to send a standard HTTP request. The Laravel app should then handle the internals as it would for any job which it triggered itself. Don't mush your apps together like you're suggesting, keep a clean interface between themADyson– ADyson2024-08-20 08:50:02 +00:00Commented Aug 20, 2024 at 8:50
-
My concern was exposing the controlling to the public, ofc i will be having middleware and authentication in place. But i don't know if it's a good practice to go for tiTibin– Tibin2024-08-20 08:53:26 +00:00Commented Aug 20, 2024 at 8:53
-
1My concern was exposing the controlling to the public @Tibin You‘re essentially describing a webhook endpoint. You can “sign” the request in your Python application, and then have the Laravel application check the signature when receiving requests. If the signature is invalid, you can just disregard the request.Martin Bean– Martin Bean2024-08-20 09:02:47 +00:00Commented Aug 20, 2024 at 9:02
|
Show 6 more comments
1 Answer
If dispatching a job from another app is your goal, perhaps you can simply have a controller on the Laravel app which accepts job requests, generates a job and dispatches it for you like in the example at https://laravel.com/docs/11.x/queues#dispatching-jobs
The example is reproduced here for completeness:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Jobs\ProcessPodcast;
use App\Models\Podcast;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class PodcastController extends Controller
{
/**
* Store a new podcast.
*/
public function store(Request $request): RedirectResponse
{
$podcast = Podcast::create(/* ... */);
// ...
ProcessPodcast::dispatch($podcast);
return redirect('/podcasts');
}
}
Then your python app can simply send a HTTP request to the controller in order to trigger the job.