def test_inexistant_pid(self): # There will be one point where we finally find a free PID for pid in xrange(30000): if not pid_exists(pid): return raise Exception( "Probably a bug in pid_exists or more than 30000 procs!!")
def test_my_own_pid(self): my_pid = os.getpid() self.assertTrue(pid_exists(my_pid)) if not Platform.is_windows(): '''Test is currently not valid under windows, because of the way nosetests is implemented. The command is 'runpy.py', but the executable command line is ['d:\\devtools\\python27\\python.exe', 'D:\\devtools\\python27\\Scripts\\nosetests.exe', 'tests.core'] Removing until we can make an more accurate test ''' self.assertTrue(is_my_process(my_pid))
def start(self, foreground=False): log.info("Starting") pid = self.pid() if pid: # Check if the pid in the pidfile corresponds to a running process if pid_exists(pid): log.error("Not starting, another instance is already running" " (using pidfile {0})".format(self.pidfile)) sys.exit(1) else: log.warn('pidfile contains the pid of a stopped process.' ' Starting normally') log.info("Pidfile: %s" % self.pidfile) if not foreground: self.daemonize() self.write_pidfile() self.run()
def kill_old_process(): """ Kills or brings to the foreground (if possible) any other instance of this program. It avoids multiple icons in the Tray on Windows. On OSX, we don't have to do anything: icons don't get duplicated. """ # On Windows, if a window is already opened, let's just bring it on the foreground if Platform.is_windows(): # Is there another Agent Manager process running ? pidfile = PidFile('agent-manager-gui').get_path() old_pid = None try: pf = file(pidfile, 'r') old_pid = int(pf.read().strip()) pf.close() except (IOError, ValueError): pass if old_pid is not None and pid_exists(old_pid): handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, old_pid) exe_path = win32process.GetModuleFileNameEx(handle, 0) # If (and only if) this process is indeed an instance of the GUI, let's kill it if 'agent-manager.exe' in exe_path: win32api.TerminateProcess(handle, -1) win32api.CloseHandle(handle) # If we reached that point it means the current process should be the only running # agent-manager.exe, let's save its pid pid = str(os.getpid()) try: fp = open(pidfile, 'w+') fp.write(str(pid)) fp.close() except Exception, e: msg = "Unable to write pidfile: %s" % pidfile log.exception(msg) sys.stderr.write(msg + "\n") sys.exit(1)
def kill_old_process(): """ Kills or brings to the foreground (if possible) any other instance of this program. It avoids multiple icons in the Tray on Windows. On OSX, we don't have to do anything: icons don't get duplicated. """ # Is there another Agent Manager process running ? pidfile = PidFile('agent-manager-gui').get_path() old_pid = None try: pf = file(pidfile, 'r') old_pid = int(pf.read().strip()) pf.close() except (IOError, ValueError): pass if old_pid is not None and pid_exists(old_pid): handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, old_pid) exe_path = win32process.GetModuleFileNameEx(handle, 0) # If (and only if) this process is indeed an instance of the GUI, let's kill it if 'agent-manager.exe' in exe_path: win32api.TerminateProcess(handle, -1) win32api.CloseHandle(handle) # If we reached that point it means the current process should be the only running # agent-manager.exe, let's save its pid pid = str(os.getpid()) try: fp = open(pidfile, 'w+') fp.write(str(pid)) fp.close() except Exception, e: msg = "Unable to write pidfile: %s" % pidfile log.exception(msg) sys.stderr.write(msg + "\n") sys.exit(1)
def test_my_own_pid(self): my_pid = os.getpid() self.assertTrue(pid_exists(my_pid)) self.assertTrue(is_my_process(my_pid))
def test_inexistant_pid(self): # There will be one point where we finally find a free PID for pid in xrange(30000): if not pid_exists(pid): return raise Exception("Probably a bug in pid_exists or more than 30000 procs!!")
def test_my_own_pid(self): my_pid = os.getpid() self.assertTrue(pid_exists(my_pid))