Skip to content

Commit

Permalink
Set paths to smart.sh script and .smart-runner.lock files relative to…
Browse files Browse the repository at this point in the history
… script directory (#3)
  • Loading branch information
nicholasodonnell authored Jan 9, 2024
1 parent 672c527 commit 203d045
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
15 changes: 11 additions & 4 deletions smart-runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
TooSoonException,
)
from sys import exit
from os import path


SMART_SCRIPT_PATH = path.realpath(path.dirname(__file__)) + "/smart.sh"
LOCK_FILE_PATH = path.realpath(path.dirname(__file__)) + "/.smart-runner.lock"


def sendTestFailureEmail(config, logger, test, disk):
Expand All @@ -31,10 +36,10 @@ def sendTestFailureEmail(config, logger, test, disk):
try:
Email.send(
message=Message(
subject="{} SMART test failed for disk {}".format(
test.testType.value, disk.disk
subject="SMART test failed on {}".format(disk.disk),
body="A {} SMART test has failed on {}. Please see server logs located at {} for more details.".format(
test.testType.value, disk.disk, config.log.file
),
body="Please see server logs for more details",
fromEmail=config.email.from_email,
toEmail=config.email.to_email,
),
Expand All @@ -55,7 +60,7 @@ def sendTestFailureEmail(config, logger, test, disk):

def main():
try:
Lock()
Lock(LOCK_FILE_PATH)
args = Args()
config = Config(configFile=args.config)
db = Database(dbFile=config.database.file)
Expand All @@ -76,6 +81,7 @@ def main():
]
shortTest = Test(
testType=TestType.SHORT,
smartScriptPath=SMART_SCRIPT_PATH,
enabled=config.short.enabled,
frequencyDays=config.short.frequency_days,
offsetDays=config.short.offset_days,
Expand All @@ -84,6 +90,7 @@ def main():
)
longTest = Test(
testType=TestType.LONG,
smartScriptPath=SMART_SCRIPT_PATH,
enabled=config.long.enabled,
frequencyDays=config.long.frequency_days,
offsetDays=config.long.offset_days,
Expand Down
14 changes: 7 additions & 7 deletions smart_runner/lock.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from atexit import register
from os import getcwd, remove

LOCK_FILE_PATH = getcwd() + "/.smart-runner.lock"
from os import remove


class Lock:
def __init__(self):
def __init__(self, file):
self.file = file

if self.isLocked():
raise RuntimeError("smart-runner is already running")

Expand All @@ -15,18 +15,18 @@ def __init__(self):
register(self.removeLock)

def createLock(self):
with open(LOCK_FILE_PATH, "w") as lockFile:
with open(self.file, "w") as lockFile:
lockFile.write("")

def removeLock(self):
try:
remove(LOCK_FILE_PATH)
remove(self.file)
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:
with open(self.file, "r") as lockFile:
return True
except FileNotFoundError:
return False
7 changes: 3 additions & 4 deletions smart_runner/test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from .command import Command
from enum import Enum
from datetime import datetime, timedelta
from os import getcwd

SMART_RUNNER_PATH = getcwd() + "/smart.sh"


class TestType(Enum):
Expand Down Expand Up @@ -35,13 +32,15 @@ class Test:
def __init__(
self,
testType,
smartScriptPath,
enabled=False,
frequencyDays=1,
offsetDays=0,
disksPerRun=1,
lastTestDate=None,
):
self.testType = testType
self.smartScriptPath = smartScriptPath
self.enabled = enabled
self.frequencyDays = frequencyDays
self.offsetDays = offsetDays
Expand Down Expand Up @@ -77,7 +76,7 @@ def runTestForDisk(self, disk, onOutput, onError, onFinished):
self.disks.append(disk)

command = Command(
cmd=SMART_RUNNER_PATH + " " + self.testType.value + " " + disk.disk,
cmd=self.smartScriptPath + " " + self.testType.value + " " + disk.disk,
stdout=onOutput,
stderr=onError,
)
Expand Down

0 comments on commit 203d045

Please sign in to comment.