JobManager and PersistedServiceJob improvements - based on feedback from Scott.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1371593 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian Crum
2012-08-10 07:50:37 +00:00
parent d8f2291c51
commit 5e05a4df42
2 changed files with 19 additions and 12 deletions
@@ -185,11 +185,14 @@ public final class JobManager {
jobsIterator = delegator.find("JobSandbox", mainCondition, null, null, UtilMisc.toList("runTime"), null);
GenericValue jobValue = jobsIterator.next();
while (jobValue != null) {
jobValue.set("runByInstanceId", instanceId); // Claim ownership of this value.
jobValue.store();
poll.add(new PersistedServiceJob(dctx, jobValue, null));
if (poll.size() == limit) {
break;
// Claim ownership of this value. Using storeByCondition to avoid a race condition.
List<EntityExpr> updateExpression = UtilMisc.toList(EntityCondition.makeCondition("jobId", EntityOperator.EQUALS, jobValue.get("jobId")), EntityCondition.makeCondition("runByInstanceId", EntityOperator.EQUALS, null));
int rowsUpdated = delegator.storeByCondition("JobSandbox", UtilMisc.toMap("runByInstanceId", instanceId), EntityCondition.makeCondition(updateExpression));
if (rowsUpdated == 1) {
poll.add(new PersistedServiceJob(dctx, jobValue, null));
if (poll.size() == limit) {
break;
}
}
jobValue = jobsIterator.next();
}
@@ -31,7 +31,6 @@ import org.apache.commons.lang.StringUtils;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
@@ -94,6 +93,14 @@ public class PersistedServiceJob extends GenericServiceJob {
@Override
public void queue() throws InvalidJobException {
super.queue();
try {
jobValue.refresh();
} catch (GenericEntityException e) {
throw new InvalidJobException("Unable to refresh JobSandbox value", e);
}
if (!JobManager.instanceId.equals(jobValue.getString("runByInstanceId"))) {
throw new InvalidJobException("Job has been accepted by a different instance");
}
Timestamp cancelTime = jobValue.getTimestamp("cancelDateTime");
Timestamp startTime = jobValue.getTimestamp("startDateTime");
if (cancelTime != null || startTime != null) {
@@ -116,20 +123,17 @@ public class PersistedServiceJob extends GenericServiceJob {
protected void init() throws InvalidJobException {
super.init();
try {
// Job might have been canceled after it was placed in the queue.
jobValue.refresh();
} catch (GenericEntityException e) {
throw new InvalidJobException("Unable to refresh JobSandbox value", e);
}
if (!JobManager.instanceId.equals(jobValue.getString("runByInstanceId"))) {
throw new InvalidJobException("Job has been accepted by a different instance");
}
if (jobValue.getTimestamp("cancelDateTime") != null) {
// Job cancelled
throw new InvalidJobException("Job [" + getJobId() + "] was cancelled");
}
String instanceId = UtilProperties.getPropertyValue("general.properties", "unique.instanceId", "ofbiz0");
if (!instanceId.equals(jobValue.getString("runByInstanceId"))) {
// This condition isn't possible, but we will leave it here.
throw new InvalidJobException("Job has been accepted by a different instance!");
}
jobValue.set("startDateTime", UtilDateTime.nowTimestamp());
jobValue.set("statusId", "SERVICE_RUNNING");
try {