-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lockfile to prevent multiple instances of the script from running…
… simultaneously (#1)
- Loading branch information
1 parent
5f558b4
commit cfc266a
Showing
5 changed files
with
39 additions
and
4 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 |
---|---|---|
|
@@ -7,3 +7,4 @@ __pycache__/ | |
smart-runner.conf | ||
smart-runner.json | ||
smart-runner.log | ||
.smart-runner.lock |
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
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,32 @@ | ||
from atexit import register | ||
from os import getcwd, remove | ||
|
||
LOCK_FILE_PATH = getcwd() + "/.smart-runner.lock" | ||
|
||
|
||
class Lock: | ||
def __init__(self): | ||
if self.isLocked(): | ||
raise RuntimeError("smart-runner is already running") | ||
|
||
self.createLock() | ||
|
||
# Register the removeLock function to be called on exit | ||
register(self.removeLock) | ||
|
||
def createLock(self): | ||
with open(LOCK_FILE_PATH, "w") as lockFile: | ||
lockFile.write("") | ||
|
||
def removeLock(self): | ||
try: | ||
remove(LOCK_FILE_PATH) | ||
except FileNotFoundError: | ||
pass # If the file is already removed or doesn't exist, ignore the error | ||
|
||
def isLocked(self): | ||
try: | ||
with open(LOCK_FILE_PATH, "r") as lockFile: | ||
return True | ||
except FileNotFoundError: | ||
return False |