Author: jvelociter
Date: 2007-10-17 11:32:42 +0200 (Wed, 17 Oct 2007)
New Revision: 5413

Modified:
   
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/JobState.java
   
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPlugin.java
   
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginException.java
Log:
XASCH-1 Scheduler plugin now re-schedule jobs on init, according to the stored 
value of their status



Modified: 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/JobState.java
===================================================================
--- 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/JobState.java
     2007-10-17 01:18:17 UTC (rev 5412)
+++ 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/JobState.java
     2007-10-17 09:32:42 UTC (rev 5413)
@@ -27,19 +27,19 @@
  */
 public class JobState
 {
-    public static final String NORMAL = "Normal";
+    private int state;
 
-    public static final String PAUSED = "Paused";
+    public static final String STATE_NORMAL = "Normal";
 
-    public static final String BLOCKED = "Blocked";
+    public static final String STATE_PAUSED = "Paused";
 
-    public static final String COMPLETE = "Complete";
+    public static final String STATE_BLOCKED = "Blocked";
 
-    public static final String ERROR = "Error";
+    public static final String STATE_COMPLETE = "Complete";
 
-    public static final String NONE = "None";
+    public static final String STATE_ERROR = "Error";
 
-    private int state;
+    public static final String STATE_NONE = "None";
 
     public JobState(int state)
     {
@@ -53,25 +53,25 @@
 
     public int getState()
     {
-        return this.state;
+        return state;
     }
 
     public String getValue()
     {
-        switch (this.state) {
+        switch (state) {
             case Trigger.STATE_NORMAL:
-                return JobState.NORMAL;
+                return JobState.STATE_NORMAL;
             case Trigger.STATE_BLOCKED:
-                return JobState.BLOCKED;
+                return JobState.STATE_BLOCKED;
             case Trigger.STATE_COMPLETE:
-                return JobState.COMPLETE;
+                return JobState.STATE_COMPLETE;
             case Trigger.STATE_ERROR:
-                return JobState.ERROR;
+                return JobState.STATE_ERROR;
             case Trigger.STATE_PAUSED:
-                return JobState.PAUSED;
+                return JobState.STATE_PAUSED;
             case Trigger.STATE_NONE:
             default:
-                return JobState.NONE;
+                return JobState.STATE_NONE;
         }
     }
 }

Modified: 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPlugin.java
===================================================================
--- 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPlugin.java
      2007-10-17 01:18:17 UTC (rev 5412)
+++ 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPlugin.java
      2007-10-17 09:32:42 UTC (rev 5413)
@@ -41,6 +41,7 @@
 
 import java.text.ParseException;
 import java.util.Date;
+import java.util.List;
 
 /**
  * See [EMAIL PROTECTED] com.xpn.xwiki.plugin.scheduler.SchedulerPluginApi} 
for documentation.
@@ -74,7 +75,6 @@
     {
         super(name, className, context);
         init(context);
-        //TODO: restaure existing jobs (according to their stored status)
     }
 
     /**
@@ -90,6 +90,7 @@
             setScheduler(getDefaultSchedulerInstance());
             setStatusListener();
             getScheduler().start();
+            restaureExistingJobs(context);
         } catch (SchedulerException e) {
             LOG.error("Failed to start the scheduler", e);
         } catch (SchedulerPluginException e) {
@@ -109,6 +110,48 @@
     }
 
     /**
+     * Restaure the existing job, by looking up for such job in the database 
and re-scheduling those
+     * according to their stored status. If a Job is stored with the status 
"Normal", it is just
+     * scheduled If a Job is stored with the status "Paused", then it is both 
scheduled and paused
+     * Jobs with other status (None, Complete) are not rescheduled.
+     *
+     * @param context The XWikiContext when initializing the plugin
+     */
+    private void restaureExistingJobs(XWikiContext context) throws 
SchedulerPluginException
+    {
+        String hql = ", BaseObject as obj where doc.web='Scheduler'" +
+            " and obj.name=doc.fullName and 
obj.className='XWiki.SchedulerJobClass'";
+        try {
+            List jobsDocsNames = 
context.getWiki().getStore().searchDocumentsNames(hql, context);
+            for (int i = 0; i < jobsDocsNames.size(); i++) {
+                XWikiDocument jobDoc =
+                    context.getWiki().getDocument((String) 
jobsDocsNames.get(i), context);
+                BaseObject jobObj = jobDoc.getObject(XWIKI_JOB_CLASS);
+                String jobName = jobObj.getStringValue("jobName");
+                try {
+                    String status = jobObj.getStringValue("status");
+                    if (status.equals(JobState.STATE_NORMAL) ||
+                        status.equals(JobState.STATE_PAUSED))
+                    {
+                        this.scheduleJob(jobObj, context);
+                    }
+                    if (status.equals(JobState.STATE_PAUSED)) {
+                        this.pauseJob(jobObj, context);
+                    }
+                } catch (XWikiException e) {
+                    throw new SchedulerPluginException(
+                        
SchedulerPluginException.ERROR_SCHEDULERPLUGIN_RESTORE_JOB,
+                        "Failed to restaure job with job name " + jobName, e);
+                }
+            }
+        } catch (XWikiException e) {
+            throw new SchedulerPluginException(
+                
SchedulerPluginException.ERROR_SCHEDULERPLUGIN_RESTAURE_EXISTING_JOBS,
+                "Failed to restaure existing scheduler jobs", e);
+        }
+    }
+    
+    /**
      * Retrieve the job's status of a given [EMAIL PROTECTED] 
com.xpn.xwiki.plugin.scheduler.SchedulerPlugin#XWIKI_JOB_CLASS}
      * job XObject, by asking the actual job status to the quartz scheduler 
instance. It's the
      * actual status, as the one stored in the XObject may be changed manually 
by users.

Modified: 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginException.java
===================================================================
--- 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginException.java
     2007-10-17 01:18:17 UTC (rev 5412)
+++ 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginException.java
     2007-10-17 09:32:42 UTC (rev 5413)
@@ -41,8 +41,10 @@
 
     protected static final int ERROR_SCHEDULERPLUGIN_GET_SCHEDULER = 90007;
 
-    protected static final int ERROR_SCHEDULERPLUGIN_RESTORE_JOBS = 90008;
+    protected static final int ERROR_SCHEDULERPLUGIN_RESTORE_JOB = 90008;
 
+    protected static final int ERROR_SCHEDULERPLUGIN_RESTAURE_EXISTING_JOBS = 
90009;
+
     public SchedulerPluginException(int code, String message)
     {
         super(SchedulerPlugin.class, code, message);

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to