What is the best way to output the status of a running Server Action to the browser?
I have a multi-step database insert/update Server Action, and I would like to display to my browser updates as it reaches/completes various action step.
ex:
Comparing existing values… (new line)
Importing New Accounts… (new line)
Building Output Table…
Spinning Font Awesome icons would be cool too (I think I can manage that, if I can get progress output sent to my page.)
Hi Lee,
You can use the spinner component and show it while the server action is executing. Check the docs, they show how to add the spinner in a button - but it’s the same for everywhere on your page:
Each action file would have to be called separately to do this, correct?
Is there a way to trigger those milestones while the action is running… say, when it completes the Database Update step, then when it completes another step within a singular action file?
Just to clarify, to accomplish what I was describing, I should separate my action files into small, distinct actions, then use the progress/executing data from each as they progress. (Using the success action to trigger the next step.)
No, there is no point in creating separate server actions.
Maybe you can try adding a setvalue step before each of the steps in the server action, then use its value on the page to show different messages.
Example steps:
setvalue name: currentaction value: upload
upload file
setvalue name: currentaction value: import
import csv
setvalue name: currentaction value: insert
insert step
On the page then show different spinners when the currentaction value changes.
But usually these steps are so fast that you are probably only going to see the messages flashing for some milliseconds.
If you really have a long running server action, writing directly progress from it won’t work because all the output is send at the end all at once.
But there is a way - it might look a bit complex, but bear with me
What you can do is save the progress messages in a Session variable and then use another Server Action to query the progress regularly while the long running action is still busy.
This will be something like:
Long action:
IMPORTANT: make sure you increase the script timeout in the action settings otherwise the action will be terminated after 30 secs.
Steps:
set session name: progress, value: upload started
upload file
set session name: progress, value: import started
import csv
set session name: progress, value: database insert started
insert step
progress action:
define $_SESSION with name ‘progress’
add a step set variable = $_SESSION.progress
enable the ‘output’ option for the step
Client side
add the server connect action for the progress
populate a progress somewhere with the message
create a action scheduler, that will run the progress action regularly, but don’t auto start it
create a define where to run the long running action.
on run of the long running action also enable the action scheduler so it starts poling of the progress server action
on end of the long running action - stop the action scheduler.