You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2012/08/09 12:15:59 UTC

svn commit: r1371140 - in /ofbiz/trunk/framework/service: data/ScheduledServices.xml src/org/ofbiz/service/job/JobManager.java src/org/ofbiz/service/job/PurgeJob.java

Author: adrianc
Date: Thu Aug  9 10:15:59 2012
New Revision: 1371140

URL: http://svn.apache.org/viewvc?rev=1371140&view=rev
Log:
JobManager improvement: Have the JobManager handle purging old jobs - instead of the purgeOldJobs service. Old jobs will be purged when the job queue is idle.

Added:
    ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java   (with props)
Modified:
    ofbiz/trunk/framework/service/data/ScheduledServices.xml
    ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobManager.java

Modified: ofbiz/trunk/framework/service/data/ScheduledServices.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/data/ScheduledServices.xml?rev=1371140&r1=1371139&r2=1371140&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/data/ScheduledServices.xml (original)
+++ ofbiz/trunk/framework/service/data/ScheduledServices.xml Thu Aug  9 10:15:59 2012
@@ -19,5 +19,7 @@ under the License.
 -->
 <entity-engine-xml>
     <TemporalExpression tempExprId="MIDNIGHT_DAILY" tempExprTypeId="FREQUENCY" description="Daily Midnight" date1="2000-01-01 00:00:00.000" integer1="5" integer2="1"/>
+    <!-- The JobManager handles purging old jobs now.
     <JobSandbox jobId="PURGE_OLD_JOBS" jobName="Purge Old Jobs" runTime="2000-01-01 00:00:00.000" serviceName="purgeOldJobs" poolId="pool" runAsUser="system" tempExprId="MIDNIGHT_DAILY" maxRecurrenceCount="-1"/>
+    -->
 </entity-engine-xml>

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobManager.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobManager.java?rev=1371140&r1=1371139&r2=1371140&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobManager.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobManager.java Thu Aug  9 10:15:59 2012
@@ -51,6 +51,8 @@ import org.ofbiz.service.calendar.Recurr
 import org.ofbiz.service.calendar.RecurrenceInfoException;
 import org.ofbiz.service.config.ServiceConfigUtil;
 
+import com.ibm.icu.util.Calendar;
+
 /**
  * Job manager. The job manager queues jobs. It contains a <code>JobPoller</code> and a
  * <code>Delegator</code>. Client code can queue a job to be run immediately by calling the
@@ -146,6 +148,9 @@ public final class JobManager {
      */
     protected List<Job> poll(int limit) {
         assertIsRunning();
+        // The rest of this method logs exceptions and does not throw them.
+        // The idea is to keep the JobPoller working even when a database
+        // connection is not available (possible on a saturated server).
         List<Job> poll = new ArrayList<Job>(limit);
         DispatchContext dctx = getDispatcher().getDispatchContext();
         if (dctx == null) {
@@ -211,6 +216,39 @@ public final class JobManager {
                 Debug.logWarning(e, "Transaction error trying to commit when polling and updating the JobSandbox: ", module);
             }
         }
+        if (poll.isEmpty()) {
+            // No jobs to run, see if there are any jobs to purge
+            int daysToKeep = ServiceConfigUtil.getPurgeJobDays();
+            Calendar cal = Calendar.getInstance();
+            cal.add(Calendar.DAY_OF_YEAR, -daysToKeep);
+            Timestamp purgeTime = new Timestamp(cal.getTimeInMillis());
+            List<EntityExpr> finExp = UtilMisc.toList(EntityCondition.makeCondition("finishDateTime", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("finishDateTime", EntityOperator.LESS_THAN, purgeTime));
+            List<EntityExpr> canExp = UtilMisc.toList(EntityCondition.makeCondition("cancelDateTime", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("cancelDateTime", EntityOperator.LESS_THAN, purgeTime));
+            EntityCondition doneCond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition(canExp), EntityCondition.makeCondition(finExp)), EntityOperator.OR);
+            mainCondition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("runByInstanceId", instanceId), doneCond));
+            try {
+                jobsIterator = delegator.find("JobSandbox", mainCondition, null, null, UtilMisc.toList("jobId"), null);
+                GenericValue jobValue = jobsIterator.next();
+                while (jobValue != null) {
+                    poll.add(new PurgeJob(jobValue));
+                    if (poll.size() == limit) {
+                        break;
+                    }
+                    jobValue = jobsIterator.next();
+                }
+            } catch (Throwable t) {
+                poll.clear();
+                Debug.logWarning(t, "Exception thrown while polling JobSandbox: ", module);
+            } finally {
+                if (jobsIterator != null) {
+                    try {
+                        jobsIterator.close();
+                    } catch (GenericEntityException e) {
+                        Debug.logWarning(e, module);
+                    }
+                }
+            }
+        }
         return poll;
     }
 

Added: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java?rev=1371140&view=auto
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java (added)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java Thu Aug  9 10:15:59 2012
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.ofbiz.service.job;
+
+import java.io.Serializable;
+import java.util.List;
+
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.entity.GenericEntityException;
+import org.ofbiz.entity.GenericValue;
+
+/**
+ * Purge job - removes a JobSandbox entity value and its related values.
+ */
+@SuppressWarnings("serial")
+public class PurgeJob extends AbstractJob implements Serializable {
+
+    public static final String module = PurgeJob.class.getName();
+
+    private final GenericValue jobValue;
+
+    public PurgeJob(GenericValue jobValue) {
+        super(jobValue.getString("jobId"), "Purge " + jobValue.getString("jobName"));
+        this.jobValue = jobValue;
+    }
+
+    @Override
+    public void exec() throws InvalidJobException {
+        if (currentState != State.QUEUED) {
+            throw new InvalidJobException("Illegal state change");
+        }
+        currentState = State.RUNNING;
+        try {
+            // TODO: This might need to be in a transaction - to avoid the possibility of
+            // leaving orphaned related values.
+            jobValue.remove();
+            GenericValue relatedValue = jobValue.getRelatedOne("RecurrenceInfo", false);
+            if (relatedValue != null) {
+                List<GenericValue> valueList = relatedValue.getRelated("JobSandbox", null, null, false);
+                if (valueList.isEmpty()) {
+                    relatedValue.removeRelated("RecurrenceRule");
+                    relatedValue.remove();
+                }
+            }
+            relatedValue = jobValue.getRelatedOne("RuntimeData", false);
+            if (relatedValue != null) {
+                List<GenericValue> valueList = relatedValue.getRelated("JobSandbox", null, null, false);
+                if (valueList.isEmpty()) {
+                    relatedValue.remove();
+                }
+            }
+            Debug.logInfo("Purged job " + getJobId(), module);
+        } catch (GenericEntityException e) {
+            Debug.logWarning(e, "Exception thrown while purging job: ", module);
+        }
+    }
+
+    @Override
+    public long getRuntime() {
+        return System.currentTimeMillis();
+    }
+
+    @Override
+    public boolean isValid() {
+        return currentState == State.CREATED;
+    }
+
+    @Override
+    public void deQueue() throws InvalidJobException {
+        if (currentState != State.QUEUED) {
+            throw new InvalidJobException("Illegal state change");
+        }
+    }
+}

Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL