BrightSide Workbench Full Report + Source Code
Elephant/elephant/src/main/java/org/turro/queue/Tasks.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.queue;
20 
21 import java.util.Iterator;
22 import org.amic.util.date.CheckDate;
23 import org.turro.elephant.context.Application;
24 
29 public class Tasks implements Runnable {
30 
31  private static final int MINUTES_OLD = 15;
32  private static final long WAIT_BEFORE_STOP = 1000 * 60 * 60;
33 
34  private final TaskQueue queue;
35  private Thread execution;
36  private long startExecution;
37 
38  public static void execute() {
39  getInstance().doExecute();
40  }
41 
42  public static void add(Task task) {
43  Tasks tasks = getInstance();
44  tasks.doAdd(task);
45  tasks.doExecute();
46  }
47 
48  public static TaskQueue tasks() {
49  return getInstance().queue;
50  }
51 
52  public static boolean isInterrupted() {
53  Thread t = getInstance().execution;
54  return t == null || t.isInterrupted();
55  }
56 
57  private static Tasks getInstance() {
59  Tasks tasks = (Tasks) app.getConstructor().getSessionAttribute("elephant_task_queue");
60  if(tasks == null) {
61  tasks = new Tasks();
62  app.getConstructor().setSessionAttribute("elephant_task_queue", tasks);
63  }
64  return tasks;
65  }
66 
67  private Tasks() {
68  this.queue = new TaskQueue();
69  }
70 
71  private void doAdd(Task task) {
72  queue.add(task);
73  }
74 
75  private void doExecute() {
76  if(execution == null || !execution.isAlive()) {
77  startExecution = System.currentTimeMillis();
78  execution = new Thread(this);
79  execution.start();
80  } else if((System.currentTimeMillis() - startExecution) > WAIT_BEFORE_STOP) {
81  execution.interrupt();
82  }
83  }
84 
85  @Override
86  public void run() {
87  Iterator<Task> tasks = queue.iterator();
88  while(tasks.hasNext()) {
89  Task task = tasks.next();
90  if(task.getStatus().equals(TaskStatus.TASK_EXECUTING)) {
91  break;
92  } else if(task.getStatus().equals(TaskStatus.TASK_PENDING)) {
93  task.execute();
94  break;
95  } else if(task.getStatus().equals(TaskStatus.TASK_FAILED)) {
96  if(task.isReintent()) {
97  task.execute();
98  break;
99  } else if(isOld(task)) {
100  tasks.remove();
101  }
102  } else {
103  if(isOld(task)) {
104  tasks.remove();
105  }
106  }
107  }
108  }
109 
110  private boolean isOld(Task task) {
111  return task.getExecution().before(new CheckDate().addMinutes(-MINUTES_OLD).getDate());
112  }
113 
114 }
void setSessionAttribute(String key, Object value)
TaskStatus getStatus()