<s> import logging <EOL> import socket <EOL> import os <EOL> import signal <EOL> import sys <EOL> import time <EOL> import datetime <EOL> import platform <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> import httplib <EOL> import subprocess <EOL> from subprocess import PIPE <EOL> try : <EOL> import psutil <EOL> except ImportError : <EOL> print ( "<STR_LIT>" ) <EOL> from octopus . core . framework . mainloopapplication import MainLoopApplication <EOL> from octopus . core . communication . requestmanager import RequestManager <EOL> from octopus . core . enums import command as COMMAND <EOL> from octopus . core . enums import rendernode <EOL> from octopus . core . enums . rendernode import * <EOL> from octopus . worker import settings <EOL> from octopus . worker import config <EOL> from octopus . worker . model . command import Command <EOL> from octopus . worker . process import spawnCommandWatcher <EOL> from octopus . worker . process import spawnRezManagedCommandWatcher <EOL> LOGGER = logging . getLogger ( "<STR_LIT>" ) <EOL> COMPUTER_NAME_TEMPLATE = "<STR_LIT>" <EOL> KILOBYTES = <NUM_LIT:0> <EOL> MEGABYTES = <NUM_LIT:1> <EOL> GIGABYTES = <NUM_LIT:2> <EOL> class WorkerInternalException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value ) : <EOL> self . parameter = value <EOL> def __str__ ( self ) : <EOL> return repr ( self . parameter ) <EOL> class Worker ( MainLoopApplication ) : <EOL> """<STR_LIT>""" <EOL> class CommandWatcher ( object ) : <EOL> def __init__ ( self ) : <EOL> self . id = None <EOL> self . processId = None <EOL> self . startTime = None <EOL> self . processObj = None <EOL> self . timeOut = None <EOL> self . commandId = None <EOL> self . command = None <EOL> self . modified = True <EOL> self . finished = False <EOL> def __repr__ ( self ) : <EOL> return str ( <EOL> "<STR_LIT>" % <EOL> ( self . id , self . processId , self . startTime , self . processObj , self . timeOut , self . commandId , self . command , self . modified , self . finished ) <EOL> ) <EOL> def __str__ ( self ) : <EOL> startTime = datetime . datetime . fromtimestamp ( int ( self . startTime ) ) . strftime ( "<STR_LIT>" ) <EOL> returncode = self . processObj . process . returncode if self . processObj . process is not None else "<STR_LIT>" <EOL> return str ( "<STR_LIT>" % ( self . processId , self . commandId , returncode , startTime ) ) <EOL> @ property <EOL> def modifiedCommandWatchers ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( watcher for watcher in self . commandWatchers . values ( ) if watcher . modified ) <EOL> @ property <EOL> def finishedCommandWatchers ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( watcher for watcher in self . commandWatchers . values ( ) if watcher . finished and not watcher . modified ) <EOL> def __init__ ( self , framework ) : <EOL> super ( Worker , self ) . __init__ ( self ) <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> self . framework = framework <EOL> self . data = None <EOL> self . requestManager = RequestManager ( settings . DISPATCHER_ADDRESS , <EOL> settings . DISPATCHER_PORT ) <EOL> self . commandWatchers = { } <EOL> self . commands = { } <EOL> self . port = settings . PORT <EOL> self . computerName = COMPUTER_NAME_TEMPLATE % ( settings . ADDRESS , <EOL> settings . PORT ) <EOL> self . createDate = time . time ( ) <EOL> self . lastSysInfosMessageTime = <NUM_LIT:0> <EOL> self . lastFullSysInfoUpdate = <NUM_LIT:0> <EOL> self . registerDate = <NUM_LIT:0> <EOL> self . httpconn = httplib . HTTPConnection ( settings . DISPATCHER_ADDRESS , settings . DISPATCHER_PORT ) <EOL> self . PID_DIR = os . path . dirname ( settings . PIDFILE ) <EOL> if not os . path . isdir ( self . PID_DIR ) : <EOL> LOGGER . warning ( "<STR_LIT>" % self . PID_DIR ) <EOL> try : <EOL> os . makedirs ( self . PID_DIR , <NUM_LIT:0> <NUM_LIT> ) <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> except OSError : <EOL> LOGGER . error ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> elif not os . access ( self . PID_DIR , os . R_OK | os . W_OK ) : <EOL> LOGGER . error ( "<STR_LIT>" , self . PID_DIR ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self . status = rendernode . RN_BOOTING <EOL> self . updateSys = False <EOL> self . isPaused = False <EOL> self . toberestarted = False <EOL> self . speed = <NUM_LIT:1.0> <EOL> self . cpuName = "<STR_LIT>" <EOL> self . distrib = "<STR_LIT>" <EOL> self . mikdistrib = "<STR_LIT>" <EOL> self . openglversion = "<STR_LIT>" <EOL> def prepare ( self ) : <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> for name in ( name for name in dir ( settings ) if name . isupper ( ) ) : <EOL> LOGGER . info ( "<STR_LIT>" , name , getattr ( settings , name ) ) <EOL> self . registerWorker ( ) <EOL> def getNbCores ( self ) : <EOL> import multiprocessing <EOL> return multiprocessing . cpu_count ( ) <EOL> def getTotalMemory ( self ) : <EOL> memTotal = <NUM_LIT> <EOL> if os . path . isfile ( '<STR_LIT>' ) : <EOL> try : <EOL> f = open ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> for line in f . readlines ( ) : <EOL> if line . split ( ) [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> memTotal = line . split ( ) [ <NUM_LIT:1> ] <EOL> f . close ( ) <EOL> break <EOL> except : <EOL> pass <EOL> return int ( memTotal ) / <NUM_LIT> <EOL> def getFreeMem ( self , pUnit = MEGABYTES ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> freeMemStr = subprocess . Popen ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] , stdout = PIPE ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> except OSError , e : <EOL> LOGGER . warning ( "<STR_LIT>" , e ) <EOL> if freeMemStr == '<STR_LIT>' : <EOL> return - <NUM_LIT:1> <EOL> freeMem = int ( freeMemStr ) <EOL> if pUnit is MEGABYTES : <EOL> freeMem = int ( freeMem / <NUM_LIT> ) <EOL> elif pUnit is GIGABYTES : <EOL> freeMem = int ( freeMem / ( <NUM_LIT> * <NUM_LIT> ) ) <EOL> return freeMem <EOL> def getSwapUsage ( self ) : <EOL> """<STR_LIT>""" <EOL> swapUsage = <NUM_LIT:0.0> <EOL> try : <EOL> try : <EOL> swapUsage = psutil . swap_memory ( ) . percent <EOL> except psutil . Error : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> except NameError , e : <EOL> LOGGER . debug ( "<STR_LIT>" % e ) <EOL> except Exception , e : <EOL> LOGGER . warning ( "<STR_LIT>" % e ) <EOL> return swapUsage <EOL> def getCpuInfo ( self ) : <EOL> if os . path . isfile ( '<STR_LIT>' ) : <EOL> try : <EOL> f = open ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> for line in f . readlines ( ) : <EOL> if '<STR_LIT>' in line : <EOL> self . cpuName = line . split ( '<STR_LIT::>' ) [ <NUM_LIT:1> ] . strip ( ) <EOL> speedStr = line . split ( '<STR_LIT:@>' ) [ <NUM_LIT:1> ] . strip ( ) <EOL> self . speed = speedStr . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . strip ( ) <EOL> break <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> def getDistribName ( self ) : <EOL> '''<STR_LIT>''' <EOL> if os . path . isfile ( '<STR_LIT>' ) : <EOL> try : <EOL> f = open ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> for line in f . readlines ( ) : <EOL> if '<STR_LIT>' in line or '<STR_LIT>' in line : <EOL> self . mikdistrib = line . split ( ) [ <NUM_LIT:1> ] <EOL> elif '<STR_LIT>' in line : <EOL> if '<STR_LIT:=>' in line : <EOL> self . distrib = line . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] . strip ( ) <EOL> else : <EOL> self . distrib = line <EOL> break <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> elif os . path . isfile ( '<STR_LIT>' ) : <EOL> try : <EOL> f = open ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> for line in f . readlines ( ) : <EOL> if '<STR_LIT>' in line : <EOL> self . mikdistrib = line . split ( ) [ <NUM_LIT:1> ] <EOL> elif '<STR_LIT>' in line : <EOL> if '<STR_LIT:=>' in line : <EOL> self . distrib = line . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] . strip ( ) <EOL> else : <EOL> self . distrib = line <EOL> break <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> else : <EOL> self . mikdistrib = "<STR_LIT>" <EOL> self . distrib = "<STR_LIT>" <EOL> def getOpenglVersion ( self ) : <EOL> import subprocess <EOL> import re <EOL> p = subprocess . Popen ( "<STR_LIT>" , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> output , errors = p . communicate ( ) <EOL> outputList = output . split ( "<STR_LIT:\n>" ) <EOL> for line in outputList : <EOL> if "<STR_LIT>" in line : <EOL> LOGGER . info ( "<STR_LIT>" % line ) <EOL> oglpattern = re . compile ( "<STR_LIT>" ) <EOL> res = oglpattern . search ( line ) <EOL> self . openglversion = res . group ( ) <EOL> break <EOL> def updateSysInfos ( self , ticket ) : <EOL> self . updateSys = True <EOL> def fetchSysInfos ( self ) : <EOL> infos = { } <EOL> if self . updateSys : <EOL> self . getCpuInfo ( ) <EOL> self . getDistribName ( ) <EOL> self . getOpenglVersion ( ) <EOL> infos [ '<STR_LIT>' ] = self . getNbCores ( ) <EOL> infos [ '<STR_LIT>' ] = self . getTotalMemory ( ) <EOL> infos [ '<STR_LIT>' ] = self . getFreeMem ( ) <EOL> infos [ '<STR_LIT>' ] = self . getSwapUsage ( ) <EOL> infos [ "<STR_LIT>" ] = settings . VERSION <EOL> infos [ "<STR_LIT>" ] = self . createDate <EOL> self . updateSys = False <EOL> infos [ '<STR_LIT>' ] = { "<STR_LIT>" : platform . system ( ) . lower ( ) , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : self . cpuName , <EOL> "<STR_LIT>" : self . distrib , <EOL> "<STR_LIT>" : self . mikdistrib , <EOL> "<STR_LIT>" : self . openglversion } <EOL> infos [ '<STR_LIT:name>' ] = self . computerName <EOL> infos [ '<STR_LIT:port>' ] = self . port <EOL> infos [ '<STR_LIT:status>' ] = self . status <EOL> infos [ '<STR_LIT>' ] = [ ] <EOL> infos [ '<STR_LIT>' ] = float ( self . speed ) <EOL> return infos <EOL> def registerWorker ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . updateSys = True <EOL> self . registerDate = time . time ( ) <EOL> infos = self . fetchSysInfos ( ) <EOL> infos [ "<STR_LIT>" ] = self . createDate <EOL> infos [ "<STR_LIT>" ] = settings . VERSION <EOL> dct = json . dumps ( infos ) <EOL> headers = { } <EOL> headers [ '<STR_LIT>' ] = len ( dct ) <EOL> while True : <EOL> try : <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> url = "<STR_LIT>" % self . computerName <EOL> resp = self . requestManager . post ( url , dct , headers ) <EOL> except RequestManager . RequestError , e : <EOL> if e . status != <NUM_LIT> : <EOL> msg = "<STR_LIT>" <EOL> msg %= ( settings . DISPATCHER_ADDRESS , settings . DISPATCHER_PORT ) <EOL> LOGGER . exception ( msg ) <EOL> else : <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> break <EOL> else : <EOL> if resp == '<STR_LIT>' : <EOL> LOGGER . warning ( '<STR_LIT>' ) <EOL> else : <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> break <EOL> time . sleep ( config . WORKER_REGISTER_DELAY_AFTER_FAILURE ) <EOL> if os . path . isfile ( settings . KILLFILE ) : <EOL> self . pauseWorker ( True , False ) <EOL> else : <EOL> self . pauseWorker ( False , False ) <EOL> self . sendSysInfosMessage ( ) <EOL> def buildUpdateDict ( self , command ) : <EOL> dct = { } <EOL> if command . completion is not None : <EOL> dct [ "<STR_LIT>" ] = command . completion <EOL> if command . status is not None : <EOL> dct [ "<STR_LIT:status>" ] = command . status <EOL> if command . validatorMessage is not None : <EOL> dct [ "<STR_LIT>" ] = command . validatorMessage <EOL> if command . errorInfos is not None : <EOL> dct [ "<STR_LIT>" ] = command . errorInfos <EOL> if hasattr ( command , '<STR_LIT>' ) : <EOL> dct [ "<STR_LIT>" ] = command . stats <EOL> dct [ '<STR_LIT:message>' ] = command . message <EOL> dct [ '<STR_LIT:id>' ] = command . id <EOL> return dct <EOL> def updateCommandWatcher ( self , commandWatcher ) : <EOL> """<STR_LIT>""" <EOL> maxRetry = max ( <NUM_LIT:1> , config . WORKER_REQUEST_MAX_RETRY_COUNT ) <EOL> delayRetry = config . WORKER_REQUEST_DELAY_AFTER_REQUEST_FAILURE <EOL> i = <NUM_LIT:0> <EOL> while i < maxRetry : <EOL> url = "<STR_LIT>" % ( self . computerName , commandWatcher . commandId ) <EOL> body = json . dumps ( self . buildUpdateDict ( commandWatcher . command ) ) <EOL> headers = { '<STR_LIT>' : len ( body ) } <EOL> try : <EOL> self . httpconn . request ( '<STR_LIT>' , url , body , headers ) <EOL> response = self . httpconn . getresponse ( ) <EOL> except httplib . HTTPException , e : <EOL> LOGGER . error ( '<STR_LIT>' , url , e ) <EOL> except socket . error , e : <EOL> LOGGER . error ( '<STR_LIT>' , url , e ) <EOL> except Exception , e : <EOL> LOGGER . info ( '<STR_LIT>' , url , e ) <EOL> else : <EOL> if response . status == <NUM_LIT:200> : <EOL> response = response . read ( ) <EOL> commandWatcher . modified = False <EOL> break <EOL> elif response . status == <NUM_LIT> : <EOL> LOGGER . warning ( '<STR_LIT>' , commandWatcher . commandId ) <EOL> response = response . read ( ) <EOL> self . removeCommandWatcher ( commandWatcher ) <EOL> break <EOL> else : <EOL> data = response . read ( ) <EOL> LOGGER . warning ( "<STR_LIT>" % ( response . status , response . reason , data ) ) <EOL> finally : <EOL> self . httpconn . close ( ) <EOL> LOGGER . warning ( '<STR_LIT>' , commandWatcher . commandId , i , maxRetry ) <EOL> LOGGER . warning ( '<STR_LIT>' % delayRetry ) <EOL> time . sleep ( delayRetry ) <EOL> i += <NUM_LIT:1> <EOL> delayRetry *= <NUM_LIT> <EOL> def pauseWorker ( self , paused , killproc ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> url = "<STR_LIT>" % ( self . computerName ) <EOL> dct = { } <EOL> dct [ '<STR_LIT>' ] = paused <EOL> dct [ '<STR_LIT>' ] = killproc <EOL> body = json . dumps ( dct ) <EOL> headers = { '<STR_LIT>' : len ( body ) } <EOL> try : <EOL> self . httpconn . request ( '<STR_LIT>' , url , body , headers ) <EOL> response = self . httpconn . getresponse ( ) <EOL> except httplib . HTTPException : <EOL> LOGGER . exception ( '<STR_LIT>' , url ) <EOL> except socket . error : <EOL> LOGGER . exception ( '<STR_LIT>' , url ) <EOL> else : <EOL> if response . status == <NUM_LIT:200> : <EOL> if paused : <EOL> self . status = rendernode . RN_PAUSED <EOL> self . isPaused = True <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> else : <EOL> self . status = rendernode . RN_IDLE <EOL> self . isPaused = False <EOL> LOGGER . info ( "<STR_LIT>" ) <EOL> return <EOL> finally : <EOL> self . httpconn . close ( ) <EOL> def killCommandWatchers ( self ) : <EOL> for commandWatcher in self . commandWatchers . values ( ) : <EOL> LOGGER . warning ( "<STR_LIT>" , commandWatcher . commandId ) <EOL> commandWatcher . processObj . kill ( ) <EOL> commandWatcher . finished = True <EOL> self . ensureNoMoreRender ( ) <EOL> @ staticmethod <EOL> def getToKeepPIDs ( ) : <EOL> result = [ ] <EOL> for pIDFileStr in settings . PIDFILES_TO_KEEP : <EOL> try : <EOL> pIDFile = open ( pIDFileStr , '<STR_LIT:r>' ) <EOL> if pIDFile : <EOL> pid = pIDFile . read ( ) <EOL> if pid : <EOL> result . append ( int ( pid . rstrip ( '<STR_LIT:\n>' ) ) ) <EOL> except : <EOL> continue <EOL> return result <EOL> def ensureNoMoreRender ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> keepPID = [ os . getpid ( ) , os . getppid ( ) ] . extend ( self . getToKeepPIDs ( ) ) <EOL> effectiveUID = os . geteuid ( ) <EOL> LOGGER . debug ( "<STR_LIT>" % keepPID ) <EOL> renderProcessList = subprocess . check_output ( [ "<STR_LIT>" , "<STR_LIT>" , str ( effectiveUID ) , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:h>" ] ) . split ( ) <EOL> killPID = [ pid for pid in renderProcessList if int ( pid ) not in keepPID ] <EOL> for pid in killPID : <EOL> try : <EOL> os . kill ( int ( pid ) , signal . SIGKILL ) <EOL> LOGGER . debug ( "<STR_LIT>" % pid ) <EOL> except OSError : <EOL> LOGGER . debug ( "<STR_LIT>" % pid ) <EOL> continue <EOL> except Exception as err : <EOL> LOGGER . error ( "<STR_LIT>" % err ) <EOL> return False <EOL> return True <EOL> def getKillfileInfo ( self ) : <EOL> """<STR_LIT>""" <EOL> killfileExists = os . path . isfile ( settings . KILLFILE ) <EOL> killfileContent = None <EOL> if killfileExists : <EOL> with open ( settings . KILLFILE , '<STR_LIT:r>' ) as f : <EOL> data = f . read ( ) <EOL> if len ( data ) != <NUM_LIT:0> : <EOL> try : <EOL> killfileContent = int ( data ) <EOL> except ValueError : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> return { <EOL> '<STR_LIT>' : killfileExists , <EOL> '<STR_LIT:content>' : killfileContent <EOL> } <EOL> def setKillfileInfo ( self , content = None ) : <EOL> """<STR_LIT>""" <EOL> if content is None : <EOL> LOGGER . warning ( '<STR_LIT>' ) <EOL> with open ( settings . KILLFILE , '<STR_LIT:w>' ) as f : <EOL> pass <EOL> elif content in [ - <NUM_LIT:1> , - <NUM_LIT:2> , - <NUM_LIT:3> ] : <EOL> with open ( settings . KILLFILE , '<STR_LIT:w>' ) as f : <EOL> try : <EOL> f . write ( str ( content ) ) <EOL> except IOError as e : <EOL> LOGGER . warning ( "<STR_LIT>" % ( settings . KILLFILE , content ) ) <EOL> return False <EOL> else : <EOL> LOGGER . warning ( "<STR_LIT>" % content ) <EOL> return False <EOL> return True <EOL> def removeKillfile ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . path . isfile ( settings . KILLFILE ) : <EOL> try : <EOL> os . remove ( settings . KILLFILE ) <EOL> LOGGER . warning ( "<STR_LIT>" % settings . KILLFILE ) <EOL> except Exception as e : <EOL> LOGGER . warning ( "<STR_LIT>" % ( settings . KILLFILE , e ) ) <EOL> return False <EOL> return True <EOL> def mainLoop ( self ) : <EOL> """<STR_LIT>""" <EOL> now = time . time ( ) <EOL> killfileInfo = self . getKillfileInfo ( ) <EOL> if killfileInfo . get ( '<STR_LIT>' ) : <EOL> if not self . isPaused : <EOL> if killfileInfo . get ( '<STR_LIT:content>' ) == - <NUM_LIT:1> : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> self . killCommandWatchers ( ) <EOL> self . pauseWorker ( paused = True , killproc = True ) <EOL> self . setKillfileInfo ( None ) <EOL> if killfileInfo . get ( '<STR_LIT:content>' ) == - <NUM_LIT:2> : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> self . killCommandWatchers ( ) <EOL> self . pauseWorker ( paused = True , killproc = True ) <EOL> self . toberestarted = True <EOL> self . removeKillfile ( ) <EOL> if killfileInfo . get ( '<STR_LIT:content>' ) == - <NUM_LIT:3> : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> self . toberestarted = True <EOL> self . killCommandWatchers ( ) <EOL> self . pauseWorker ( paused = True , killproc = True ) <EOL> self . removeKillfile ( ) <EOL> else : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> self . pauseWorker ( paused = True , killproc = False ) <EOL> else : <EOL> if killfileInfo . get ( '<STR_LIT:content>' ) == - <NUM_LIT:1> : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> self . killCommandWatchers ( ) <EOL> self . pauseWorker ( paused = True , killproc = True ) <EOL> self . setKillfileInfo ( None ) <EOL> if killfileInfo . get ( '<STR_LIT:content>' ) == - <NUM_LIT:2> : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> self . toberestarted = True <EOL> self . killCommandWatchers ( ) <EOL> self . pauseWorker ( paused = True , killproc = True ) <EOL> self . setKillfileInfo ( None ) <EOL> if killfileInfo . get ( '<STR_LIT:content>' ) == - <NUM_LIT:3> : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> self . toberestarted = True <EOL> self . killCommandWatchers ( ) <EOL> self . pauseWorker ( paused = True , killproc = True ) <EOL> self . setKillfileInfo ( None ) <EOL> else : <EOL> if self . isPaused : <EOL> self . pauseWorker ( False , False ) <EOL> if self . toberestarted : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> self . framework . stop ( ) <EOL> try : <EOL> pid , stat = os . waitpid ( - <NUM_LIT:1> , os . WNOHANG ) <EOL> if pid : <EOL> LOGGER . info ( "<STR_LIT>" % str ( pid ) ) <EOL> for commandWatcher in self . commandWatchers . values ( ) : <EOL> if pid == commandWatcher . processId : <EOL> if commandWatcher . command . status == COMMAND . CMD_RUNNING : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> newStatus = COMMAND . CMD_ERROR <EOL> else : <EOL> LOGGER . warning ( "<STR_LIT>" , commandWatcher . command . status ) <EOL> newStatus = commandWatcher . command . status <EOL> LOGGER . warning ( "<STR_LIT>" , commandWatcher ) <EOL> commandWatcher . finished = True <EOL> self . updateCompletionAndStatus ( commandWatcher . commandId , commandWatcher . command . completion , newStatus , "<STR_LIT>" ) <EOL> except OSError : <EOL> pass <EOL> for commandWatcher in self . modifiedCommandWatchers : <EOL> self . updateCommandWatcher ( commandWatcher ) <EOL> for commandWatcher in self . finishedCommandWatchers : <EOL> LOGGER . info ( "<STR_LIT>" , commandWatcher . command . id , commandWatcher . command . status , commandWatcher . finished , commandWatcher . modified ) <EOL> self . removeCommandWatcher ( commandWatcher ) <EOL> for commandWatcher in self . commandWatchers . values ( ) : <EOL> if commandWatcher . timeOut and commandWatcher . command . status == COMMAND . CMD_RUNNING : <EOL> responding = ( now - commandWatcher . startTime ) <= commandWatcher . timeOut <EOL> if not responding : <EOL> LOGGER . warning ( "<STR_LIT>" , commandWatcher . commandId ) <EOL> commandWatcher . processObj . kill ( ) <EOL> commandWatcher . finished = True <EOL> self . updateCompletionAndStatus ( commandWatcher . commandId , None , COMMAND . CMD_CANCELED , None ) <EOL> now = time . time ( ) <EOL> if ( now - self . lastFullSysInfoUpdate ) > config . WORKER_MAX_SYSINFO_DELAY : <EOL> self . updateSysInfos ( <NUM_LIT:0> ) <EOL> self . lastFullSysInfoUpdate = now <EOL> if ( now - self . lastSysInfosMessageTime ) > config . WORKER_SYSINFO_DELAY : <EOL> self . sendSysInfosMessage ( ) <EOL> self . lastSysInfosMessageTime = now <EOL> self . httpconn . close ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> def sendSysInfosMessage ( self ) : <EOL> """<STR_LIT>""" <EOL> infos = { } <EOL> if self . updateSys : <EOL> infos = self . fetchSysInfos ( ) <EOL> self . updateSys = False <EOL> infos [ '<STR_LIT:status>' ] = self . status <EOL> infos [ '<STR_LIT>' ] = self . getFreeMem ( ) <EOL> infos [ '<STR_LIT>' ] = self . getSwapUsage ( ) <EOL> dct = json . dumps ( infos ) <EOL> headers = { } <EOL> headers [ '<STR_LIT>' ] = len ( dct ) <EOL> try : <EOL> self . requestManager . put ( "<STR_LIT>" % self . computerName , dct , headers ) <EOL> except RequestManager . RequestError , err : <EOL> if err . status == <NUM_LIT> : <EOL> self . registerWorker ( ) <EOL> else : <EOL> raise <EOL> except httplib . BadStatusLine : <EOL> LOGGER . exception ( '<STR_LIT>' ) <EOL> LOGGER . debug ( '<STR_LIT>' % dct ) <EOL> def connect ( self ) : <EOL> return httplib . HTTPConnection ( settings . DISPATCHER_ADDRESS , settings . DISPATCHER_PORT ) <EOL> def removeCommandWatcher ( self , commandWatcher ) : <EOL> print "<STR_LIT>" % commandWatcher . command . id <EOL> del self . commandWatchers [ commandWatcher . commandId ] <EOL> del self . commands [ commandWatcher . commandId ] <EOL> try : <EOL> os . remove ( commandWatcher . processObj . pidfile ) <EOL> if self . status is not rendernode . RN_PAUSED : <EOL> self . status = rendernode . RN_IDLE <EOL> except OSError , e : <EOL> from errno import ENOENT <EOL> err , msg = e . args <EOL> LOGGER . exception ( msg ) <EOL> if err != ENOENT : <EOL> raise <EOL> def updateCompletionAndStatus ( self , commandId , completion , status , message , stats = None ) : <EOL> try : <EOL> commandWatcher = self . commandWatchers [ commandId ] <EOL> except KeyError : <EOL> LOGGER . warning ( "<STR_LIT>" , commandId ) <EOL> else : <EOL> commandWatcher . modified = True <EOL> if commandWatcher . command . status == COMMAND . CMD_CANCELED : <EOL> return <EOL> if completion is not None : <EOL> commandWatcher . command . completion = completion <EOL> if message is not None : <EOL> commandWatcher . command . message = message <EOL> if status is not None : <EOL> commandWatcher . command . status = status <EOL> if COMMAND . isFinalStatus ( status ) : <EOL> commandWatcher . finished = True <EOL> commandWatcher . command . stats = stats <EOL> def addCommandApply ( self , ticket , commandId , runner , arguments , validationExpression , taskName , relativePathToLogDir , environment , runnerPackages = None , watcherPackages = None ) : <EOL> if not self . isPaused : <EOL> try : <EOL> newCommand = Command ( commandId , runner , arguments , validationExpression , taskName , relativePathToLogDir , environment = environment , runnerPackages = runnerPackages , watcherPackages = watcherPackages ) <EOL> self . commands [ commandId ] = newCommand <EOL> self . addCommandWatcher ( newCommand ) <EOL> LOGGER . info ( "<STR_LIT>" , commandId , runner , repr ( arguments ) ) <EOL> except Exception , e : <EOL> LOGGER . error ( "<STR_LIT>" % e ) <EOL> raise e <EOL> else : <EOL> raise WorkerInternalException ( "<STR_LIT>" ) <EOL> def stopCommandApply ( self , ticket , commandId ) : <EOL> try : <EOL> commandWatcher = self . commandWatchers [ commandId ] <EOL> except KeyError : <EOL> LOGGER . warning ( "<STR_LIT>" , commandId ) <EOL> else : <EOL> commandWatcher . processObj . kill ( ) <EOL> self . updateCompletionAndStatus ( commandId , <NUM_LIT:0> , COMMAND . CMD_CANCELED , "<STR_LIT>" ) <EOL> self . ensureNoMoreRender ( ) <EOL> LOGGER . info ( "<STR_LIT>" , commandId ) <EOL> def updateCommandApply ( self , ticket , commandId , status , completion , message , stats ) : <EOL> self . updateCompletionAndStatus ( commandId , completion , status , message , stats ) <EOL> def updateCommandValidationApply ( self , ticket , commandId , validatorMessage , errorInfos ) : <EOL> try : <EOL> commandWatcher = self . commandWatchers [ commandId ] <EOL> except KeyError : <EOL> ticket . status = ticket . ERROR <EOL> ticket . message = "<STR_LIT>" <EOL> else : <EOL> commandWatcher . command . validatorMessage = validatorMessage <EOL> commandWatcher . command . errorInfos = errorInfos <EOL> LOGGER . info ( "<STR_LIT>" % ( commandId , validatorMessage , errorInfos ) ) <EOL> def addCommandWatcher ( self , command ) : <EOL> newCommandWatcher = self . CommandWatcher ( ) <EOL> newCommandWatcher . commandId = command . id <EOL> commandId = command . id <EOL> from octopus . commandwatcher import commandwatcher <EOL> logdir = os . path . join ( settings . LOGDIR , command . relativePathToLogDir ) <EOL> outputFile = os . path . join ( logdir , '<STR_LIT>' % ( commandId ) ) <EOL> commandWatcherLogFile = outputFile <EOL> scriptFile = commandwatcher . __file__ [ : - <NUM_LIT:1> ] if commandwatcher . __file__ . endswith ( "<STR_LIT>" ) else commandwatcher . __file__ <EOL> LOGGER . info ( "<STR_LIT>" , scriptFile ) <EOL> workerPort = self . framework . webService . port <EOL> pidFile = os . path . join ( self . PID_DIR , "<STR_LIT>" % newCommandWatcher . commandId ) <EOL> if not os . path . exists ( logdir ) : <EOL> try : <EOL> os . makedirs ( logdir , <NUM_LIT:0> <NUM_LIT> ) <EOL> except OSError , e : <EOL> import errno <EOL> err = e . args [ <NUM_LIT:0> ] <EOL> if err != errno . EEXIST : <EOL> raise <EOL> logFile = file ( outputFile , "<STR_LIT:w>" ) <EOL> d = os . path . dirname ( pidFile ) <EOL> if not os . path . exists ( d ) : <EOL> try : <EOL> os . makedirs ( d , <NUM_LIT:0> <NUM_LIT> ) <EOL> except OSError , e : <EOL> import errno <EOL> err = e . args [ <NUM_LIT:0> ] <EOL> if err != errno . EEXIST : <EOL> raise <EOL> command . environment [ "<STR_LIT>" ] = command . id <EOL> command . environment [ "<STR_LIT>" ] = command . runnerPackages <EOL> command . environment [ "<STR_LIT>" ] = command . taskName <EOL> command . environment [ "<STR_LIT>" ] = command . relativePathToLogDir <EOL> command . environment [ "<STR_LIT>" ] = outputFile <EOL> if '<STR_LIT>' in os . environ : <EOL> pythonExec = "<STR_LIT>" <EOL> runnerPackages = command . runnerPackages if command . runnerPackages != '<STR_LIT>' else '<STR_LIT>' <EOL> else : <EOL> pythonExec = sys . executable <EOL> runnerPackages = "<STR_LIT>" <EOL> args = [ <EOL> pythonExec , <EOL> "<STR_LIT>" , <EOL> scriptFile , <EOL> commandWatcherLogFile , <EOL> str ( settings . DISPATCHER_ADDRESS + "<STR_LIT::>" + str ( settings . DISPATCHER_PORT ) ) , <EOL> str ( workerPort ) , <EOL> str ( command . id ) , <EOL> command . runner , <EOL> command . validationExpression , <EOL> runnerPackages , <EOL> json . dumps ( command . arguments ) <EOL> ] <EOL> try : <EOL> if '<STR_LIT>' in os . environ : <EOL> LOGGER . warning ( "<STR_LIT>" % command . watcherPackages ) <EOL> watcherProcess = spawnRezManagedCommandWatcher ( pidFile , logFile , args , command . watcherPackages , command . environment ) <EOL> else : <EOL> LOGGER . warning ( "<STR_LIT>" ) <EOL> watcherProcess = spawnCommandWatcher ( pidFile , logFile , args , command . environment ) <EOL> newCommandWatcher . processObj = watcherProcess <EOL> newCommandWatcher . startTime = time . time ( ) <EOL> newCommandWatcher . timeOut = None <EOL> newCommandWatcher . command = command <EOL> newCommandWatcher . processId = watcherProcess . pid <EOL> self . commandWatchers [ command . id ] = newCommandWatcher <EOL> self . status = rendernode . RN_WORKING <EOL> LOGGER . info ( "<STR_LIT>" , command . id ) <EOL> except Exception , e : <EOL> LOGGER . error ( "<STR_LIT>" , e ) <EOL> raise e <EOL> def reloadConfig ( self ) : <EOL> reload ( config ) <EOL> pass </s>
