Rahul Tikhe Rahul Tikhe - 3 months ago 12
PHP Question

Handling large database with a cron job for a remote API connection

I am newbie in Laravel and building a small project for practice.

There is one registration form and up to 100 registration done a day approx.

It return me status->pending of user.

Now my question come here that, php execution time for connection with remote Api is 30 second as per my knowledge.

Then, how can I handle the cron job in such a condition where I need to call remote api for receiving the data for every user status if it becomes "accepted" or "rejected" or still in "pending"

I am struggling with this condition and my database structure like

userId - status - created_at - updated_at

I need to call it after 20 hours if possible for all user or every 4 hours if we cannot call them at once.

What will be the best practise to do it?

Answer

For long-running tasks, two options come to my mind:

Task Queues

Use an asynchronous task queue. They are designed for long-running tasks. Schedule one job for each newly registered user. Be sure to set the timeout longer than your estimated 30s. Pass the user instance to the constructor of the job and store it to an instance variable. From within the handler, just update the record directly.

Callback

Implement a callback via http. This requires control over the remote api. When calling the remote api, you would submit a callback url, such as https://local-app.domain/callbacks/finished-check-user/223 where 223 is the id of the user. The remote api would send a http request to the given url after it's finished processing. In your local app, have a controller handle the request, fetch the user by its id and update it with the data received from the remote api in the request body. Don't forget to make sure the request is authentic.

Does this help?