Skip to content

Commit

Permalink
Bug fix for issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
neilmunday committed Aug 9, 2018
1 parent 9c571ee commit 27e8709
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
28 changes: 19 additions & 9 deletions bin/slurm-send-mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,40 @@ def runCommand(cmd):
except Exception as e:
die('Error: %s' % e)

logging.basicConfig(format='%(asctime)s:%(levelname)s: %(message)s', datefmt='%Y/%m/%d %H:%M:%S', level=logLevel, filename=logFile)
if logFile:
logging.basicConfig(format='%(asctime)s:%(levelname)s: %(message)s', datefmt='%Y/%m/%d %H:%M:%S', level=logLevel, filename=logFile)
else:
logging.basicConfig(format='%(asctime)s:%(levelname)s: %(message)s', datefmt='%Y/%m/%d %H:%M:%S', level=logLevel)

checkFile(sacctExe)

css = getFileContents(stylesheet)

if not os.access(spoolDir, os.R_OK | os.W_OK):
die("Cannot access %s, check file permissions and that the directory exists " % spoolDir)

# look for any new mail notifications in the spool dir
files = glob.glob(spoolDir + os.sep + "*.mail")
for f in files:
filename = os.path.basename(f)
fields = filename.split('.')
if len(fields) == 4:
if len(fields) == 3:
logging.info("processing: " + f)
try:
userEmail = None
jobId = int(fields[0])
state = fields[1]
user = fields[2]
# e-mail address stored in the file
with open(f, 'r') as spoolFile:
userEmail = spoolFile.read()

if state in ['Began', 'Ended', 'Failed']:
# get job info from sacct
cmd = '%s -j %d -p -n --fields=JobId,Partition,JobName,Start,End,State,nnodes,WorkDir,Elapsed,ExitCode,Comment,Cluster' % (sacctExe, jobId)
cmd = '%s -j %d -p -n --fields=JobId,Partition,JobName,Start,End,State,nnodes,WorkDir,Elapsed,ExitCode,Comment,Cluster,User' % (sacctExe, jobId)
rtnCode, stdout, stderr = runCommand(cmd)
if rtnCode == 0:
body = ''
jobName = ''
user = ''
partition = ''
cluster = ''
nodes = 0
Expand All @@ -168,8 +177,8 @@ def runCommand(cmd):
elapsed = 'N/A'
start = ''
end = 'N/A'
stdoutFile = ''
stderrFile = ''
stdoutFile = '?'
stderrFile = '?'

logging.debug(stdout)
for line in stdout.split("\n"):
Expand All @@ -182,6 +191,7 @@ def runCommand(cmd):
start = data[3].replace('T', ' ')
comment = data[10]
nodes = data[6]
user = data[12]
if state != 'Began':
end = data[4].replace('T', ' ')
elapsed = data[8]
Expand Down Expand Up @@ -257,8 +267,8 @@ def runCommand(cmd):
body = MIMEText(body, 'html')
msg.attach(body)
s = smtplib.SMTP('localhost')
logging.info('sending e-mail to: %s for job %d (%s)' % (user, jobId, state))
s.sendmail(emailFromUserAddress, user, msg.as_string())
logging.info('sending e-mail to: %s using %s for job %d (%s)' % (user, userEmail, jobId, state))
s.sendmail(emailFromUserAddress, userEmail, msg.as_string())
logging.info('deleting: %s' % f)
os.remove(f)
else:
Expand Down
4 changes: 2 additions & 2 deletions bin/slurm-spool-mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ def die(msg):
jobIdRe = re.compile('Job_id=([0-9]+)')
match = jobIdRe.match(jobIdStr)
if match:
path = os.path.join(spoolDir, '%s.%s.%s.mail' % (match.group(1), action, user))
path = os.path.join(spoolDir, '%s.%s.mail' % (match.group(1), action))
logging.debug('job ID match, writing file %s' % path)
with open(path, 'w') as f:
f.write('')
f.write(user)
else:
die('Could not determine job ID')
except Exception as e:
Expand Down

0 comments on commit 27e8709

Please sign in to comment.