Changeset 6616


Ignore:
Timestamp:
Jan 17, 2018, 5:04:16 PM (21 months ago)
Author:
kbut
Message:

add support to cancel command. Add sleep into exec command . Clean comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin-center/trunk/fuentes/admin-center.install/usr/lib/python2.7/actasks.py

    r2002 r6616  
    1919
    2020class Task:
    21     def __init__(self, taskid, command):
     21    def __init__(self, taskid, command, cancelcommand=''):
    2222        '''
    2323        It is a task in server, with certain taskid and performing certain command
     
    3131        self.status='PREPARED'
    3232        self.command=command
     33        self.cancelcommand = cancelcommand
    3334        self.logfolder="/run/taskmanager"
    3435        self.filepipe=''
     
    9596        temp = tempfile.NamedTemporaryFile(prefix='pipe_', dir=self.logfolder, delete=False)
    9697        proc = subprocess.Popen([self.command+"; sleep 5"],  shell=True, stdout=temp, preexec_fn=os.setsid)
    97         #proc=pexpect.spawn(self.command, logfile=temp)  #
    9898
    9999        # Adding process
    100         self.process=proc;
    101         self.status="RUNNING"
    102         self.filepipe=temp.name
    103        
    104        
     100        self.process = proc
     101        self.status = "RUNNING"
     102        self.filepipe = temp.name
     103
    105104        # Getting process output
    106105        ret=self.process.poll()
    107         print str(ret)
    108106        while ret is None:
    109             #time.sleep(3)
     107            time.sleep(1)
    110108            ret=self.process.poll()
    111            
    112         #print str(ret)
    113        
    114109
    115         '''
    116         # getting proces output with expect
    117         while True:
    118             ret=0;
    119             try:
    120                 proc.expect("\n")
    121                 stdout=proc.before
    122             except pexpect.EOF:
    123                 proc.close()
    124                 ret=proc.exitstatus
    125                 print "FIN"
    126             break
    127         '''
    128                
    129        
    130110        if (str(ret)=='0'):
    131111            self.status="DONE"
    132         elif (str(ret)=='-9'):                 
    133             self.status="CANCELLED"
    134         else: # return code 1 when install fails
    135             self.status="BROKEN"
     112        else:
     113            self.runCancel()
     114            if (str(ret)=='-9'):
     115                self.status="CANCELLED"
     116            else: # return code 1 when install fails
     117                self.status="BROKEN"
    136118
    137119
     
    160142        '''
    161143        # Prepare thread       
    162         taskthread=threading.Thread(target=self.run, args=())
     144        taskthread = threading.Thread(target=self.run, args=())
    163145        taskthread.daemon = True
    164146        taskthread.start()
    165147       
    166148        return True
    167        
    168         ### Exec command ###
    169        
    170        
    171         # Prepare file pipe
    172         #temp = tempfile.NamedTemporaryFile(prefix='pipe_', dir=self.logfolder, delete=False)
    173        
    174         # New exec command, ignoring stderr stdin for now
    175         #proc = subprocess.Popen([self.command],  shell=True, stdout=temp, preexec_fn=os.setsid)
    176        
    177         #self.process=proc
    178         #self.status="RUNNING"
    179         #self.filepipe=temp.name #????
    180        
    181    
     149
     150    def runCancel(self):
    182151        '''
    183            
    184        
    185         except Exception as e:
    186             job['status']="Error"
    187             print '[LmdServer]',e
    188             if (ret is None):
    189                 job['msg']="Err code None (running)"
    190             elif (ret<0):
    191                 job['msg']="Interrupted by signal: "+str(ret)
    192             else:
    193                 job['msg']="Aparently all went well: "+str(ret)
    194            
    195    
    196             # Append result of job and release mutex. Now all inform_me return finish
    197             self.locks[job['taskid']]['result'] = str(ret)
    198             self.locks[job['taskid']]['lock'].release()
    199    
    200             return str(e)
    201        
    202         '''   
     152            Sync method to run cancel command
     153        '''
     154        temp = open(self.filepipe,'a+b')
     155        proc = subprocess.Popen([self.cancelcommand],  shell=True, stdout=temp, preexec_fn=os.setsid).communicate()
    203156
    204        
    205         pass
    206157
    207158    def stop(self):
Note: See TracChangeset for help on using the changeset viewer.