← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3617: Implemented a SchedulingManager. Saves state of each task as system setting (so they survice serv...

 

------------------------------------------------------------
revno: 3617
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2011-05-14 21:32:53 +0200
message:
  Implemented a SchedulingManager. Saves state of each task as system setting (so they survice server boots etc). Makes it possible to control each task individually. Tasks are configured directly in app context.
added:
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/
  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/Runnables.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/SchedulingManager.java
modified:
  dhis-2/dhis-services/dhis-service-options/src/main/java/org/hisp/dhis/options/SystemSettingManager.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/scheduling/action/ScheduleTasksAction.java
  dhis-2/dhis-web/dhis-web-reporting/src/main/resources/META-INF/dhis/beans.xml


--
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-services/dhis-service-options/src/main/java/org/hisp/dhis/options/SystemSettingManager.java'
--- dhis-2/dhis-services/dhis-service-options/src/main/java/org/hisp/dhis/options/SystemSettingManager.java	2011-04-11 15:42:51 +0000
+++ dhis-2/dhis-services/dhis-service-options/src/main/java/org/hisp/dhis/options/SystemSettingManager.java	2011-05-14 19:32:53 +0000
@@ -55,6 +55,8 @@
     final String KEY_AGGREGATION_STRATEGY = "aggregationStrategy";
     final String KEY_COMPLETENESS_OFFSET = "completenessOffset";
     final String KEY_PATIENT_EXCEL_TEMPLATE_FILE_NAME = "patientExcelTemplateFileName";
+    final String KEY_DATAMART_TASK = "keyDataMartTask";
+    final String KEY_DATASETCOMPLETENESS_TASK = "keyDataSetCompletenessTask";
     
     final int DEFAULT_MAX_NUMBER_OF_ATTEMPTS = 20;
     final int DEFAULT_TIMEFRAME_MINUTES = 1;

=== added directory 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling'
=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/DefaultSchedulingManager.java	2011-05-14 19:32:53 +0000
@@ -0,0 +1,98 @@
+package org.hisp.dhis.scheduling;
+
+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;
+
+public class DefaultSchedulingManager
+    implements SchedulingManager
+{
+    private SystemSettingManager systemSettingManager;
+    
+    public void setSystemSettingManager( SystemSettingManager systemSettingManager )
+    {
+        this.systemSettingManager = systemSettingManager;
+    }
+
+    private Scheduler scheduler;
+
+    public void setScheduler( Scheduler scheduler )
+    {
+        this.scheduler = scheduler;
+    }
+
+    private Map<String, Runnable> tasks = new HashMap<String, Runnable>();
+
+    public void setTasks( Map<String, Runnable> tasks )
+    {
+        this.tasks = tasks;
+    }
+
+    // -------------------------------------------------------------------------
+    // SchedulingManager implementation
+    // -------------------------------------------------------------------------
+
+    public void scheduleTasks()
+    {
+        scheduler.scheduleTask( getRunnables(), Scheduler.CRON_NIGHTLY_1AM );
+    }
+    
+    public void stopTasks()
+    {
+        scheduler.stopTask( Runnables.class );
+    }
+    
+    public void executeTasks()
+    {
+        scheduler.executeTask( getRunnables() );
+    }
+    
+    public String getTaskStatus()
+    {
+        return scheduler.getTaskStatus( Runnables.class );
+    }
+    
+    public Set<String> getRunningTaskKeys()
+    {
+        final Set<String> keys = new HashSet<String>();
+        
+        for ( String key : tasks.keySet() )
+        {
+            if ( scheduler.getTaskStatus( tasks.get( key ).getClass() ).equals( Scheduler.STATUS_RUNNING ) )
+            {
+                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 ) );
+            }
+            else
+            {
+                scheduler.stopTask( tasks.get( key ).getClass() );
+            }
+        }
+        
+        return runnables;
+    }
+}

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/Runnables.java	2011-05-14 19:32:53 +0000
@@ -0,0 +1,24 @@
+package org.hisp.dhis.scheduling;
+
+import java.util.ArrayList;
+import java.util.List;
+
+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();
+        }
+    }    
+}

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/scheduling/SchedulingManager.java	2011-05-14 19:32:53 +0000
@@ -0,0 +1,16 @@
+package org.hisp.dhis.scheduling;
+
+import java.util.Set;
+
+public interface SchedulingManager
+{
+    void scheduleTasks();
+    
+    void stopTasks();
+    
+    void executeTasks();
+    
+    String getTaskStatus();
+    
+    Set<String> getRunningTaskKeys();
+}

