This topic is locked
[SOLVED]

AJAX Start Long background job, how to wait for it

1/2/2024 5:43:31 AM
PHPRunner Tips and Tricks
K
KeithS author

I have a button in a list page grid row that launches a backgrounjd job thru the server side of the ajax code.

The background job may run for 2 to 10 minutes.

I want the users browser page to keep querying the server every 10-15 seconds for process status to keep the page alive and update the waiting user.

How would I set this up?

A thought was to put a button on the page that is invisible and have some jquery invoke the button to get an ajax loop started. Each timed interval, the server would return a process status message and display in a sweetalert notice.

In statdard phpRunner, it seems you can only setup an ajax function by adding a button kind of mechanism; which gets you the code setup for the tri-part coding.

Or, how to I make the launch button 3rd state JS code, trigger the same Launch button again with messages to make it act like a status request instead of a launch request? Or perhaps use jquery to hide the launch button once it is pressed and then unhide a status button. The last thing the Launch button does is set a timer to invoke the status button over and over until the process completes?

I know I am just missing a basic part (of understanding) of this process so I can make this work.

Any Ideas?

fhumanes 1/2/2024

Hello,

You can see this article: https://fhumanes.com/blog/guias-desarrollo/guia-73-planificador-de-tareas-batch/
Where I explain how Batch processes can be executed, and above all, how a process can be established (separated from the web server), to execute these long processes.

Also, Phprunner has an API of notifications, to notify users or their groups.

I think studying these 2 things, you can find what you need.

Greetings,
fernando

Sergey Kornilov 1/2/2024

We have discussed this topic in one of our DevClub webinars and there is also an article in our blog on this exact topic:
https://xlinesoft.com/blog/2023/08/28/long-running-process-with-notifications/

K
KeithS author 1/7/2024

Many Thanks!!!!!!
I have implemented a solution based on your long running blog example.
It is not intuitve to know that you can just start a popup in the client side of a ajax button event and then put the server code in the before display event code of the list page. But after working this out and thinking aboujt it, it is a Eureka moement and it works nice. Your blog could have explained that concept a bit more though. Also, explain that the ajax event traffic between the browser and the list page before display event code works to keep your session alive, avoids the 504 gateway timeout from a long running job and so on. My case was not like your ssample. I was just starting a command line process that ran silently. In any case, your help put me on the path to solve this and help me learn something yet again.
You all Rock!