A Method for Long Running tasks

Date: 7th May 2020


This blog post displays a method for writing long running tasks for a web application. It is aimed at long running tasks that are composite of many long repetitive actions. Examples of such are: sending a lot of emails, paying staff salaries etc.

This approach is more comfortable than the usual method of creating a separate server for long running tasks.

Prerequisite Knowledge

  1. Golang and its libraries for developing web applications
  2. JavaScript including Jquery

Steps Involved

  1. Get the ids representing the objects to be worked on.

  2. Create a page that works on one id at at time. This id would be passed using GET parameters

  3. Pass the ids in step 1 to a page using templates.

  4. Add a little JavaScript that would call the page in step 2 with each id

  5. When all the tasks are done, notify the user

Demo Description

The demo here doesn’t do anything with the id. Instead it delays for 3 seconds to simulate real life situations.

The demo doesn’t show how to get the ids from a database. These you could look up online. In the demo the ids were randomly created and placed in an array.

A real life situation should store the ids that has been worked on. This is necessary in case of any network interruption.

Necessary Explanations

1. Web Workers

Web workers are needed here because if AJAX requests are made synchronously the page might start hanging. The web workers seems like threading in the browser.

2. No Jquery in Web Workers

After including Jquery on a page. It was found that Jquery cannot be called from a web worker. So to achieve this opinion I had to use plain JavaScript. Majorly the XMLHttpRequest object.

3. Synchronous Requests

Synchronous requests were preferred here since the script would be easier to write.

Final Demo

The final demo

Any Comments

If you have any comments or objections or complaints or contributions please mail us