=== 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-04-24 12:47:31 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml	2011-05-14 19:32:53 +0000
@@ -293,7 +293,34 @@
     <property name="chartService"
       ref="org.hisp.dhis.chart.ChartService"/>
   </bean>
-    
+  
+  <!-- Scheduling -->
+  
+  <bean id="org.hisp.dhis.scheduling.SchedulingManager" 
+	class="org.hisp.dhis.scheduling.DefaultSchedulingManager" init-method="scheduleTasks">
+	<property name="systemSettingManager" ref="org.hisp.dhis.options.SystemSettingManager"/>
+	<property name="scheduler" ref="scheduler"/>
+	<property name="tasks">
+	  <map>
+	    <entry key="keyDataMartTask" value-ref="dataMartTask"/>
+	    <entry key="keyDataSetCompletenessTask" value-ref="dataSetCompletenessTask"/>
+	  </map>
+	</property>
+  </bean>
+  
+  <bean id="dataMartTask" class="org.hisp.dhis.system.scheduling.DataMartTask" scope="prototype">
+	<constructor-arg ref="org.hisp.dhis.datamart.DataMartService"/>
+	<constructor-arg ref="org.hisp.dhis.dataelement.DataElementService"/>
+	<constructor-arg ref="org.hisp.dhis.indicator.IndicatorService"/>
+	<constructor-arg ref="org.hisp.dhis.organisationunit.OrganisationUnitService"/>
+  </bean>
+  
+  <bean id="dataSetCompletenessTask" class="org.hisp.dhis.system.scheduling.DataSetCompletenessTask" scope="prototype">
+	<constructor-arg ref="registrationDataCompletenessService"/>
+	<constructor-arg ref="org.hisp.dhis.dataset.DataSetService"/>
+	<constructor-arg ref="org.hisp.dhis.organisationunit.OrganisationUnitService"/>
+  </bean>
+  
   <!-- DeletionHandler -->
   
   <bean id="org.hisp.dhis.report.ReportDeletionHandler"

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/scheduling/action/ScheduleTasksAction.java'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/scheduling/action/ScheduleTasksAction.java	2011-02-03 20:15:02 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/scheduling/action/ScheduleTasksAction.java	2011-05-14 19:32:53 +0000
@@ -27,19 +27,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.common.ServiceProvider;
-import org.hisp.dhis.completeness.DataSetCompletenessService;
-import org.hisp.dhis.dataelement.DataElementService;
-import org.hisp.dhis.datamart.DataMartService;
-import org.hisp.dhis.dataset.DataSetService;
-import org.hisp.dhis.indicator.IndicatorService;
-import org.hisp.dhis.organisationunit.OrganisationUnitService;
-import org.hisp.dhis.system.scheduling.DataMartTask;
-import org.hisp.dhis.system.scheduling.DataSetCompletenessTask;
+import org.hisp.dhis.options.SystemSettingManager;
+import org.hisp.dhis.scheduling.SchedulingManager;
 import org.hisp.dhis.system.scheduling.Scheduler;
 
 import com.opensymphony.xwork2.Action;
 
+import static org.hisp.dhis.options.SystemSettingManager.*;
+
 /**
  * @author Lars Helge Overland
  */
@@ -50,53 +45,18 @@
     // Dependencies
     // -------------------------------------------------------------------------
 
