Play Framework: expected features in Job

I am trying to run a long-term task (without blocking the execution pool of the HTTP request) using asynchronous tasks in the game. Jobs on Play is fully managed by the infrastructure, so Play will monitor database connections and transactions. This means that the task runs as a simple call within a single transaction. This works great for most use cases, but what if you need to commit some data before completing a job?

Well, according to Guillaume Bort , one option would be

to divide the process into several small tasks

He gives the following example:

@On("0 0 12 * * ?") 
public class UpdateCounters extends Job { 
   public void doJob() { 
       List<Employee> empList = Employee.find("long and boring SQL :)").fetch(); 
       for(Employee emp : empList) { 
           new UpdateEmployeeJob(emp).now().get(); 
       } 
   } 
}

... , , , . ! , ? , , , ?

, Promise (jobPromise), ( Job)...

, , ?

+5
3

, ( ), , Promise Play Future , , isDone(), .

, Future get(),

, , .

, , , , get() Promise, now() ( )

!

+8

, :


        Employee.em().getTransaction().commit();
        Employee.em().getTransaction().begin();
        Employee.em().flush();
        Employee.em().clear();
+1

Given the nature of Play, I would say that the only way is to have some flag (usually in the database), the “father” task can check and block itself in a waiting cycle.

For exmaple, something like:

//take it as an idea, not as working code as it may have typos and dragons
public void doJob() { 
       String uuid = generateUUID();
       List<Employee> empList = Employee.find("long and boring SQL :)").fetch(); 
       for(Employee emp : empList) { 
           //update employee giving uuid so we know this job is related to this parent
           new UpdateEmployeeJob(emp, uuid).now().get(); 
       } 
       int done = 0;
       while(done < empList.size()) {
          //query db for number of instances updated with this uuid 
          //(assuming instance updated once child job is done)
          done = Update.count("uuid = ?1",uuid);
       }
   }

It can handle her

0
source

All Articles