def test_write_pid_file(self): self.inst.start_application() #get pid/procname of current process this_pid = os.getpid() this_procname = tools.process_name(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '{}\n{}'.format(this_pid, this_procname))
def test_auto_exit_unique_process(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), auto_exit = True) self.assertTrue(os.path.exists(self.file_name)) this_pid = os.getpid() this_procname = tools.process_name(this_pid) with open(self.file_name, 'rt') as file_with_pid: self.assertEqual(file_with_pid.read(), '{}\n{}'.format(this_pid, this_procname))
def test_auto_exit_other_running_process(self): pid = self.createProcess() procname = tools.process_name(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) with self.assertRaises(SystemExit): self.inst = ApplicationInstance(os.path.abspath(self.file_name), auto_exit = True)
def test_existing_process_with_wrong_pid(self): """ Test the check function with an existing process with wrong pid """ pid = self.createProcess() procname = tools.process_name(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write("987654321\n") file_with_pid.write(procname) # Execute test self.assertTrue(self.inst.check())
def start_application( self ): """ Called when the single instance starts to save it's pid """ pid = os.getpid() procname = tools.process_name(pid) try: with open( self.pid_file, 'wt' ) as f: f.write('{}\n{}'.format(pid, procname)) except OSError as e: logger.error('Failed to write PID file %s: [%s] %s' %(e.filename, e.errno, e.strerror)) self.flockUnlock()
def test_existing_process_with_correct_procname(self): """ Test the check function with an existing process with correct process name """ pid = self.createProcess() procname = tools.process_name(pid) # create file with pid and process name with open(self.file_name, "wt") as file_with_pid: file_with_pid.write(str(pid) + "\n") file_with_pid.write(procname) # Execute test self.assertFalse(self.inst.check())
def check(self, auto_exit=False): """ Check if the current application is already running Args: auto_exit (bool): automatically call sys.exit if there is an other instance running Returns: bool: True if this is the only application instance """ # check if the pidfile exists if not os.path.isfile(self.pid_file): return True self.pid, self.procname = self.readPidFile() # check if the process with specified by pid exists if 0 == self.pid: return True if not tools.is_process_alive(self.pid): return True # check if the process has the same procname # check cmdline for backwards compatibility if ( self.procname and self.procname != tools.process_name(self.pid) and self.procname != tools.process_cmdline(self.pid) ): return True if auto_exit: # exit the application print("The application is already running !") exit(0) # exit raise an exception so don't put it in a try/except block return False
def test_process_name(self): pid = self.createProcess() self.assertEqual(tools.process_name(pid), 'dummy_proc.sh')