-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2173 from devos50/procescheck_refactor
Created a new process checker
- Loading branch information
Showing
5 changed files
with
161 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import os | ||
|
||
from Tribler.Core.SessionConfig import SessionStartupConfig | ||
|
||
|
||
LOCK_FILE_NAME = 'triblerd.lock' | ||
|
||
|
||
class ProcessChecker(object): | ||
""" | ||
This class contains code to check whether a Tribler process is already running. | ||
""" | ||
|
||
def __init__(self): | ||
""" | ||
Check whether a lock file exists in the Tribler directory. If not, create the file. If it exists, | ||
check the PID that is written inside the lock file. | ||
""" | ||
self.already_running = False | ||
|
||
self.statedir = SessionStartupConfig().get_state_dir() | ||
self.lock_file_path = os.path.join(self.statedir, LOCK_FILE_NAME) | ||
|
||
if os.path.exists(self.lock_file_path): | ||
file_pid = self.get_pid_from_lock_file() | ||
if file_pid == str(os.getpid()): | ||
# Ignore when we find our own PID inside the lock file | ||
self.already_running = False | ||
elif file_pid != os.getpid() and not ProcessChecker.is_pid_running(int(file_pid)): | ||
# The process ID written inside the lock file is old, just remove the lock file and create a new one. | ||
self.remove_lock_file() | ||
self.create_lock_file() | ||
else: | ||
self.already_running = True | ||
else: | ||
self.create_lock_file() | ||
|
||
@staticmethod | ||
def is_pid_running(pid): | ||
""" | ||
Check whether a given process ID is currently running. We do this by sending signal 0 to the process | ||
which does not has any effect on the running process. | ||
Source: http://stackoverflow.com/questions/7647167/check-if-a-process-is-running-in-python-in-linux-unix | ||
""" | ||
try: | ||
os.kill(pid, 0) | ||
except OSError: | ||
return False | ||
else: | ||
return True | ||
|
||
def create_lock_file(self): | ||
""" | ||
Create the lock file and write the PID in it. We also create the directory structure since the ProcessChecker | ||
might be called before the .Tribler directory has been created. | ||
""" | ||
if not os.path.exists(self.statedir): | ||
os.makedirs(self.statedir) | ||
|
||
with open(self.lock_file_path, 'wb') as lock_file: | ||
lock_file.write(str(os.getpid())) | ||
|
||
def remove_lock_file(self): | ||
""" | ||
Remove the lock file. | ||
""" | ||
os.unlink(self.lock_file_path) | ||
|
||
def get_pid_from_lock_file(self): | ||
""" | ||
Returns the PID from the lock file. | ||
""" | ||
with open(self.lock_file_path, 'rb') as lock_file: | ||
return lock_file.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
from multiprocessing import Process | ||
|
||
from Tribler.Core.Modules.process_checker import ProcessChecker, LOCK_FILE_NAME | ||
from Tribler.Test.test_as_server import AbstractServer | ||
|
||
|
||
def process_func(): | ||
while True: | ||
pass | ||
|
||
|
||
class TestProcessChecker(AbstractServer): | ||
|
||
def tearDown(self, annotate=True): | ||
super(TestProcessChecker, self).tearDown(annotate=annotate) | ||
if self.process: | ||
self.process.terminate() | ||
|
||
def setUp(self, annotate=True): | ||
super(TestProcessChecker, self).setUp(annotate=annotate) | ||
self.process = None | ||
self.state_dir = self.getStateDir() | ||
|
||
def create_lock_file_with_pid(self, pid): | ||
with open(os.path.join(self.state_dir, LOCK_FILE_NAME), 'wb') as lock_file: | ||
lock_file.write(str(pid)) | ||
|
||
def test_no_lock_file(self): | ||
""" | ||
Testing whether the process checker returns false when there is no lock file | ||
""" | ||
process_checker = ProcessChecker() | ||
self.assertTrue(os.path.exists(os.path.join(self.state_dir, LOCK_FILE_NAME))) | ||
self.assertFalse(process_checker.already_running) | ||
|
||
def test_own_pid_in_lock_file(self): | ||
""" | ||
Testing whether the process checker returns false when it finds its own pid in the lock file | ||
""" | ||
self.create_lock_file_with_pid(os.getpid()) | ||
process_checker = ProcessChecker() | ||
self.assertFalse(process_checker.already_running) | ||
|
||
def test_other_instance_running(self): | ||
""" | ||
Testing whether the process checker returns true when another process is running | ||
""" | ||
self.process = Process(target=process_func) | ||
self.process.start() | ||
|
||
self.create_lock_file_with_pid(self.process.pid) | ||
process_checker = ProcessChecker() | ||
self.assertTrue(process_checker.is_pid_running(self.process.pid)) | ||
self.assertTrue(process_checker.already_running) | ||
|
||
def test_dead_pid_in_lock_file(self): | ||
""" | ||
Testing whether the process checker returns false when there is a dead pid in the lock file | ||
""" | ||
dead_pid = 134824733 | ||
self.create_lock_file_with_pid(dead_pid) | ||
process_checker = ProcessChecker() | ||
self.assertFalse(process_checker.is_pid_running(dead_pid)) | ||
self.assertFalse(process_checker.already_running) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters