Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress indicator in Play framework

What is the best way to implement some kind of progress indicator in Play?

I have a simple import page where the user can upload a csv file. The server is then doing some lenghty processing until the import is complete. I would like to redirect the user to a separate page after uploading and give him some continuing feedback on this page á la "150 of 856 datasets imported".

The upload action triggers a controller method which could start the time consuming task in its own thread, but how do I get the status of the job with an ajax call from another page (since play doesn't have any kind of state between requests)?

like image 789
black666 Avatar asked Oct 07 '22 11:10

black666


1 Answers

What you are likely to do is create a Job from your controller. In fact if you read the Play documentation, you are expressly encouraged to perform long running processing in Jobs so that they do not hog the HTTP request threads.

So, your job is running, by processing your CSV file. The next step is to record when each dataset has been processed. So, assume you pass your Job a reference, like a uid or some unique number, which you pass back to your client. You then simply need to record (probably in a database if you want to conform to statelessness and scale easily) each increment of your number of datasets processed against your unique id.

for example

@Entity
public class DatasetProgress extends Model {

    public Long uid;
    public Long datasetsDone;
    public Long datasetsTotal;

}

You can then create a controller action that returns the DatasetProgress object based on your uid, which you can use to show a progress bar.

like image 81
Codemwnci Avatar answered Oct 12 '22 10:10

Codemwnci