Coverage Report - com.buckosoft.PicMan.business.BatchManager
 
Classes in this File Line Coverage Branch Coverage Complexity
BatchManager
0%
0/179
0%
0/70
7.333
 
 1  
 /******************************************************************************
 2  
  * BatchManager.java - Serialize heavy Pic processing.  Insure single threadedness
 3  
  * (two copies could lead to _heavens_ out of memory
 4  
  * 
 5  
  * PicMan - The BuckoSoft Picture Manager in Java
 6  
  * Copyright(c) 2007 - Dick Balaska
 7  
  * 
 8  
  */
 9  
 package com.buckosoft.PicMan.business;
 10  
 
 11  
 import java.util.Date;
 12  
 import java.util.LinkedList;
 13  
 import java.util.List;
 14  
 
 15  
 import org.apache.commons.logging.Log;
 16  
 import org.apache.commons.logging.LogFactory;
 17  
 
 18  
 import com.buckosoft.PicMan.business.util.MD5SumUpdater;
 19  
 import com.buckosoft.PicMan.business.util.MlbFilterFixer;
 20  
 import com.buckosoft.PicMan.business.util.PicSizeUpdater;
 21  
 import com.buckosoft.PicMan.domain.JobLogEntry;
 22  
 
 23  
 /** Batch processing of pic jobs.  Serializes heavy Pic processing.
 24  
  * Insure single threadedness (two copies could lead to, <i>heavens</i>, out of memory)
 25  
  * @author Dick Balaska
 26  
  * @since 2007/12/01
 27  
  * @see <a href="http://cvs.buckosoft.com/Projects/PicMan/PicMan/src/main/java/com/buckosoft/PicMan/business/BatchManager.java">BatchManager.java</a>
 28  
  */
 29  0
 public class BatchManager {
 30  
         @SuppressWarnings("unused")
 31  0
         private static boolean DEBUG = false;
 32  0
         private static boolean DEBUGENTRY = false;
 33  
 
 34  0
         protected final Log logger = LogFactory.getLog(getClass());
 35  
 
 36  0
         private        boolean                        isRunning = false;
 37  0
         private        boolean                        syncRunning = false;
 38  0
         private boolean                        picSizeRunning = false;
 39  0
         private        boolean                        mlbFiltersRunning = false;
 40  0
         private        boolean                        md5sumRunning = false;
 41  0
         private        boolean                        mosaicVectorRunning = false;
 42  0
         private        boolean                        mosaicBuilderRunning = false;
 43  0
         private        boolean                        thumbCacheBuilderRunning = false;
 44  0
         private        boolean                        contactsRunning = false;
 45  
         private        PicManFacade        pmf;
 46  
         private        Date                        batchRunStarted;
 47  
         @SuppressWarnings("unused")
 48  
         private        Date                        batchRunEnded;
 49  
 
 50  
         
 51  0
         private        ContactManager contactManager = new ContactManager();
 52  
 
 53  0
         private        PicSizeUpdater        picSizeUpdater = null;
 54  0
         private        MlbFilterFixer        mlbFilterFixer = null;
 55  0
         private        MD5SumUpdater        md5sumUpdater = null;
 56  
 
 57  
         /** Set the reference to the PicMan API.
 58  
          * @param pmf The PicManFacade
 59  
          */
 60  
         public        void setPicMan(PicManFacade pmf) {
 61  0
                 this.pmf = pmf;
 62  0
                 this.contactManager.setPicMan(pmf);
 63  0
         }
 64  
 
 65  
         /** Enable logger output on this module
 66  
          * @param debugFlag true == turn on debugging.
 67  
          */
 68  
         public void setDEBUG(boolean debugFlag) {
 69  0
                 DEBUG = debugFlag;
 70  0
         }
 71  
 
 72  
         /** Enable logger output on this module
 73  
          * @param debugFlag true == turn on debugging.
 74  
          */
 75  
         public void setDEBUGEntry(boolean debugFlag) {
 76  0
                 DEBUGENTRY = debugFlag;
 77  0
         }
 78  
 
 79  
         /** Fetch a reference to the ContactManager
 80  
          * @return the contactManager
 81  
          */
 82  
         public ContactManager getContactManager() {
 83  0
                 return contactManager;
 84  
         }
 85  
 
 86  
         synchronized private boolean okToRun() { 
 87  0
                 if (isRunning == true)
 88  0
                         return(false);
 89  0
                 isRunning = true;
 90  0
                 return(true);
 91  
         }
 92  
         synchronized private void exitRun() {
 93  0
                 isRunning = false;
 94  0
         }
 95  
         
 96  
         /** Return a short status of the engine's state.
 97  
          * @return A one or two word String
 98  
          */
 99  
         public        String        engineRunningShortStatus() {
 100  0
                 if (isRunning) {
 101  0
                         if (contactsRunning)
 102  0
                                 return("run (" + contactManager.getContactRunning() + ")");
 103  0
                         else if (picSizeRunning)
 104  0
                                 return("PicSize #" + this.picSizeUpdater.getPicProcessing());
 105  0
                         else if (this.mlbFiltersRunning)
 106  0
                                 return("mlbFilters #" + this.mlbFilterFixer.getPicProcessing());
 107  0
                         else if (mosaicVectorRunning)
 108  0
                                 return("MosaicVector #" + pmf.getMosaicMan().getMosaicManDevelopment().getPicProcessing());
 109  0
                         else if (this.thumbCacheBuilderRunning)
 110  0
                                 return("BuildThumbCache #" + pmf.getThumbCache().getPicProcessing());
 111  0
                         else if (mosaicBuilderRunning)
 112  0
                                 return("Mosaic: " + pmf.getMosaicMan().getMosaicEngine().getName());
 113  0
                         else if (syncRunning)
 114  0
                                 return("Sync");
 115  0
                         else if (this.md5sumRunning)
 116  0
                                 return("md5sum #" + this.md5sumUpdater.getPicProcessing());
 117  0
                         else return("InvalidState?");
 118  
                 }
 119  0
                 else if (!pmf.getDB().getSystem().isEngineOn())
 120  0
                         return("off");
 121  
                 else
 122  0
                         return("idle");
 123  
         }
 124  
 
 125  
         /** Get a list of other jobs queued.  Useful for reports.
 126  
          * @return A list of work to do.
 127  
          */
 128  
         public List<String>        getOtherJobsQueued() {
 129  0
                 LinkedList<String> list = new LinkedList<String>();
 130  0
                 if (pmf.getDB().getSystem().isUpdatePicSize())
 131  0
                         list.add("Update Pic Sizes");
 132  0
                 if (pmf.getDB().getSystem().isUpdateMlbFilters())
 133  0
                         list.add("Update Mlb Filters");
 134  0
                 if (pmf.getDB().getSystem().isCalcMosaicVectors())
 135  0
                         list.add("Calc Mosaic Vectors");
 136  0
                 if (pmf.getThumbCache().isBuildThumbCache())
 137  0
                         list.add("Build Thumb Cache");
 138  0
                 if (pmf.getDB().getSystem().isUpdateMD5Sums())
 139  0
                         list.add("Update MD5sums");
 140  0
                 return(list);
 141  
         }
 142  
 
 143  
         /** Entry point to the BatchManager. <br>
 144  
          * This runs on the cron tick and will insure single-threadedness of itself.
 145  
          */
 146  
         public        void        run() throws Exception {
 147  0
                 if (DEBUGENTRY)
 148  0
                         logger.info("run!");
 149  0
                 if (!pmf.getDB().getSystem().isEngineOn()) {        // turned off by user
 150  0
                         if (DEBUGENTRY)
 151  0
                                 logger.info("Turned off");
 152  0
                         return;
 153  
                 }
 154  0
                 if (!okToRun()) {
 155  0
                         if (DEBUGENTRY)
 156  0
                                 logger.info("Not ok to run");
 157  0
                         return;
 158  
                 }
 159  
                 // from here on, you *must* call exitRun() to decrement the reference counter
 160  
                 
 161  0
                 if (pmf.getDB().getSystem().isEngineRunOnce() && batchRunStarted != null)        { //only run once per lifetime
 162  0
                         if (DEBUGENTRY)
 163  0
                                 logger.info("Ran once");
 164  0
                         exitRun();
 165  0
                         return;
 166  
                 }
 167  
 
 168  0
                 batchRunStarted = new Date();
 169  0
                 boolean writeJobLog = false;
 170  
                 boolean        workDone;
 171  0
                 JobLogEntry jle = new JobLogEntry();
 172  
                 while (true) {
 173  0
                         if (!pmf.getDB().getSystem().isEngineOn())        // turned off by user
 174  0
                                 break;
 175  0
                         workDone = false;
 176  0
                         if (pmf.getDB().getSystem().isSyncEnable()) {
 177  0
                                 this.syncRunning = true;
 178  0
                                 this.pmf.getSyncManager().run();
 179  0
                                 this.syncRunning = false;
 180  
                         }
 181  0
                         if (pmf.getDB().getSystem().isUpdatePicSize()) {
 182  0
                                 this.picSizeRunning = true;
 183  0
                                 this.picSizeUpdater = new PicSizeUpdater();
 184  0
                                 this.picSizeUpdater.setPicMan(pmf);
 185  
                                 try {
 186  0
                                         this.picSizeUpdater.run();
 187  0
                                 } catch (Exception e) {
 188  0
                                         pmf.addError(e);
 189  0
                                         e.printStackTrace();
 190  0
                                 }
 191  0
                                 pmf.getDB().getSystem().setUpdatePicSize(false);
 192  0
                                 this.picSizeUpdater = null;
 193  0
                                 this.picSizeRunning = false;
 194  0
                                 workDone = true;
 195  0
                         } else if (pmf.getDB().getSystem().isUpdateMlbFilters()) {
 196  0
                                 this.mlbFiltersRunning = true;
 197  0
                                 this.mlbFilterFixer = new MlbFilterFixer();
 198  0
                                 this.mlbFilterFixer.setPicMan(pmf);
 199  
                                 try {
 200  0
                                         this.mlbFilterFixer.run();
 201  0
                                 } catch (Exception e) {
 202  0
                                         pmf.addError(e);
 203  0
                                         e.printStackTrace();
 204  0
                                 }
 205  0
                                 pmf.getDB().getSystem().setUpdateMlbFilters(false);
 206  0
                                 this.mlbFilterFixer = null;
 207  0
                                 this.mlbFiltersRunning = false;
 208  0
                                 workDone = true;
 209  0
                         } else if (pmf.getDB().getSystem().isUpdateMD5Sums()) {
 210  0
                                 this.md5sumRunning = true;
 211  0
                                 this.md5sumUpdater = new MD5SumUpdater();
 212  0
                                 this.md5sumUpdater.setPicMan(pmf);
 213  
                                 try {
 214  0
                                         this.md5sumUpdater.run();
 215  0
                                 } catch (Exception e) {
 216  0
                                         pmf.addError(e);
 217  0
                                         e.printStackTrace();
 218  0
                                 }
 219  0
                                 pmf.getDB().getSystem().setUpdateMD5Sums(false);
 220  0
                                 this.md5sumUpdater = null;
 221  0
                                 this.md5sumRunning = false;
 222  0
                                 workDone = true;
 223  0
                         } else if (pmf.getDB().getSystem().isCalcMosaicVectors()) {
 224  0
                                 this.mosaicVectorRunning = true;
 225  
                                 try {
 226  0
                                         pmf.getMosaicMan().getMosaicManDevelopment().runVectors();
 227  0
                                 } catch (Exception e) {
 228  0
                                         pmf.addError(e);
 229  0
                                         e.printStackTrace();
 230  0
                                 }
 231  0
                                 this.mosaicVectorRunning = false;
 232  0
                                 pmf.getDB().getSystem().setCalcMosaicVectors(false);
 233  0
                                 workDone = true;
 234  
                         }
 235  0
                         if (pmf.getThumbCache().isBuildThumbCache()) {
 236  0
                                 this.thumbCacheBuilderRunning = true;
 237  
                                 try {
 238  0
                                         pmf.getThumbCache().batchBuildCache();
 239  0
                                 } catch (Exception e) {
 240  0
                                         logger.error("Build cache died", e);
 241  0
                                         Exception ex = new Exception("Build Thumb Cache died", e);
 242  0
                                         pmf.addError(ex);
 243  0
                                 }
 244  0
                                 pmf.getThumbCache().setBuildThumbCache(false);
 245  0
                                 this.thumbCacheBuilderRunning = false;
 246  0
                                 workDone = true;
 247  
                         }
 248  0
                         if (pmf.getMosaicMan().hasWorkToDo()) {
 249  0
                                 this.mosaicBuilderRunning = true;
 250  
                                 try {
 251  
                                         @SuppressWarnings("unused")
 252  0
                                         boolean moreWork = pmf.getMosaicMan().runBuilder();
 253  0
                                         workDone = true;
 254  0
                                 } catch (Exception e) {
 255  0
                                         logger.error("Build Mosaic died", e);
 256  0
                                         Exception ex = new Exception("Build Mosaic died", e);
 257  0
                                         pmf.addError(ex);
 258  0
                                 }
 259  0
                                 this.mosaicBuilderRunning = false;
 260  
                         }  /* always check the contacts */ {
 261  0
                                 this.contactsRunning = true;
 262  
                                 try {
 263  0
                                         workDone = contactManager.makeContacts() | workDone;
 264  0
                                 } catch (Throwable t) {
 265  0
                                         batchRunEnded = new Date();
 266  0
                                         this.contactsRunning = false;
 267  0
                                         pmf.addError(t);
 268  0
                                 }
 269  0
                                 this.contactsRunning = false;
 270  
                         }
 271  0
                         if (workDone)
 272  0
                                 writeJobLog = true;
 273  0
                         if (!workDone)
 274  0
                                 break;
 275  
                 }
 276  0
                 batchRunEnded = new Date();
 277  0
                 exitRun();
 278  0
                 if (writeJobLog) {
 279  0
                         jle.setType(JobLogEntry.DONE);
 280  0
                         jle.setColor(JobLogEntry.COLOR_OK);
 281  0
                         jle.setEndTime(new Date());
 282  0
                         pmf.addJobToLog(jle);
 283  
                         
 284  
                 }
 285  0
         }
 286  
 }