def restart_software_updater(): """ <Purpose> Attempts to start a new software updater, and will exit this one if the new one seems to start successfully. If the new one does not start successfully, then we just return. <Arguments> None <Exceptions> Possible exception if there is problems writing the OK file. <Side Effects> If all goes well, a new softwareupdater will be started, and this one will exit. <Returns> In the successful case, it will not return. If the new softwareupdater does not start correctly, we will return None. """ safe_log( "[restart_software_updater] Attempting to restart software updater.") # find an unused mutex thismutex = get_mutex() # starts new with arg that is the mutex junkupdaterobject = portable_popen.Popen( ["python", "softwareupdater.py", thismutex]) # wait for some time (1 minute) for them to init and stop them if they don't for junkcount in range(30): misc.do_sleep(2.0) # if "OK" file exists, release softwareupdater.old, remove OK file and exit if os.path.exists("softwareupdater.OK." + thismutex): runonce.releaseprocesslock('softwareupdater.old') os.remove("softwareupdater.OK." + thismutex) # I'm happy, it is taking over safe_log( "[restart_software_updater] The new instance of the software updater is running. This one is exiting." ) sys.exit(10) # else write "stop" file because it failed... file("softwareupdater.stop." + thismutex, "w").close() safe_log( "[restart_software_updater] Failed to restart software updater. This instance will continue." ) # I continue normal operation return
def main(): # Give time for any changes in acquiring/relinquishing keys to take effect time.sleep(2) for lockname in locklist: status = runonce.getprocesslock(lockname) if status == True: print "The lock '" + lockname + "' was not being held." runonce.releaseprocesslock(lockname) elif status == False: print "There was an error getting the lock '" + lockname + "'." else: print "The lock '" + lockname + "' is currently being used by process pid: " + str(status)
def main(): # Give time for any changes in acquiring/relinquishing keys to take effect time.sleep(2) for lockname in locklist: status = runonce.getprocesslock(lockname) if status == True: print "The lock '" + lockname + "' was not being held." runonce.releaseprocesslock(lockname) elif status == False: print "There was an error getting the lock '" + lockname + "'." else: print "The lock '" + lockname + "' is currently being used by process pid: " + str( status)
def restart_software_updater(): """ <Purpose> Attempts to start a new software updater, and will exit this one if the new one seems to start successfully. If the new one does not start successfully, then we just return. <Arguments> None <Exceptions> Possible exception if there is problems writing the OK file. <Side Effects> If all goes well, a new softwareupdater will be started, and this one will exit. <Returns> In the successful case, it will not return. If the new softwareupdater does not start correctly, we will return None. """ safe_log("[restart_software_updater] Attempting to restart software updater.") # find an unused mutex thismutex = get_mutex() # starts new with arg that is the mutex junkupdaterobject = portable_popen.Popen(["python", "softwareupdater.py", thismutex]) # wait for some time (1 minute) for them to init and stop them if they don't for junkcount in range(30): misc.do_sleep(2.0) # if "OK" file exists, release softwareupdater.old, remove OK file and exit if os.path.exists("softwareupdater.OK." + thismutex): runonce.releaseprocesslock("softwareupdater.old") os.remove("softwareupdater.OK." + thismutex) # I'm happy, it is taking over safe_log( "[restart_software_updater] The new instance of the software updater is running. This one is exiting." ) sys.exit(10) # else write "stop" file because it failed... file("softwareupdater.stop." + thismutex, "w").close() safe_log("[restart_software_updater] Failed to restart software updater. This instance will continue.") # I continue normal operation return
def fresh_software_updater(): """ <Purpose> This function is ment to be called when starting a softwareupdater when no other is currently running. It will clear away any outdated OK or stop files, then release the softwareupdater.new lock and acquire the softwareupdater.old lock. <Arguments> None <Exceptions> Possible exception if there is a problem removing the OK/stop files. <Side Effects> The softwareupdater.new lock is released. The softwareupdater.old lock is acquired. All old OK and stop files are removed. <Returns> None """ # clean all mutex files for filename in os.listdir('.'): # Remove any outdated stop or OK files... if filename.startswith('softwareupdater.OK.') or filename.startswith( 'softwareupdater.stop.'): os.remove(filename) # Get the process lock for the main part of the program. gotlock = runonce.getprocesslock("softwareupdater.old") # Release the lock on the initialization part of the program runonce.releaseprocesslock('softwareupdater.new') if gotlock == True: # I got the lock. All is well... pass else: if gotlock: safe_log( "[fresh_software_updater] Another software updater old process (pid: " + str(gotlock) + ") is running") sys.exit(55) else: safe_log( "[fresh_software_updater] Another software updater old process is running" ) sys.exit(55) # Should be ready to go... safe_log("[fresh_software_updater] Fresh software updater started.")
def fresh_software_updater(): """ <Purpose> This function is ment to be called when starting a softwareupdater when no other is currently running. It will clear away any outdated OK or stop files, then release the softwareupdater.new lock and acquire the softwareupdater.old lock. <Arguments> None <Exceptions> Possible exception if there is a problem removing the OK/stop files. <Side Effects> The softwareupdater.new lock is released. The softwareupdater.old lock is acquired. All old OK and stop files are removed. <Returns> None """ # clean all mutex files for filename in os.listdir("."): # Remove any outdated stop or OK files... if filename.startswith("softwareupdater.OK.") or filename.startswith("softwareupdater.stop."): os.remove(filename) # Get the process lock for the main part of the program. gotlock = runonce.getprocesslock("softwareupdater.old") # Release the lock on the initialization part of the program runonce.releaseprocesslock("softwareupdater.new") if gotlock == True: # I got the lock. All is well... pass else: if gotlock: safe_log( "[fresh_software_updater] Another software updater old process (pid: " + str(gotlock) + ") is running" ) sys.exit(55) else: safe_log("[fresh_software_updater] Another software updater old process is running") sys.exit(55) # Should be ready to go... safe_log("[fresh_software_updater] Fresh software updater started.")
def restart_client(filenamelist): """ <Purpose> Restarts the node manager. <Arguments> filenamelist - Currently not used, but is included for possible future use. <Exceptions> None <Side Effects> The current node manager is killed, and a new one is started. <Returns> None. """ # kill nmmain if it is currently running retval = runonce.getprocesslock('seattlenodemanager') if retval == True: safe_log( "[restart_client] Obtained the lock 'seattlenodemanager', it wasn't running." ) # I got the lock, it wasn't running... # we want to start a new one, so lets release runonce.releaseprocesslock('seattlenodemanager') elif retval == False: # Someone has the lock, but I can't do anything... safe_log( "[restart_client] The lock 'seattlenodemanager' is held by an unknown process. Will try to start it anyways." ) else: safe_log("[restart_client] Stopping the nodemanager.") # I know the process ID! Let's stop the process... harshexit.portablekill(retval) safe_log("[restart_client] Starting the nodemanager.") # run the node manager. I rely on it to do the smart thing (handle multiple # instances, etc.) nm_restart_command_args_list = ["python", "nmmain.py"] if run_nodemanager_in_foreground: nm_restart_command_args_list.append('--foreground') junkprocessobject = portable_popen.Popen(nm_restart_command_args_list)
def restart_client(filenamelist): """ <Purpose> Restarts the node manager. <Arguments> filenamelist - Currently not used, but is included for possible future use. <Exceptions> None <Side Effects> The current node manager is killed, and a new one is started. <Returns> None. """ # kill nmmain if it is currently running retval = runonce.getprocesslock("seattlenodemanager") if retval == True: safe_log("[restart_client] Obtained the lock 'seattlenodemanager', it wasn't running.") # I got the lock, it wasn't running... # we want to start a new one, so lets release runonce.releaseprocesslock("seattlenodemanager") elif retval == False: # Someone has the lock, but I can't do anything... safe_log( "[restart_client] The lock 'seattlenodemanager' is held by an unknown process. Will try to start it anyways." ) else: safe_log("[restart_client] Stopping the nodemanager.") # I know the process ID! Let's stop the process... harshexit.portablekill(retval) safe_log("[restart_client] Starting the nodemanager.") # run the node manager. I rely on it to do the smart thing (handle multiple # instances, etc.) nm_restart_command_args_list = ["python", "nmmain.py"] if run_nodemanager_in_foreground: nm_restart_command_args_list.append("--foreground") junkprocessobject = portable_popen.Popen(nm_restart_command_args_list)
def software_updater_start(mutexname): """ <Purpose> When restarting the software updater, this method is called in the new one. It will write an OK file to let the original know it has started, then will wait for the original to acknowledge by either removing the OK file, meaning we should carry on, or by writing a stop file, meaning we should exit. Carrying on means getting the softwareupdater.old lock, and releasing the softwareupdater.new lock, then returning. <Arguments> mutexname - The new software updater was started with a given mutex name, which is used to uniquely identify the stop and OK files as coming from this softwareupdater. This way the old one can know that the softwareupdater it started is the one that is continueing on. <Exceptions> Possible Exception creating the OK file. <Side Effects> Acquires the softwareupdater.old lock and releases the softwareupdater.new lock. <Return> None """ safe_log("[software_updater_start] This is a new software updater process started by an existing one.") # if "stop" file exists, then exit if os.path.exists("softwareupdater.stop."+mutexname): safe_log("[software_updater_start] There's a stop file. Exiting.") sys.exit(2) # write "OK" file file("softwareupdater.OK."+mutexname,"w").close() # while "OK" file exists while os.path.exists("softwareupdater.OK."+mutexname): safe_log("[software_updater_start] Waiting for the file softwareupdater.OK."+mutexname+" to be removed.") sleep(1.0) # if "stop" file exists, then exit if os.path.exists("softwareupdater.stop."+mutexname): sys.exit(3) # Get the process lock for the main part of the program. gotlock = runonce.getprocesslock("softwareupdater.old") # Release the lock on the initialization part of the program runonce.releaseprocesslock('softwareupdater.new') if gotlock == True: # I got the lock. All is well... pass else: if gotlock: safe_log("[software_updater_start] Another software updater old process (pid: "+str(gotlock)+") is running") sys.exit(55) else: safe_log("[software_updater_start] Another software updater old process is running") sys.exit(55) safe_log("[software_updater_start] This software updater process is now taking over.") # start normal operation return
def software_updater_start(mutexname): """ <Purpose> When restarting the software updater, this method is called in the new one. It will write an OK file to let the original know it has started, then will wait for the original to acknowledge by either removing the OK file, meaning we should carry on, or by writing a stop file, meaning we should exit. Carrying on means getting the softwareupdater.old lock, and releasing the softwareupdater.new lock, then returning. <Arguments> mutexname - The new software updater was started with a given mutex name, which is used to uniquely identify the stop and OK files as coming from this softwareupdater. This way the old one can know that the softwareupdater it started is the one that is continueing on. <Exceptions> Possible Exception creating the OK file. <Side Effects> Acquires the softwareupdater.old lock and releases the softwareupdater.new lock. <Return> None """ safe_log("[software_updater_start] This is a new software updater process started by an existing one.") # if "stop" file exists, then exit if os.path.exists("softwareupdater.stop."+mutexname): safe_log("[software_updater_start] There's a stop file. Exiting.") sys.exit(2) # write "OK" file file("softwareupdater.OK."+mutexname,"w").close() # while "OK" file exists while os.path.exists("softwareupdater.OK."+mutexname): safe_log("[software_updater_start] Waiting for the file softwareupdater.OK."+mutexname+" to be removed.") misc.do_sleep(1.0) # if "stop" file exists, then exit if os.path.exists("softwareupdater.stop."+mutexname): sys.exit(3) # Get the process lock for the main part of the program. gotlock = runonce.getprocesslock("softwareupdater.old") # Release the lock on the initialization part of the program runonce.releaseprocesslock('softwareupdater.new') if gotlock == True: # I got the lock. All is well... pass else: if gotlock: safe_log("[software_updater_start] Another software updater old process (pid: "+str(gotlock)+") is running") sys.exit(55) else: safe_log("[software_updater_start] Another software updater old process is running") sys.exit(55) safe_log("[software_updater_start] This software updater process is now taking over.") # start normal operation return
runonce.getprocesslock(str(os.getpid())) print "my process id is:"+str(os.getpid()) retval = runonce.getprocesslock(lockname) if retval == True: print "I have the mutex" elif retval == False: print "Another process has the mutex (owned by another user most likely)" else: print "Process "+str(retval)+" has the mutex!" while True: for num in range(random.randint(0,5)): time.sleep(2) if runonce.stillhaveprocesslock(lockname): print "I have the mutex" else: print "I do not have the mutex" if runonce.stillhaveprocesslock(str(os.getpid())): print "I have my mutex" else: print "I do not have my mutex" time.sleep(2) print "releasing mutex (if possible)" runonce.releaseprocesslock(lockname)
lockname = "seattletestlock" runonce.getprocesslock(str(os.getpid())) print "my process id is:" + str(os.getpid()) retval = runonce.getprocesslock(lockname) if retval == True: print "I have the mutex" elif retval == False: print "Another process has the mutex (owned by another user most likely)" else: print "Process " + str(retval) + " has the mutex!" while True: for num in range(random.randint(0, 5)): time.sleep(2) if runonce.stillhaveprocesslock(lockname): print "I have the mutex" else: print "I do not have the mutex" if runonce.stillhaveprocesslock(str(os.getpid())): print "I have my mutex" else: print "I do not have my mutex" time.sleep(2) print "releasing mutex (if possible)" runonce.releaseprocesslock(lockname)