0

We are creating an application using laravel like example.com. On our application, there is a post api "example.com/api/order-place". In this api, we store some data on our database and send a successful response to our customer. We also call a third-party application to get some others data (third-party.com/api/get-data) on the same request. We are using a Queue job to get this data, without hampering the main order place request journey.

But sometimes the third-party api service is down. At that time, we want to store that third-party api call in some place (queue) and when the third-party application service is up, we want to process all queued jobs.

How could we achieve this? is it possible to solve this problem using laravel queue? Like when third-party applications are down, we hold our queue, and also when the third-party application is up, process these jobs.

We can do this using queue retry on failed jobs. But don't wants that. We just wants to holt a queue when third-party application is down

Hasan Hafiz Pasha
  • 1,402
  • 2
  • 17
  • 25

1 Answers1

0

Should works like this

  1. Create helper function to detect 3rd party api if up or down

  2. Create a class or helper function for processing the 3rd party API request, throw an error if the request to 3rd party API fail ( you might want to call #1)

  3. Create a job with no retries (must only run once) and call #2 in handle

  4. Push your Job on different queue e.i. WhatEverJob::dispatch()->onQueue('apirequest');. You should also process this queue in your supervisor worker e.i

    php artisan queue:work --queue=default,apirequest

  5. Create a Task Scheduler (cron) that runs every hour, minute or whatever. This should first run #1 (or query the failed_jobs first whatever suits you) and exit if its down, if its up, then proceed to querying the failed_jobs table, only pull data with column queue value apirequest. you can do additional filter with payload->displayName which should be your Job Class, you can also check payload->maxTries and do something if its more than number of tries. then manually retry each failed entry.

sample;

if ( ThirdPartyAPI::is('down') )
    return;

$failedEntries = DB::table('failed_jobs') ->where('queue', 'apirequest')->pluck('uuid');

if ( !$failedEntries )
    return;

$uuids = implode(' ', $failedEntries);

\Artisan::call('queue:retry '. $uuids);
silver
  • 4,433
  • 1
  • 18
  • 30