Robycool
Robycool

Reputation: 1224

Laravel API: extremely long timeouts for external API

For an endpoint in the Laravel api, I am calling an external api that is extremely slow and can take 90 min to respond because it needs to run a whole bunch of processes.

In my laravel api, I am sending an immediate response to my clients that the request is launched, and they are receiving an email when the 90 min are over after I get a response from the api.

Is it ok to have timeouts of 120 minutes or even more for calling a REST API, and leaving an open connexion during all that time ? Does it affect performance for other users of my API ? Are there any Laravel parameters I need to change (except timeout) so that everything runs smoothly (e.g., is there a max number of concurrent workers ?) ?

Upvotes: 5

Views: 2241

Answers (1)

Kerel
Kerel

Reputation: 732

Laravel Queues

If you aren't using them yet then yes you need to dispatch a job instead of just executing your code.

Step 1: Create a job

php artisan make:job ProcessRequest

Step 2: Add handle logic to job

/**
 * Execute the job.
 *
 * @return void
 */
public function handle(AudioProcessor $processor)
{
    //Logic goes in here.
}

Step 3: Dispatch the job where needed

 ProcessRequest::dispatch();

Step 4: (Optional) Install Laravel Horizon to monitor jobs Don't forget to start it after installing

php artisan horizon

Also take a look at Delayed Dispatching Jobs A good thing to have is Telescope,

Upvotes: 2

Related Questions