def test_write_pid_file(self): self.inst.startApplication() #get pid/procname of current process this_pid = os.getpid() this_procname = tools.processName(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_autoExit_unique_process(self): self.inst = ApplicationInstance(os.path.abspath(self.file_name), autoExit = True) self.assertExists(self.file_name) this_pid = os.getpid() this_procname = tools.processName(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_autoExit_other_running_process(self): pid = self.createProcess() procname = tools.processName(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), autoExit=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.processName(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 startApplication(self): """ Called when the single instance starts to save its pid """ pid = os.getpid() procname = tools.processName(pid) try: with open(self.pidFile, '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 startApplication(self): """ Called when the single instance starts to save it's pid """ pid = os.getpid() procname = tools.processName(pid) try: with open(self.pidFile, '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.processName(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()) self.assertTrue(self.inst.busy())
def check(self, autoExit=False): """ Check if the current application is already running Args: autoExit (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.pidFile): 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.processAlive(self.pid): return True #check if the process has the same procname #check cmdline for backwards compatibility if self.procname and \ self.procname != tools.processName(self.pid) and \ self.procname != tools.processCmdline(self.pid): return True if autoExit: #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 check(self, autoExit = False): """ Check if the current application is already running Args: autoExit (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.pidFile): 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.processAlive(self.pid): return True #check if the process has the same procname #check cmdline for backwards compatibility if self.procname and \ self.procname != tools.processName(self.pid) and \ self.procname != tools.processCmdline(self.pid): return True if autoExit: #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_processName(self): pid = self.createProcess() self.assertEqual(tools.processName(pid), generic.DUMMY[:15])