- Timestamp:
- Jan 31, 2013, 4:13:09 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20121219/ippToPsps/jython/ipptopsps.py
r35063 r35076 1 1 #!/usr/bin/env jython 2 # EAM : config -> skychunk DONE 2 3 3 4 import signal … … 9 10 10 11 from config import Config 12 from skychunk import Skychunk 11 13 from pslogger import PSLogger 12 14 from ipptopspsdb import IppToPspsDb … … 23 25 def __init__(self, argv, logToStdout=1, logToFile=0): 24 26 25 testmode = False 26 for arg in sys.argv: 27 if arg == "-test": 28 testmode = True 29 sys.argv.remove(arg) 30 if arg == "-t": 31 testmode = True 32 sys.argv.remove(arg) 33 34 # are the arsg ok? all programs require a config name 27 # set up config object (global config information, also parse command-line options, eg -test / -t) 28 self.config = Config() 29 30 # are the args ok? all programs require a config name 35 31 if len(sys.argv) < 2: 36 32 self.printUsage() … … 38 34 39 35 # some class constants 40 self.PROGNAME = os.path.basename(sys.argv[0])41 CONFIGNAME = sys.argv[1]42 36 self.HOST = socket.gethostname() 43 37 self.PID = os.getpid() … … 47 41 self.SECONDS = None 48 42 49 self.configDir = os.getenv("IPPTOPSPS_DATA")50 if self.configDir is not None:51 self.configDir = self.configDir + "/"52 53 self.createNewConfig = False54 self.rotateConfigs = False55 # a new config?56 if CONFIGNAME == "edit": self.createNewConfig = True57 # should we rotate configs for this program?58 elif CONFIGNAME == "all": self.rotateConfigs = True59 60 # set up config object61 self.config = Config(self.PROGNAME, CONFIGNAME, self.configDir)62 self.config.test = testmode63 64 if self.config.test: print "using test mode"65 else: print "not using test mode"66 67 43 self.logger = self.config.getLogger(self.HOST, self.PID, logToStdout, logToFile) 68 44 45 # argv[1] -> self.skychunk.name 46 self.skychunk = Skychunk(self.logger) 47 print "here... " 48 69 49 # create connection to databases database 70 50 try: 71 self.ippToPspsDb = IppToPspsDb(self.logger, self.config )51 self.ippToPspsDb = IppToPspsDb(self.logger, self.config, self.skychunk) 72 52 except: 73 53 self.exitProgram("Could not connect to ipptopsps Db") 74 54 raise 75 55 56 print "here... " 57 76 58 self.checkClientStatus() 59 print "here... " 77 60 78 61 # catch Ctrl-C signal 79 62 signal.signal(signal.SIGINT, self.signal_handler) 63 print "here... " 80 64 81 65 # title for log 82 66 self.logger.infoSeparator() 83 self.logger.infoTitle("ippToPsps '" + self. PROGNAME+ "' started")67 self.logger.infoTitle("ippToPsps '" + self.config.programName + "' started") 84 68 self.logger.infoPair("Host", self.HOST) 85 69 self.logger.infoPair("PID", "%d" % self.PID) 86 self.logger.infoPair("Config", self.config.name) 70 self.logger.infoPair("Skychunk", self.skychunk.name) 71 print "here... " 87 72 88 73 ''' … … 122 107 123 108 # write message to log or stdout if no logger set up 124 msg = sys.argv[0] + " < configName|all|edit> " + extra109 msg = sys.argv[0] + " <skychunkName|all|edit> " + extra 125 110 try: 126 111 self.logger.errorPair("Usage:", msg) … … 129 114 130 115 ''' 131 Refreshes the config then recreates objects with new settings 132 ''' 133 def refreshConfig(self): 134 135 # new config? 136 if self.createNewConfig: 137 self.ippToPspsDb.editConfig() 138 self.ippToPspsDb.setConfigForThisClient(self.config.name, self.HOST, self.PID) 139 self.createNewConfig = False 140 141 # if we are rotating configs, then look for next active one in list 142 if self.rotateConfigs: 143 self.ippToPspsDb.getConfigForThisClient(self.HOST, self.PID) 144 configs = self.ippToPspsDb.getActiveConfigList() 116 Refreshes the skychunk then recreates objects with new settings 117 ''' 118 def refreshSkychunk(self): 119 120 print "refresh... " 121 print "self.skychunk.createNewSkychunk: ", self.skychunk.createNewSkychunk 122 123 # new skychunk? 124 if self.skychunk.createNewSkychunk: 125 print "self.skychunk.createNewSkychunk: ", self.skychunk.createNewSkychunk 126 self.ippToPspsDb.editSkychunk() 127 print "self.skychunk.createNewSkychunk: ", self.skychunk.createNewSkychunk 128 self.ippToPspsDb.setSkychunkForThisClient(self.skychunk.name, self.HOST, self.PID) 129 print "self.skychunk.createNewSkychunk: ", self.skychunk.createNewSkychunk 130 self.skychunk.createNewSkychunk = False 131 132 print "refresh... " 133 # if we are rotating skychunks, then look for next active one in list 134 if self.skychunk.rotateSkychunks: 135 self.ippToPspsDb.getSkychunkForThisClient(self.HOST, self.PID) 136 skychunks = self.ippToPspsDb.getActiveSkychunkList() 145 137 146 138 i = 1 147 for config in configs:148 if config == self.config.name: break139 for skychunk in skychunks: 140 if skychunk == self.skychunk.name: break 149 141 i += 1 150 142 151 if i >= len(configs): newConfig = configs[0] 152 else: newConfig = configs[i] 153 154 self.ippToPspsDb.setConfigForThisClient(newConfig, self.HOST, self.PID) 155 156 return self.ippToPspsDb.readConfig(self.HOST, self.PID) 157 158 ''' 159 All implementing subclasses (programs) should call this method once in a while to see if it should be paused or killed, otherwise this updates the clients table with current time and reloads the config data 143 if i >= len(skychunks): newSkychunk = skychunks[0] 144 else: newSkychunk = skychunks[i] 145 146 self.ippToPspsDb.setSkychunkForThisClient(newSkychunk, self.HOST, self.PID) 147 148 print "refresh... " 149 return self.ippToPspsDb.readSkychunk(self.HOST, self.PID) 150 151 ''' 152 All implementing subclasses (programs) should call this method once in a while to see if it should be paused or killed, otherwise this updates the clients table with current time and reloads the skychunk data 160 153 ''' 161 154 def checkClientStatus(self): 162 155 163 self.ippToPspsDb.updateClient(self.PROGNAME, self.HOST, self.PID) 164 156 print "stat... " 157 self.ippToPspsDb.updateClient(self.config.programName, self.HOST, self.PID) 158 159 print "stat... " 165 160 if self.ippToPspsDb.isKilled(self.HOST, self.PID): 166 161 self.exitProgram("killed via Db") 167 162 168 # this loop pauses the process if we have no configor we have set it to pause163 # this loop pauses the process if we have no skychunk or we have set it to pause 169 164 firstTimeIn = True 170 while not self.refreshConfig() or self.ippToPspsDb.isPaused(self.HOST, self.PID): 171 172 self.ippToPspsDb.updateClient(self.PROGNAME, self.HOST, self.PID) 165 print "stat... " 166 while not self.refreshSkychunk() or self.ippToPspsDb.isPaused(self.HOST, self.PID): 167 168 print "stat... " 169 self.ippToPspsDb.updateClient(self.config.programName, self.HOST, self.PID) 173 170 if self.ippToPspsDb.isKilled(self.HOST, self.PID): 174 171 self.exitProgram("killed while paused") … … 177 174 time.sleep(self.PAUSEPERIOD) 178 175 176 print "stat done... " 177 179 178 180 179 ''' … … 193 192 # write message to log or stdout if no logger set up 194 193 try: 195 self.logger.infoPair(self. PROGNAME+ " exited", exitReason)194 self.logger.infoPair(self.config.programName + " exited", exitReason) 196 195 except: 197 196 print "*** Program exited: " + exitReason … … 204 203 205 204 sys.exit(0) 206 207
Note:
See TracChangeset
for help on using the changeset viewer.
