1 24 package org.riotfamily.riot.job.persistence; 25 26 import java.util.Date ; 27 28 import org.riotfamily.common.util.FormatUtils; 29 import org.riotfamily.riot.job.JobDescription; 30 31 34 public class JobDetail { 35 36 public static final int NEW = -1; 37 38 public static final int INITIALIZED = 0; 39 40 public static final int STARTED = 1; 41 42 public static final int INTERRUPTED = 2; 43 44 public static final int CANCELED = 3; 45 46 public static final int COMPLETED = 4; 47 48 private Long id; 49 50 private String type; 51 52 private String name; 53 54 private String description; 55 56 private int state = NEW; 57 58 private Date startDate; 59 60 private Date endDate; 61 62 private int stepsTotal; 63 64 private int stepsCompleted; 65 66 private long lastStepTime; 67 68 private long executionTime; 69 70 private int averageStepTime; 71 72 private String objectId; 73 74 75 public JobDetail() { 76 } 77 78 public JobDetail(String type, String objectId, int averageStepTime) { 79 this.type = type; 80 this.objectId = objectId; 81 this.averageStepTime = averageStepTime; 82 } 83 84 public void init(JobDescription desc) { 85 this.name = desc.getName(); 86 this.description = desc.getDescription(); 87 this.stepsTotal = desc.getSteps(); 88 this.state = INITIALIZED; 89 } 90 91 public Long getId() { 92 return this.id; 93 } 94 95 public void setId(Long id) { 96 this.id = id; 97 } 98 99 103 public String getType() { 104 return this.type; 105 } 106 107 public void setType(String type) { 108 this.type = type; 109 } 110 111 public String getName() { 112 return this.name; 113 } 114 115 public void setName(String name) { 116 this.name = name; 117 } 118 119 public String getDescription() { 120 return this.description; 121 } 122 123 public void setDescription(String description) { 124 this.description = description; 125 } 126 127 public int getState() { 128 return this.state; 129 } 130 131 public void setState(int state) { 132 this.state = state; 133 } 134 135 public String getObjectId() { 136 return this.objectId; 137 } 138 139 public void setObjectId(String objectId) { 140 this.objectId = objectId; 141 } 142 143 public Date getStartDate() { 144 return this.startDate; 145 } 146 147 public void setStartDate(Date startDate) { 148 this.startDate = startDate; 149 } 150 151 155 public Date getEndDate() { 156 return this.endDate; 157 } 158 159 public void setEndDate(Date endDate) { 160 this.endDate = endDate; 161 } 162 163 public int getStepsTotal() { 164 return this.stepsTotal; 165 } 166 167 public void setStepsTotal(int stepsTotal) { 168 this.stepsTotal = stepsTotal; 169 } 170 171 public int getStepsCompleted() { 172 return this.stepsCompleted; 173 } 174 175 public void setStepsCompleted(int stepsCompleted) { 176 this.stepsCompleted = stepsCompleted; 177 } 178 179 public void stepCompleted() { 180 stepsCompleted++; 181 updateExecutionTime(); 182 } 183 184 185 public long getExecutionTime() { 186 return this.executionTime; 187 } 188 189 public void setExecutionTime(long executionTime) { 190 this.executionTime = executionTime; 191 } 192 193 public int getAverageStepTime() { 194 return this.averageStepTime; 195 } 196 197 public void setAverageStepTime(int averageStepTime) { 198 this.averageStepTime = averageStepTime; 199 } 200 201 public void updateExecutionTime() { 202 long now = System.currentTimeMillis(); 203 if (lastStepTime > 0) { 204 long millis = now - lastStepTime; 205 this.executionTime += millis; 206 } 207 lastStepTime = now; 208 if (stepsTotal > 0 && stepsCompleted > 0) { 209 averageStepTime = (int) (executionTime / stepsCompleted); 210 } 211 } 212 213 public String getElapsedTime() { 214 return FormatUtils.formatMillis(executionTime); 215 } 216 217 public String getEstimatedTime() { 218 if (averageStepTime > 0 && stepsTotal > 0) { 219 long eta = averageStepTime * stepsTotal - executionTime; 220 return FormatUtils.formatMillis(eta); 221 } 222 return null; 223 } 224 225 public int getProgress() { 226 if (stepsTotal > 0) { 227 return stepsCompleted * 100 / stepsTotal; 228 } 229 return -1; 230 } 231 232 public String toString() { 233 return "Job " + id + ": " + name; 234 } 235 236 } 237 | Popular Tags |