res=run_sql("SELECT COUNT(id) FROM schTASK WHERE status=%s GROUP BY status",(status,))
try:
out=res[0][0]
except:
pass
returnout
defchange_priority(self):
task_id=self.currentrow[0]
priority=self.currentrow[8]
new_priority=self._display_ask_number_box("Insert the desired priority for task %s. The smaller the number the less the priority. Note that a negative number will mean to always postpone the task while a number bigger than 10 will mean some tasks with less priority could be stopped in order to let this task run. The current priority is %s. New value:"%(task_id,priority))
self.rows=run_sql("""SELECT id,proc,user,runtime,sleeptime,status,progress,arguments,priority FROM %s WHERE status NOT LIKE '%%_DELETED' %s ORDER BY %s"""%(table,where,order))
ring=0
self.repaint()
ring+=1
char=-1
try:
char=timed_out(self.stdscr.getch,1)
ifchar==27:# escaping sequence
char=self.stdscr.getch()
ifchar==79:# arrow
char=self.stdscr.getch()
ifchar==65:#arrow up
char=self.curses.KEY_UP
elifchar==66:#arrow down
char=self.curses.KEY_DOWN
elifchar==72:
char=self.curses.KEY_PPAGE
elifchar==70:
char=self.curses.KEY_NPAGE
elifchar==91:
char=self.stdscr.getch()
ifchar==53:
char=self.stdscr.getch()
ifchar==126:
char=self.curses.KEY_HOME
exceptTimedOutExc:
char=-1
self.handle_keys(char)
classBibSched:
def__init__(self):
self.helper_modules=CFG_BIBTASK_VALID_TASKS
self.scheduled=None
self.task_status={}
self.next_bibupload=()
self.waitings=()
self.rows=()
os.environ['BIBSCHED_MODE']='automatic'
deftasks_safe_p(self,proc1,proc2):
"""Return True when the two tasks can run concurrently."""
returnproc1!=proc2# and not proc1.startswith('bibupload') and not proc2.startswith('bibupload')
"""Ping the task and update its status to error if necessary."""
res=run_sql("SELECT status FROM schTASK WHERE id=%s AND status in ('CONTINUING', 'RUNNING', 'ABOUT TO STOP', 'SLEEPING', 'ABOUT TO SLEEP')",(task_id,))
Log("Send SLEEP signal to #%d (%s) which was in status %s"%(other_task_id,other_proc,other_status))
bibsched_set_status(other_task_id,'ABOUT TO SLEEP',other_status)
time.sleep(CFG_BIBSCHED_REFRESHTIME)
returnTrue
defwatch_loop(self):
defcalculate_rows():
"""Return all the rows to work on."""
ifrun_sql("SELECT count(id) FROM schTASK WHERE status='ERROR' OR status='DONE WITH ERRORS'")[0][0]>0:
raiseStandardError('BibSched had to halt because at least a task is in status ERROR or DONE WITH ERRORS')
self.next_bibupload=run_sql("SELECT id,proc,runtime,status,priority FROM schTASK WHERE status='WAITING' AND proc='bibupload' AND runtime<=NOW() ORDER BY id ASC LIMIT 1",n=1)
self.waitings=run_sql("SELECT id,proc,runtime,status,priority FROM schTASK WHERE status='WAITING' AND runtime<=NOW() ORDER BY priority DESC, runtime ASC, id ASC")
self.rows=run_sql("SELECT id,proc,runtime,status,priority FROM schTASK WHERE status IN ('RUNNING','CONTINUING','SCHEDULED','SLEEPING','ABOUT TO STOP','ABOUT TO SLEEP')")
defcalculate_task_status():
"""Return a handy data structure to analize the task status."""
write_message("BibSched queue status report for %s:"%gethostname())
ifstatusisNone:
report_about_processes('Running',since,tasks)
report_about_processes('Waiting',since,tasks)
else:
forstateinstatus:
report_about_processes(state,since,tasks)
write_message("Done.")
return
defrestart(verbose=True):
halt(verbose,soft=True)
start(verbose)
return
defstop(verbose=True):
"""
* Stop bibsched
* Send stop signal to all the running tasks
* wait for all the tasks to stop
* return
"""
ifverbose:
print"Stopping BibSched if running"
halt(verbose,soft=True)
run_sql("UPDATE schTASK SET status='WAITING' WHERE status='SCHEDULED'")
res=run_sql("SELECT id,proc,status FROM schTASK WHERE status NOT LIKE 'DONE' AND status NOT LIKE '%_DELETED' AND (status='RUNNING' OR status='ABOUT TO STOP' OR status='ABOUT TO SLEEP' OR status='SLEEPING' OR status='CONTINUING')")
ifverbose:
print"Stopping all running BibTasks"
fortask_id,proc,statusinres:
ifstatus=='SLEEPING':
bibsched_send_signal(proc,task_id,signal.SIGCONT)
time.sleep(1)
bibsched_set_status(task_id,'ABOUT TO STOP')
whilerun_sql("SELECT id FROM schTASK WHERE status NOT LIKE 'DONE' AND status NOT LIKE '%_DELETED' AND (status='RUNNING' OR status='ABOUT TO STOP' OR status='ABOUT TO SLEEP' OR status='SLEEPING' OR status='CONTINUING')"):