-    private Scheduler scheduler;
-    
-    public void setScheduler( Scheduler scheduler )
-    {
-        this.scheduler = scheduler;
-    }
-
-    private DataMartService dataMartService;
-
-    public void setDataMartService( DataMartService dataMartService )
-    {
-        this.dataMartService = dataMartService;
-    }
-    
-    private ServiceProvider<DataSetCompletenessService> serviceProvider;
-
-    public void setServiceProvider( ServiceProvider<DataSetCompletenessService> serviceProvider )
-    {
-        this.serviceProvider = serviceProvider;
-    }
-
-    private DataElementService dataElementService;
-
-    public void setDataElementService( DataElementService dataElementService )
-    {
-        this.dataElementService = dataElementService;
-    }
-
-    private IndicatorService indicatorService;
-
-    public void setIndicatorService( IndicatorService indicatorService )
-    {
-        this.indicatorService = indicatorService;
-    }
-
-    private OrganisationUnitService organisationUnitService;
-
-    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
-    {
-        this.organisationUnitService = organisationUnitService;
-    }
-    
-    private DataSetService dataSetService;
-
-    public void setDataSetService( DataSetService dataSetService )
-    {
-        this.dataSetService = dataSetService;
+    private SystemSettingManager systemSettingManager;
+    
+    public void setSystemSettingManager( SystemSettingManager systemSettingManager )
+    {
+        this.systemSettingManager = systemSettingManager;
+    }
+    
+    private SchedulingManager schedulingManager;
+
+    public void setSchedulingManager( SchedulingManager schedulingManager )
+    {
+        this.schedulingManager = schedulingManager;
     }
 
     // -------------------------------------------------------------------------
@@ -141,31 +101,30 @@
 
     public String execute()
     {
-        DataMartTask dataMartTask = new DataMartTask( dataMartService, dataElementService, indicatorService, organisationUnitService );
-        DataSetCompletenessTask completenessTask = new DataSetCompletenessTask( serviceProvider.provide( "registration" ), dataSetService, organisationUnitService );
-        
         if ( !statusOnly )
         {
             if ( execute )
             {
-                scheduler.executeTask( dataMartTask );
-                scheduler.executeTask( completenessTask );
+                schedulingManager.executeTasks();
             }
-            else if ( scheduler.getTaskStatus( DataMartTask.class ).equals( Scheduler.STATUS_RUNNING ) )
+            else if ( Scheduler.STATUS_RUNNING.equals( schedulingManager.getTaskStatus() ) )
             {
-                scheduler.stopTask( DataMartTask.class );
-                scheduler.stopTask( DataSetCompletenessTask.class );
+                systemSettingManager.saveSystemSetting( KEY_DATAMART_TASK, new Boolean( false ) );
+                systemSettingManager.saveSystemSetting( KEY_DATASETCOMPLETENESS_TASK, new Boolean( false ) );
+                
+                schedulingManager.stopTasks();
             }
             else
             {
-                scheduler.scheduleTask( dataMartTask, Scheduler.CRON_NIGHTLY_2AM );
-                scheduler.scheduleTask( completenessTask, Scheduler.CRON_NIGHTLY_1AM );
+                systemSettingManager.saveSystemSetting( KEY_DATAMART_TASK, new Boolean( true) );
+                systemSettingManager.saveSystemSetting( KEY_DATASETCOMPLETENESS_TASK, new Boolean( true ) );
+                
+                schedulingManager.scheduleTasks();
             }
         }
-        
-        status = scheduler.getTaskStatus( DataMartTask.class );
-        
-        running = scheduler.getTaskStatus( DataMartTask.class ).equals( Scheduler.STATUS_RUNNING );
+
+        status = schedulingManager.getTaskStatus();        
+        running = Scheduler.STATUS_RUNNING.equals( status );
         
         return SUCCESS;
     }

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/resources/META-INF/dhis/beans.xml	2011-04-07 13:48:56 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/resources/META-INF/dhis/beans.xml	2011-05-14 19:32:53 +0000
@@ -472,19 +472,8 @@
   <bean id="org.hisp.dhis.reporting.scheduling.action.ScheduleTasksAction"
     class="org.hisp.dhis.reporting.scheduling.action.ScheduleTasksAction"
     scope="prototype">
-    <property name="scheduler" ref="scheduler"/>
-    <property name="dataMartService"
-      ref="org.hisp.dhis.datamart.DataMartService"/>
-    <property name="serviceProvider"
-      ref="dataCompletenessServiceProvider"/>
-    <property name="dataElementService"
-      ref="org.hisp.dhis.dataelement.DataElementService"/>
-    <property name="indicatorService"
-      ref="org.hisp.dhis.indicator.IndicatorService"/>
-    <property name="organisationUnitService"
-      ref="org.hisp.dhis.organisationunit.OrganisationUnitService"/>
-    <property name="dataSetService"
-      ref="org.hisp.dhis.dataset.DataSetService"/>
+	<property name="systemSettingManager" ref="org.hisp.dhis.options.SystemSettingManager"/>
+	<property name="schedulingManager" ref="org.hisp.dhis.scheduling.SchedulingManager"/>
   </bean>
   
   <!-- Export -->
@@ -496,10 +485,10 @@
 	<property name="exportPivotViewService" ref="org.hisp.dhis.importexport.synchronous.ExportPivotViewService" />
   </bean>
 
-  	<bean id="org.hisp.dhis.reporting.exp.MetaDataExportAction"
-		class="org.hisp.dhis.reporting.exp.MetaDataExportAction"
-		scope="prototype">
-		<property name="serviceProvider" ref="exportServiceProvider" />
-	</bean>
+  <bean id="org.hisp.dhis.reporting.exp.MetaDataExportAction"
+    class="org.hisp.dhis.reporting.exp.MetaDataExportAction"
+    scope="prototype">
+    <property name="serviceProvider" ref="exportServiceProvider" />
+  </bean>
 
 </beans>