Background jobs for asynchronously executing longer tasks

Hi everybody,

Just wondering what is the right way to create a background jobs in Wappler. Let say a user press a button “Send confirmation link”. If I create a Server Action with Send Mail, the page will stuck until the message will be sent and Server Action will return 200.

Usually such tasks are done in background, so user will immediately receive confirmation notification “thanks, we got your request” and background job sends an email.

There maybe a lot of demand for asynchronously executing longer tasks in the background, like sending mails, resizing images, batch imports etc.

Any thoughts how to implement this?

Normally these finish in milliseconds, so it shouldn’t be a problem.
You can just show a spinner / preloader / message while the server action is executing. This way the user knows that something is going on. Example:

Thanks, @Teodor,
I understand that it can be done like that, but there are cases, where 1) the delay is longer 2) there may be a failure in the try, and 2nd and 3rd try to do something, so we can not force customer to wait.

There should be a right approach for that.

Is the wait to not the right approach to deal with failure so the app can detect the error and return an error message?

The Server Connect actions are asynchronously. If you don’t want to wait on the result then you don’t have to, but in most cases you want to know if the action was successful or failed. Instead of showing a spinner you could also let the user work further on the page and show some notification when the action finished.

I think the right approach is - to save the task, then execute it in the background. If SMTP server is not responding, try in 1 min and send again. Sending emails is quite easy, but what if after user registration you have to do a bunch of tasks, starting from creating his resized avatars, creating default projects and settings etc. I think in this case this jobs must be done async.

Thanks, Patrik,

Could it be like that. I create a Server Action, which sends a response 200, so my frontend notifies user it’s successful. And after sending this response executes a list of tasks, like updating DBs, sending mails etc? How in this case to control that this execution was done?

ANother question - any capabilities with Cron-like scenarios, where you schedule task execution on a daily/hourly basis?

Actually I used to do delayed jobs with my past ruby on rails experience - they had a special gem for that.


Every task was serialised and put in the DB and then executed.

Maybe now there are more modern options - like RabbitMQ etc. Need an advise here.

But that is exactly how server connect steps work. The next step is executed, after the previous one finished.

Another way, if you want to run a different server action, is to use the onsuccess event of the server action component on the front end and run another server action, after it returns a success status (200)…

We don’t support scheduled tasks out of the box, but you could implement it easily yourself.

As you already explain yourself, you will need to have a place where you store the jobs, probably in your database. In the server action you then only store the job task data in the database and do nothing further. A second server action will read the database, loop through the jobs and execute the required actions. This server action can be called using a cron job.

You can even make it more advanced with having conditions like start time for the jobs, you just check in the second server action if it matches the condition (like if the current time is >= the start time). When the job is done remove the job from the database.

4 Likes