#NEWLINE# def main( ) : #NEWLINE# #INDENT# debug = int ( os . environ [ 'GNT_DEBUG' ] ) #NEWLINE# logname = pathutils . GetLogFilename ( 'jobs' ) #NEWLINE# utils . SetupLogging ( logname , 'job-startup' , debug = debug ) #NEWLINE# ( job_id , livelock_name , secret_params_serialized ) = _GetMasterInfo ( ) #NEWLINE# secret_params = '' #NEWLINE# if secret_params_serialized : #NEWLINE# #INDENT# secret_params_json = serializer . LoadJson ( secret_params_serialized ) #NEWLINE# secret_params = RestorePrivateValueWrapping ( secret_params_json ) #NEWLINE# #UNINDENT# utils . SetupLogging ( logname , ( 'job-%s' % ( job_id , ) ) , debug = debug ) #NEWLINE# try : #NEWLINE# #INDENT# logging . debug ( 'Preparing the context and the configuration' ) #NEWLINE# context = masterd . GanetiContext ( livelock_name ) #NEWLINE# logging . debug ( 'Registering signal handlers' ) #NEWLINE# cancel = [ False ] #NEWLINE# prio_change = [ False ] #NEWLINE# def _TermHandler( signum , _frame ) : #NEWLINE# #INDENT# logging . info ( 'Killed by signal %d' , signum ) #NEWLINE# cancel [ 0 ] = True #NEWLINE# #UNINDENT# signal . signal ( signal . SIGTERM , _TermHandler ) #NEWLINE# def _HupHandler( signum , _frame ) : #NEWLINE# #INDENT# logging . debug ( 'Received signal %d, old flag was %s, will set to True' , signum , mcpu . sighupReceived ) #NEWLINE# mcpu . sighupReceived [ 0 ] = True #NEWLINE# #UNINDENT# signal . signal ( signal . SIGHUP , _HupHandler ) #NEWLINE# def _User1Handler( signum , _frame ) : #NEWLINE# #INDENT# logging . info ( 'Received signal %d, indicating priority change' , signum ) #NEWLINE# prio_change [ 0 ] = True #NEWLINE# #UNINDENT# signal . signal ( signal . SIGUSR1 , _User1Handler ) #NEWLINE# job = JobQueue . SafeLoadJobFromDisk ( context . jobqueue , job_id , False ) #NEWLINE# job . SetPid ( os . getpid ( ) ) #NEWLINE# if secret_params : #NEWLINE# #INDENT# for i in range ( 0 , len ( secret_params ) ) : #NEWLINE# #INDENT# if hasattr ( job . ops [ i ] . input , 'osparams_secret' ) : #NEWLINE# #INDENT# job . ops [ i ] . input . osparams_secret = secret_params [ _TermHandler ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# execfun = mcpu . Processor ( context , job_id , job_id ) . ExecOpCode #NEWLINE# proc = _JobProcessor ( context . jobqueue , execfun , job ) #NEWLINE# result = _JobProcessor . DEFER #NEWLINE# while ( result != _JobProcessor . FINISHED ) : #NEWLINE# #INDENT# result = proc ( ) #NEWLINE# if ( ( result == _JobProcessor . WAITDEP ) and ( not cancel [ 0 ] ) ) : #NEWLINE# #INDENT# logging . warning ( 'Got started despite a dependency not yet finished' ) #NEWLINE# time . sleep ( 5 ) #NEWLINE# #UNINDENT# if cancel [ 0 ] : #NEWLINE# #INDENT# logging . debug ( 'Got cancel request, cancelling job %d' , job_id ) #NEWLINE# r = context . jobqueue . CancelJob ( job_id ) #NEWLINE# job = JobQueue . SafeLoadJobFromDisk ( context . jobqueue , job_id , False ) #NEWLINE# proc = _JobProcessor ( context . jobqueue , execfun , job ) #NEWLINE# logging . debug ( 'CancelJob result for job %d: %s' , job_id , r ) #NEWLINE# cancel [ 0 ] = False #NEWLINE# #UNINDENT# if prio_change [ 0 ] : #NEWLINE# #INDENT# logging . debug ( 'Received priority-change request' ) #NEWLINE# try : #NEWLINE# #INDENT# fname = os . path . join ( pathutils . LUXID_MESSAGE_DIR , ( '%d.prio' % job_id ) ) #NEWLINE# new_prio = int ( utils . ReadFile ( fname ) ) #NEWLINE# utils . RemoveFile ( fname ) #NEWLINE# logging . debug ( 'Changing priority of job %d to %d' , job_id , new_prio ) #NEWLINE# r = context . jobqueue . ChangeJobPriority ( job_id , new_prio ) #NEWLINE# job = JobQueue . SafeLoadJobFromDisk ( context . jobqueue , job_id , False ) #NEWLINE# proc = _JobProcessor ( context . jobqueue , execfun , job ) #NEWLINE# logging . debug ( 'Result of changing priority of %d to %d: %s' , job_id , new_prio , r ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# logging . warning ( 'Informed of priority change, but could not read new priority' ) #NEWLINE# #UNINDENT# prio_change [ 0 ] = False #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except Exception : #NEWLINE# #INDENT# logging . exception ( 'Exception when trying to run job %d' , job_id ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# logging . debug ( 'Job %d finalized' , job_id ) #NEWLINE# logging . debug ( 'Removing livelock file %s' , livelock_name . GetPath ( ) ) #NEWLINE# os . remove ( livelock_name . GetPath ( ) ) #NEWLINE# #UNINDENT# sys . exit ( 0 )