← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5213: Improved SchedulingManager to accept multiple tasks. Decoupled tasks and cron expressions.

 

------------------------------------------------------------
revno: 5213
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2011-11-24 14:19:36 +0100
message:
  Improved SchedulingManager to accept multiple tasks. Decoupled tasks and cron expressions.
removed:
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/Runnables.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/options/SystemSettingManager.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/DefaultSchedulingManager.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/SchedulingManager.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/Scheduler.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/SpringScheduler.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/scheduling/ScheduleTasksAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/struts.xml
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/options/SystemSettingManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/options/SystemSettingManager.java	2011-11-02 14:51:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/options/SystemSettingManager.java	2011-11-24 13:19:36 +0000
@@ -66,6 +66,7 @@
     final String KEY_EMAIL_USERNAME = "keyEmailUsername";
     final String KEY_EMAIL_PASSWORD = "keyEmailPassword";
     final String KEY_SCHEDULED_PERIOD_TYPES = "keyScheduledPeriodTypes";
+    final String KEY_SCHEDULED_TASKS = "keyScheduledTasks";
     
     final int DEFAULT_MAX_NUMBER_OF_ATTEMPTS = 20;
     final int DEFAULT_TIMEFRAME_MINUTES = 1;

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/DefaultSchedulingManager.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/DefaultSchedulingManager.java	2011-06-09 20:24:19 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/DefaultSchedulingManager.java	2011-11-24 13:19:36 +0000
@@ -27,16 +27,15 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.options.SystemSettingManager.KEY_SCHEDULED_TASKS;
+import static org.hisp.dhis.system.scheduling.Scheduler.STATUS_NOT_STARTED;
+
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
 
 import org.hisp.dhis.options.SystemSettingManager;
 import org.hisp.dhis.system.scheduling.Scheduler;
 
-import static org.hisp.dhis.system.scheduling.Scheduler.*;
-
 /**
  * @author Lars Helge Overland
  */
@@ -74,59 +73,64 @@
 
     public void scheduleTasks()
     {
-        scheduler.scheduleTask( getRunnables(), CRON_NIGHTLY_0AM );
+        Map<String, String> keyCronMap = getScheduledTasks();
+        
+        for ( String key : keyCronMap.keySet() )
+        {
+            String cron = keyCronMap.get( key );
+            Runnable task = tasks.get( key );
+            
+            if ( cron != null && task != null )
+            {
+                scheduler.scheduleTask( key, task, cron );
+            }
+        }
+    }
+    
+    public void scheduleTasks( Map<String, String> keyCronMap )
+    {
+        systemSettingManager.saveSystemSetting( KEY_SCHEDULED_TASKS, new HashMap<String, String>( keyCronMap ) );
+        
+        scheduleTasks();
     }
     
     public void stopTasks()
     {
-        scheduler.stopTask( Runnables.class );
+        systemSettingManager.saveSystemSetting( KEY_SCHEDULED_TASKS, null );
+        
+        scheduler.stopAllTasks();
     }
     
     public void executeTasks()
     {
-        scheduler.executeTask( getRunnables() );
+        Map<String, String> keyCronMap = getScheduledTasks();
+        
+        for ( String key : keyCronMap.keySet() )
+        {
+            Runnable task = tasks.get( key );
+            
+            if ( task != null )
+            {
+                scheduler.executeTask( task );
+            }
+        }
+    }
+    
+    @SuppressWarnings("unchecked")
+    public Map<String, String> getScheduledTasks()
+    {
+        return (Map<String, String>) systemSettingManager.getSystemSetting( KEY_SCHEDULED_TASKS, new HashMap<String, String>() );
     }
     
     public String getTaskStatus()
     {
-        return scheduler.getTaskStatus( Runnables.class );
-    }
-    
-    public Set<String> getScheduledTaskKeys()
-    {
-        final Set<String> keys = new HashSet<String>();
-        
-        for ( String key : tasks.keySet() )
-        {
-            boolean schedule = (Boolean) systemSettingManager.getSystemSetting( key, false );
-            
-            if ( schedule )
-            {
-                keys.add( key );
-            }
-        }
-        
-        return keys;
-    }
-
-    // -------------------------------------------------------------------------
-    // Supportive methods
-    // -------------------------------------------------------------------------
-
-    private Runnables getRunnables()
-    {
-        final Runnables runnables = new Runnables();
-        
-        for ( String key : tasks.keySet() )
-        {
-            boolean schedule = (Boolean) systemSettingManager.getSystemSetting( key, false );
-            
-            if ( schedule )
-            {
-                runnables.addRunnable( tasks.get( key ) );
-            }
-        }
-        
-        return runnables;
+        Map<String, String> keyCronMap = getScheduledTasks();
+                
+        if ( keyCronMap.size() == 0 )
+        {
+            return STATUS_NOT_STARTED;
+        }
+        
+        return scheduler.getTaskStatus( keyCronMap.keySet().iterator().next() );
     }
 }

=== removed file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/Runnables.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/Runnables.java	2011-05-14 19:41:19 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/Runnables.java	1970-01-01 00:00:00 +0000
@@ -1,54 +0,0 @@
-package org.hisp.dhis.scheduling;
-
-/*
- * Copyright (c) 2004-2010, University of Oslo
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright notice, this
- *   list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright notice,
- *   this list of conditions and the following disclaimer in the documentation
- *   and/or other materials provided with the distribution.
- * * Neither the name of the HISP project nor the names of its contributors may
- *   be used to endorse or promote products derived from this software without
- *   specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * @author Lars Helge Overland
- */
-public class Runnables
-    implements Runnable
-{
-    private List<Runnable> runnables = new ArrayList<Runnable>();
-    
-    public void addRunnable( Runnable runnable )
-    {
-        this.runnables.add( runnable );
-    }
-    
-    @Override
-    public void run()
-    {
-        for ( Runnable runnable : runnables )
-        {
-            runnable.run();
-        }
-    }    
-}

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/SchedulingManager.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/SchedulingManager.java	2011-06-07 10:35:56 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/SchedulingManager.java	2011-11-24 13:19:36 +0000
@@ -27,20 +27,24 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.Set;
+import java.util.Map;
 
 /**
  * @author Lars Helge Overland
  */
 public interface SchedulingManager
 {
+    final String TASK_DATAMART_LAST_12_MONTHS = "dataMartLast12MonthsTask";
+    
     void scheduleTasks();
     
+    void scheduleTasks( Map<String, String> keyCronMap );
+    
     void stopTasks();
     
     void executeTasks();
     
-    String getTaskStatus();
+    Map<String, String> getScheduledTasks();
     
-    Set<String> getScheduledTaskKeys();
+    String getTaskStatus();   
 }

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml	2011-11-24 10:38:05 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml	2011-11-24 13:19:36 +0000
@@ -276,7 +276,7 @@
     <property name="scheduler" ref="scheduler" />
     <property name="tasks">
       <map>
-        <entry key="keyDataMartTask" value-ref="dataMartLast12MonthsTask" />
+        <entry key="dataMartLast12MonthsTask" value-ref="dataMartLast12MonthsTask" />
       </map>
     </property>
   </bean>

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/Scheduler.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/Scheduler.java	2011-06-09 20:24:19 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/Scheduler.java	2011-11-24 13:19:36 +0000
@@ -1,5 +1,35 @@
 package org.hisp.dhis.system.scheduling;
 
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @author Lars Helge Overland
+ */
 public interface Scheduler
 {
     final String CRON_NIGHTLY_0AM = "0 0 0 * * ?";
@@ -13,9 +43,11 @@
     
     void executeTask( Runnable task );
     
-    boolean scheduleTask( Runnable task, String cronExpr );
-    
-    boolean stopTask( Class<? extends Runnable> taskClass );
-    
-    String getTaskStatus( Class<? extends Runnable> taskClass );
+    boolean scheduleTask( String key, Runnable task, String cronExpr );
+    
+    boolean stopTask( String key );
+    
+    void stopAllTasks();
+    
+    String getTaskStatus( String key );
 }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/SpringScheduler.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/SpringScheduler.java	2011-11-01 15:39:49 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/SpringScheduler.java	2011-11-24 13:19:36 +0000
@@ -74,17 +74,15 @@
         taskExecutor.execute( task );
     }
     
-    public boolean scheduleTask( Runnable task, String cronExpr )
-    {
-        String key = task != null ? task.getClass().getName() : null;
-        
+    public boolean scheduleTask( String key, Runnable task, String cronExpr )
+    {        
         if ( key != null && !futures.containsKey( key ) )
         {
             ScheduledFuture<?> future = taskScheduler.schedule( task, new CronTrigger( cronExpr ) );
             
             futures.put( key, future );
             
-            log.info( "Scheduled task of type: " + key );
+            log.info( "Scheduled task with key: " + key );
             
             return true;
         }
@@ -92,10 +90,8 @@
         return false;
     }
     
-    public boolean stopTask( Class<? extends Runnable> taskClass )
-    {
-        String key = taskClass != null ? taskClass.getName() : null;
-        
+    public boolean stopTask( String key )
+    {        
         if ( key != null )
         {
             ScheduledFuture<?> future = futures.get( key );
@@ -104,18 +100,24 @@
             
             futures.remove( key );
             
-            log.info( "Stopped task of type: " + taskClass.getName() );
+            log.info( "Stopped task with key: " + key );
             
             return result;
         }
         
         return false;
-    }    
+    }
+    
+    public void stopAllTasks()
+    {
+        for ( String key : futures.keySet() )
+        {
+            stopTask( key );
+        }
+    }
 
-    public String getTaskStatus( Class<? extends Runnable> taskClass )
-    {
-        String key = taskClass != null ? taskClass.getName() : null;
-        
+    public String getTaskStatus( String key )
+    {        
         ScheduledFuture<?> future = futures.get( key );
         
         if ( future == null )

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/scheduling/ScheduleTasksAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/scheduling/ScheduleTasksAction.java	2011-11-02 14:51:40 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/scheduling/ScheduleTasksAction.java	2011-11-24 13:19:36 +0000
@@ -30,7 +30,9 @@
 import static org.hisp.dhis.options.SystemSettingManager.KEY_SCHEDULED_PERIOD_TYPES;
 import static org.hisp.dhis.options.SystemSettingManager.*;
 
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 import org.hisp.dhis.options.SystemSettingManager;
@@ -45,6 +47,8 @@
 public class ScheduleTasksAction
     implements Action
 {
+    private static final String STRATEGY_DATAMART_LAST_12_MONTHS_DAILY = "dataMartLast12MonthsDaily";
+    
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -74,19 +78,26 @@
         this.execute = execute;
     }
     
-    private boolean statusOnly = false;
+    private boolean schedule;
 
-    public void setStatusOnly( boolean statusOnly )
+    public void setSchedule( boolean schedule )
     {
-        this.statusOnly = statusOnly;
+        this.schedule = schedule;
     }
-    
+
     private Set<String> scheduledPeriodTypes = new HashSet<String>();
 
     public void setScheduledPeriodTypes( Set<String> scheduledPeriodTypes )
     {
         this.scheduledPeriodTypes = scheduledPeriodTypes;
     }
+    
+    private String dataMartStrategy;
+
+    public void setDataMartStrategy( String dataMartStrategy )
+    {
+        this.dataMartStrategy = dataMartStrategy;
+    }
 
     // -------------------------------------------------------------------------
     // Output
@@ -124,23 +135,24 @@
         {
             schedulingManager.executeTasks();
         }
-        else if ( !statusOnly )
+        else if ( schedule )
         {
             systemSettingManager.saveSystemSetting( KEY_SCHEDULED_PERIOD_TYPES, (HashSet<String>) scheduledPeriodTypes );
             
             if ( Scheduler.STATUS_RUNNING.equals( schedulingManager.getTaskStatus() ) )
             {
-                systemSettingManager.saveSystemSetting( KEY_DATAMART_TASK, new Boolean( false ) );
-                systemSettingManager.saveSystemSetting( KEY_DATASETCOMPLETENESS_TASK, new Boolean( false ) );
-                
                 schedulingManager.stopTasks();
             }
             else
             {
-                systemSettingManager.saveSystemSetting( KEY_DATAMART_TASK, new Boolean( true) );
-                systemSettingManager.saveSystemSetting( KEY_DATASETCOMPLETENESS_TASK, new Boolean( true ) );
-                
-                schedulingManager.scheduleTasks();
+                Map<String, String> keyCronMap = new HashMap<String, String>();
+                
+                if ( STRATEGY_DATAMART_LAST_12_MONTHS_DAILY.equals( dataMartStrategy ) )
+                {
+                    keyCronMap.put( SchedulingManager.TASK_DATAMART_LAST_12_MONTHS, Scheduler.CRON_NIGHTLY_0AM );
+                }
+                
+                schedulingManager.scheduleTasks( keyCronMap );
             }
         }
 

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/struts.xml	2011-11-24 08:27:50 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/struts.xml	2011-11-24 13:19:36 +0000
@@ -557,7 +557,6 @@
       <param name="page">/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm</param>
       <param name="menu">/dhis-web-maintenance-dataadmin/menu.vm</param>
       <param name="javascripts">javascript/scheduling.js</param>
-      <param name="statusOnly">true</param>
       <param name="requiredAuthorities">F_SCHEDULING_ADMIN</param>
     </action>
 
@@ -566,6 +565,7 @@
       <param name="page">/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm</param>
       <param name="menu">/dhis-web-maintenance-dataadmin/menu.vm</param>
       <param name="javascripts">javascript/scheduling.js</param>
+	  <param name="schedule">true</param>
       <param name="requiredAuthorities">F_SCHEDULING_ADMIN</param>
     </action>
 	

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm	2011-11-24 08:27:50 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/viewScheduledTasks.vm	2011-11-24 13:19:36 +0000
@@ -4,6 +4,9 @@
 <input id="isRunning" type="hidden" value="${running}">
 
 <form id="schedulingForm" action="scheduleTasks.action" method="post">
+
+<input id="dataMartStrategy" name="dataMartStrategy" type="hidden" value="dataMartLast12MonthsDaily">
+
 <table>
 <tr>
 	<th>$i18n.getString( "aggregation_period_types" )</th>