001    /*
002     * DispatcherBase.java
003     *
004     * Created on March 16, 2006, 2:18 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    
024    package gov.bnl.star.offline.scheduler.Dispatchers;
025    
026    import gov.bnl.star.offline.scheduler.util.CSHCommandLineTask;
027    import gov.bnl.star.offline.scheduler.Queue;
028    import java.util.logging.Level;
029    import org.apache.log4j.Logger;
030    
031    
032    /**
033     * The base class for all Dispatchers
034     * @author  Levente Hajdu
035     */
036    public class DispatcherBase {
037        
038        static private Logger log = Logger.getLogger(DispatcherBase.class.getName());
039        
040        /** Creates a new instance of DispatcherBase */
041        public DispatcherBase() {}
042        
043        
044    
045        private String testQueueCommand = null;
046        public void setTestQueueCommand(String testQueueCommand){this.testQueueCommand = testQueueCommand;}
047        public String getTestQueueCommand(){return testQueueCommand;}
048        
049        private String testQueueCommandMustMatch = null;
050        public void setTestQueueCommandMustMatch(String testQueueCommand){this.testQueueCommandMustMatch = testQueueCommandMustMatch;}
051        public String getTestQueueCommandMustMatch(){return testQueueCommandMustMatch;}
052        
053        
054        
055        
056        
057        
058        
059        
060        public String threadOuput;
061        /*
062         * Runs a command in separate thread which is time limited so if the operation false the main program will not hand. 
063         * @param command the command to be executed 
064         * @param maxElapseTime the max time allowed to the process to execute in milliseconds
065         * @param msBtwnFailure the time delay before trying again in milliseconds
066         * @param maxAttempts the total number of times executing the command should be tryed
067         * @return true if the command executed and false if it did not execute with in the time limit (maxElapseTime)
068         */
069        public boolean runInTimeLimitedThread(String command, int maxElapseTime, int msBtwnFailure, int maxAttempts  ){
070    
071                threadOuput = null;
072                int attempt = 0;
073                boolean success = false;
074    
075                while (!success && (attempt < maxAttempts)) {
076                        try {
077                           log.info("Executing \"" + command + "\"");
078                           CSHCommandLineTask task = new CSHCommandLineTask(command , true, maxElapseTime);
079                            task.execute();
080                            if (task.getExitStatus() != 0) {
081                                log.warn(command + " (we got)---> " + task.getOutput());
082                                Thread.sleep(msBtwnFailure);
083                                System.out.print(task.getOutput());
084                                attempt++;
085                            } 
086                            else{ 
087                                success = true;
088                                System.out.print("Done");
089    
090                            }
091    
092                            threadOuput = task.getOutput();
093                            log.info("We got:  \"" + threadOuput + "\"");
094                        } 
095                        catch (Exception e) { System.out.print("Failed" + e); 
096                        System.out.print(threadOuput);
097                        }
098                        try { Thread.sleep(msBtwnFailure);} 
099                        catch (Exception e1) {System.out.print("Failed");}
100                        if(!success) System.out.print("/");
101                            attempt++;
102                }
103                return success;
104        }
105        
106        
107    }