Laravel Queues and Job Processing : Handle Job and Queues Workflows In Laravel

Other Blogs

Blogs ❯❯ Laravel

Image could not load

Laravel Queues

Job and Queues In Laravel

Laravel एक powerful PHP framework है जो background tasks और long-running processes को handle करना बहुत ही आसान बना देता है। Laravel कि एक key feature जो ऐसे tasks के लिए काम आता है, वो है queues.

Queues आपको time-consuming tasks, जैसे कि emails send करना या large data files process करना, को defer करने कि सुविधा देती हैं। मतलब ये काम तुरंत execute होने के वजाय बाद में होते हैं, जिससे आपका web server दूसरे requests को efficiently handle कर पता है।

इस blog में, हम Laravel में complex queue workflows को handle करना सीखेंगे , जिसमे delayed jobs भी शामिल हैं।

What is Queues in Laravel ?

Laravel में queues का use time-consuming tasks को background में execute करने के लिए होता है, जिससे application का performance और responsiveness maintain रहती है , Laravel multiple queue drivers support करता है जैसे Beanstalkd, Amazon SQS, Redis, और database.

Laravel queue driver configuration

पहले आपको queue driver configure करना होता है। Laravel के config/queue.php file में default queue driver set होता है -

'connections' => [
    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => env('REDIS_QUEUE', 'default'),
        'retry_after' => 90,
    ],
],

Laravel queue job example

Job create करने के लिए, आप artisan command का use कर सकते हैं -

php artisan make:job ProcessPodcast

इस command से App\Jobs\ProcessPodcast class create होती है, जो handle() method define करती है ,आप इस method में अपना business logic लिखते हैं।

namespace App\Jobs; class ProcessPodcast implements ShouldQueue { public function handle() { // Background mein process karne wala code yaha likhen } }

Laravel complex queue workflows

Laravel में आप complex workflows handle करने के लिए chained jobs, job batching, और event-driven jobs का use कर सकते हैं।

1. Laravel chained jobs

Chained jobs का मतलब होता है कि आप एक job के complete hone के बाद दूसरा job run करना चाहते हैं , Laravel इस feature को easy बनाता है -

ProcessPodcast::withChain([ new OptimizePodcast, new NotifyUserOfPodcastCompletion ])->dispatch($podcast);

इस example में, ProcessPodcast job के बाद OptimizePodcast और फिर NotifyUserOfPodcastCompletion job run होंगे।

2. Laravel job batching

Job batching का मतलब है एक group of jobs को एक साथ process करना और batch के complete होने के बाद कुछ action perform करना।

use Illuminate\Bus\Batch; use Illuminate\Support\Facades\Bus; Bus::batch([ new ProcessPodcast($podcast), new OptimizePodcast($podcast), new NotifyUserOfPodcastCompletion($user), ])->then(function (Batch $batch) { // Batch ke complete hone ke baad ka code })->dispatch();

3. Laravel event-driven jobs

Laravel में आप events को listen करने के लिए jobs schedule कर सकते हैं।

Event::listen( PodcastProcessed::class, [NotifyUserOfPodcastCompletion::class, 'handle'] );

Laravel Delayed Jobs

Delayed jobs का use तब होता है जब आप किसी job को तुरंत execute नहीं करना चाहते, बल्कि कुछ delay के बाद execute करना चाहते हैं।

ProcessPodcast::dispatch($podcast)->delay(now()->addMinutes(10));

इस example में, ProcessPodcast job 10 minutes के delay के बाद run होगा।

आप को अपनी job class में ShouldQueue interface implement करना होता है ताकि job queue में schedule हो सके।

use Illuminate\Contracts\Queue\ShouldQueue; class ProcessPodcast implements ShouldQueue { // Handle method & it's logic }

Laravel Run Queues and Jobs

Laravel में queue jobs को run करने के लिए, आपको queue worker start करना होता है। ये worker background में continuously jobs को check करता है और execute करता है।

php artisan queue:work

ये command run करते ही, Laravel का queue worker jobs को process करना शुरू कर देगा।

अगर आप चाहते हैं कि ये worker एक बार में सिर्फ एक ही job process करे, तो आप queue:listen command का use कर सकते हैं ।

Run Queue Worker In Daemon Mode

Production environment में, आपको queue worker को daemon mode में चलना चाहिए ताकि वो continuously run करता रहे।

php artisan queue:work --daemon

अगर आपके पास multiple queues या jobs हैं, तो आपको Supervisor का use करना चाहिए ताकि आप अपने queue workers को monitor और manage कर सकें। Supervisor एक process control system है जो ensure करता है कि आपके queue workers कभी band न हो।

Conclusion

Laravel queues और job processing आपको complex workflows और background tasks handle करने में help करती है। इस blog में हमने chained jobs, job batching, event-driven jobs, और delayed jobs के bare में discuss किया।

आप इन features का use करके अपने Laravel application को ज़्यादा scalable और responsive बना सकते हैं।

अगर आपको ये article पसंद आया हो, तो इससे अपने fellow developers के साथ जरूर share करें!

Happy coding! 🚀

Recent Blogs

Loading ...

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    0 Comment(s) found !