We have an event file which I'm processing to get the status of a job (process), whereas a single job contains multiple fields which I want to segregate based on what I required, for example, "JOB_NEW
", "JOB_START
" etc.
As in my current code I'm processing the file line by line and with a loop and that again processed through multiple conditions, so it works fine but looks to be time taking in case the file is really large (millions of lines). I'm wondering if there is a better nice way to process this in an elegant way to make it really better and faster.
#!/python/v3.6.1/bin/python3
import sys , os , re
from optparse import OptionParser
import numpy as np
import time
import sys
import datetime
import textwrap
wrapper = ''
preferredWidth = 75
def text_format(self,initial):
global wrapper
wrapper = textwrap.TextWrapper(initial_indent=initial, width=preferredWidth,
subsequent_indent=' '*14)
return wrapper
wrapper = text_format('self','Job')
def opt():
parser = OptionParser()
parser.add_option("-l", "--longformat", dest="long",
help="longformat", metavar="LONG")
parser.add_option("-n", "--number", dest="num",
help="number", metavar="NUM")
(options, args) = parser.parse_args()
return(options, args)
(options,args) = opt()
if options.long:
jobid = options.long
check_jobid = re.match("^\d+$",jobid)
if not check_jobid:
print(jobid + ": Illegal job ID.")
sys.exit()
event_file = "/proj/lsb.events"
with open(event_file, "r") as r:
for line in r:
line = line.strip()
match_jobid = line.split(' ')[3]
match_jobid = match_jobid.strip()
var = line.split()
var = [v.replace('\"', '') for v in var]
#===============================JOB_NEW=========================================
#if match_jobid == jobid and "JOB_NEW" in line:
if match_jobid == jobid and var[0] == "JOB_NEW":
print (wrapper.fill("<%s>, Job Name <%s>, User <%s>, Project <%s>, Command <%s>" %(jobid,var[-1],var[11],var[-6],var[-7])))
wrapper = text_format('self','')
var[2] = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime(int(var[2])))
print(wrapper.fill("%s: Submitted from host <%s>, to Queue <%s>, CWD <%s>, Output File <%s>, Requested Resources <%s>;" %(var[2],var[25],var[23],var[26],var[28],var[24])))
#==============================JOB_START========================================
elif match_jobid == jobid and var[0] == "JOB_START":
wrapper = text_format('self','')
var[2] = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime(int( var[2])))
print(wrapper.fill("%s: Dispatched to <%s>;" %(var[2],var[9])))
#=============================JOB_START_ACCEPT=================================
elif match_jobid == jobid and var[0] == "JOB_START_ACCEPT":
wrapper = text_format('self','')
var[2] = time.strftime('%a %b %d %H:%M:%S %Y', \
time.localtime(int(var[2])))
print(wrapper.fill("%s: Starting (Pid %s);" %(var[2],var[4])))
#=============================JOB_EXECUTE=====================================
elif match_jobid == jobid and var[0] == "JOB_EXECUTE":
wrapper = text_format('self','')
var[2] = time.strftime('%a %b %d %H:%M:%S %Y', \
time.localtime(int(var[2])))
print(wrapper.fill("%s: Running with execution home <%s>,\
Execution CWD <%s>,Execution Pid <%s> ;"\
%(var[2],var[7],var[6],var[9])))
#============================JOB_SIGNAL=======================================
elif match_jobid == jobid and var[0] == "JOB_SIGNAL":
wrapper = text_format('self','')
var[2] = time.strftime('%a %b %d %H:%M:%S %Y', \
time.localtime(int(var[2])))
print(wrapper.fill("%s: Signal <%s> requested by user \
or administrator <%s> ;" %(var[2],var[5],var[4])))
Sample File
"JOB_NEW" "1" 1536742813 84 258034 33554482 1 0 1536742813 0 0 "xyz" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "lnx64" "" "no-xyz" "/home/xyz/junk" "" "/home/xyz/123a" "cmd.err" "/home/xyz" "1536742813.84" 0 "" "" "sleep 10" "default" 2048 0 -1 "" ""
"JOB_NEW" "1" 1536742813 85 258034 33554482 1 0 1536742813 0 0 "xyz" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "lnx64" "" "no-xyz" "/home/xyz/junk" "" "/home/xyz/123a" "cmd.err" "/home/xyz" "1536742813.84" 0 "" "" "sleep 10" "default" 2048 0 -1 "" ""
"JOB_START" "1" 1536742819 84 4 0 0 1.0 1 "no-xyz" "" "" 0 0 ""
"JOB_START" "1" 1536742819 85 4 0 0 1.0 1 "no-xyz" "" "" 0 0 ""
"JOB_START_ACCEPT" "1" 1536742819 84 6702 6702 0 0
"JOB_EXECUTE" "1" 1536742820 84 258034 6702 "/home/xyz/junk" "/home/xyz" "xyz" 6702 0
"JOB_START_ACCEPT" "1" 1536742819 85 6702 6702 0 0
"JOB_EXECUTE" "1" 1536742820 85 258034 6702 "/home/xyz/junk" "/home/xyz" "xyz" 6702 0
"JOB_STATUS" "1" 1536742840 85 64 0 0 0.0000 1536742840 0 0 1996 0
"JOB_NEW" "1" 1536742813 86 258034 33554482 1 0 1536742813 0 0 "xyz" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "lnx64" "" "no-xyz" "/home/xyz/junk" "" "/home/xyz/123a" "cmd.err" "/home/xyz" "1536742813.84" 0 "" "" "sleep 10" "default" 2048 0 -1 "" ""
"JOB_START" "1" 1536742819 86 4 0 0 1.0 1 "no-xyz" "" "" 0 0 ""
"JOB_START_ACCEPT" "1" 1536742819 86 6702 6702 0 0
"JOB_EXECUTE" "1" 1536742820 86 258034 6702 "/home/xyz/junk" "/home/xyz" "xyz" 6702 0
"JOB_STATUS" "1" 1536742840 86 64 0 0 0.0000 1536742840 0 0 1996 0
"JOB_START_ACCEPT" "1" 1536742819 24 6702 6702 0 0
"JOB_EXECUTE" "1" 1536742820 14 258034 6702 "/home/xyz/junk" "/home/xyz" "xyz" 6702 0
"JOB_STATUS" "1" 1536742840 84 64 0 0 0.0000 1536742840 0 0 1996 0
Bit more details:
Here a paricular job goes through different stages (JOB_NEW , JOB_START, JOB_START_ACCEPT , JOB_EXECUTE , JOB_STATUS). Now I want to find all the lines for a paricluar job id. For example if I want to find job with job id "84", it should show the following output:
"JOB_NEW" "1" 1536742813 84 258034 33554482 1 0 1536742813 0 0 "xyz" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "lnx64" "" "no-xyz" "/home/xyz/junk" "" "/home/xyz/123a" "cmd.err" "/home/xyz" "1536742813.84" 0 "" "" "sleep 10" "default" 2048 0 -1 "" ""
"JOB_START" "1" 1536742819 84 4 0 0 1.0 1 "no-xyz" "" "" 0 0 ""
"JOB_START_ACCEPT" "1" 1536742819 84 6702 6702 0 0
"JOB_EXECUTE" "1" 1536742820 84 258034 6702 "/home/xyz/junk" "/home/xyz" "xyz" 6702 0
One key point is here that job id appears at column 3 (starting count from 0).
Brief context: We have a in-house tool which is used to run the Jobs or say run the batch process and records the every run with a Job ID with some JOB details as defined above, so in a nutshell it contains the complete chronology of a JOb when it started or executed which is been captured run in time in a form of event
file, However the processing can be happen at given point in time by a user to see the status of his JOB with job id.