How to send user to new page while Server Action is still processing data?

Maybe I need to break this up somehow. Currently, I have a form on page 1 that triggers a Server Action. The server action calls two APIs and then the user is supposed to be sent to another page. The first API gets the parent object and the second API gets all items related to the parent object. The second API has about 10,000 records and I can only get 50 at a time, so it takes a few minutes to complete. I want to send the user to the second page as soon as they have submitted the form, but that is not happening. Currently, the user is stuck on the first page while the Server Action retrieves all data from the second API.

Is there a way to send them to the second page before the Server Action finishes?

I tried to send a 200 Response in the middle of the Server Action, but that appears to stop the Server Action.

The ideal way to solve this is with queues. You pass long running tasks to the queue and it processes in the background.

Wappler doesn’t have that built in, but maybe another vote here will help. :slightly_smiling_face:

Without a queue, you can use the scheduler or cron jobs. This works but is not as immediate in its processing. You basically have a table for these “jobs” and insert a new job into the table when the user submit the form.

The job record has all the information that will be needed to perform the tasks—user-id, type of job, other parameters, etc.

The scheduler or cron then searches the jobs table periodically for anything not yet processed and performs all the api calls etc. and ultimately sets the job record as completed so it is not performed again.

2 Likes

Do you want to send feedback to the user eventually when the SC is finished?

2 Likes

Well now I hadn’t thought of your plug-in @JonL! Are you using it for long running tasks?

I think he’s just happy to see it in my Server Action. :blush:

It was @Apple 's recommendation to help with parsing some JSON. Definitely came in handy. Thanks @JonL.

2 Likes

Nope I was just happy to see someone use it. For complex reusable stuff I would normally resort to a module.

Creating a custom module for a queue is something that has crossed my mind but I think that is one thing that would be better integrated by wappler.

1 Like

Maybe I will look at the custom queues I built and make it consumable by the public.

2 Likes

I voted. I also read another post by @mebeingken about Bull and started to look into it, just not sure how complex the setup will be to get everything working properly.

The user is going to be sent to a list view of the items. It might be good to let them know that the full list will not be available for X mins, but I’m not sure of the proper UX for that part yet.

The idea is that they will use the filters on the left to build a query for alerts that will be sent to them when new items are listed. They will use the list to get a visualization of the item’s attributes in the filter query they are creating for the alert.

1 Like

I have a custom module working that will allow you to execute an api in the background. Just need to clean up some things and can send @kfawcett an alpha version tomorrow if interested.

2 Likes

Nice. I’d love to give it a try.

I also found https://zeplo.io/ and going to see if it’s a quicker path to a queue system.

Nice find. I’ve done basically the same thing for this first version. You select your api and it will call it inside the queue, passing it the data points you need. I handle all the queue setup inside the module.

I want to call a library directly but haven’t figured that out yet.

I’ll dm you tomorrow