2009-06-12 03:06:01 +00:00
|
|
|
#!/usr/bin/env python
|
2004-01-01 04:12:08 +00:00
|
|
|
'''
|
2009-06-02 05:20:46 +00:00
|
|
|
Generic Assimilator framework
|
2004-01-01 04:12:08 +00:00
|
|
|
'''
|
|
|
|
|
2009-05-29 22:47:31 +00:00
|
|
|
import os, re, signal, sys, time, hashlib
|
2009-06-11 15:38:48 +00:00
|
|
|
import boinc_path_config
|
2009-06-11 16:06:25 +00:00
|
|
|
from Boinc import database, boinc_db, boinc_project_path, configxml, sched_messages
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
# Peter Norvig's Abstract base class hack
|
|
|
|
def abstract():
|
|
|
|
"""
|
|
|
|
This function is not necessary, but provides
|
|
|
|
a nice error message when Abstract methods are not
|
|
|
|
overridden by child classes.
|
|
|
|
See: http://norvig.com/python-iaq.html for details.
|
|
|
|
"""
|
|
|
|
import inspect
|
|
|
|
# get the name of the calling function off the stack
|
|
|
|
caller = inspect.getouterframes(inspect.currentframe())[1][3]
|
|
|
|
raise NotImplementedError(caller + ' must be implemented in subclass')
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
class Assimilator():
|
|
|
|
'''
|
|
|
|
Use this class to create new pure-Python Assimilators.
|
|
|
|
To create a new assimilator:
|
|
|
|
1) call __init__ from the new child class' __init__ method
|
|
|
|
2) override the assimilate_handler method
|
|
|
|
3) add the standard if __name__ == "__main__" bootstrap (see end of this file)
|
|
|
|
'''
|
2009-05-29 22:47:31 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
def __init__(self):
|
|
|
|
# Be sure to call Assimilator.__init__(self) from child classes
|
|
|
|
|
|
|
|
# HACK: this belongs in boinc_db.py!
|
|
|
|
boinc_db.WU_ERROR_NO_CANONICAL_RESULT = 32
|
|
|
|
|
|
|
|
# initialize member vars
|
|
|
|
self.config = None
|
2009-06-11 16:06:25 +00:00
|
|
|
self.STOP_TRIGGER_FILENAME = boinc_project_path.project_path('stop_daemons')
|
2009-06-02 05:20:46 +00:00
|
|
|
self.caught_sig_int = False
|
|
|
|
self.log=sched_messages.SchedMessages()
|
|
|
|
self.pass_count = 0
|
|
|
|
self.update_db = True
|
|
|
|
self.noinsert = False
|
|
|
|
self.wu_id_mod = 0
|
|
|
|
self.wu_id_remainder = 0
|
|
|
|
self.one_pass = False
|
|
|
|
self.one_pass_N_WU = 0
|
|
|
|
self.appname = ''
|
|
|
|
self.sleep_interval = 10
|
|
|
|
|
|
|
|
def check_stop_trigger(self):
|
|
|
|
"""
|
|
|
|
Stops the daemon when not running in one_pass mode
|
|
|
|
There are two cases when the daemon will stop:
|
|
|
|
1) if the SIGINT signal is received
|
|
|
|
2) if the stop trigger file is present
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
junk = open(self.STOP_TRIGGER_FILENAME, 'r')
|
|
|
|
except IOError:
|
|
|
|
if self.caught_sig_int:
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logCritical("Caught SIGINT\n")
|
2009-06-02 05:20:46 +00:00
|
|
|
sys.exit(1)
|
|
|
|
else:
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logCritical("Found stop trigger\n")
|
2009-06-02 05:20:46 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def sigint_handler(self, sig, stack):
|
|
|
|
"""
|
|
|
|
This method handles the SIGINT signal. It sets a flag
|
|
|
|
but waits to exit until check_stop_trigger is called
|
|
|
|
"""
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logDebug("Handled SIGINT\n")
|
2009-06-02 05:20:46 +00:00
|
|
|
self.caught_sig_int = True
|
|
|
|
|
|
|
|
def filename_hash(self, name, hash_fanout):
|
|
|
|
"""
|
|
|
|
Accepts a filename (without path) and the hash fanout.
|
|
|
|
Returns the directory bucket where the file will reside.
|
|
|
|
The hash fanout is typically provided by the project config file.
|
|
|
|
"""
|
|
|
|
h = hex(int(hashlib.md5(name).hexdigest()[:8], 16) % hash_fanout)[2:]
|
|
|
|
|
|
|
|
# check for the long L suffix. It seems like it should
|
|
|
|
# never be present but that isn't the case
|
|
|
|
if h.endswith('L'):
|
|
|
|
h = h[:-1]
|
|
|
|
return h
|
|
|
|
|
|
|
|
def get_file_path(self, result):
|
|
|
|
"""
|
|
|
|
Accepts a result object and returns the relative path to the file.
|
|
|
|
This method accounts for file hashing and includes the directory
|
|
|
|
bucket in the path returned.
|
|
|
|
"""
|
|
|
|
name = re.search('<file_name>(.*)</file_name>',result.xml_doc_in).group(1)
|
|
|
|
fanout = int(self.config.uldl_dir_fanout)
|
|
|
|
hashed = self.filename_hash(name, fanout)
|
|
|
|
updir = self.config.upload_dir
|
|
|
|
result = os.path.join(updir,hashed,name)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def assimilate_handler(self, wu, results, canonical_result):
|
|
|
|
"""
|
|
|
|
This method is called for each workunit (wu) that needs to be
|
|
|
|
processed. A canonical result is not guarenteed and several error
|
|
|
|
conditions may be present on the wu. Call report_errors(wu) when
|
|
|
|
overriding this method.
|
|
|
|
|
|
|
|
Note that the -noinsert flag (self.noinsert) must be accounted for when
|
|
|
|
overriding this method.
|
|
|
|
"""
|
|
|
|
abstract()
|
|
|
|
|
|
|
|
def report_errors(self, wu):
|
|
|
|
"""
|
|
|
|
Writes error logs based on the workunit (wu) error_mask field.
|
|
|
|
Returns True if errors were present, False otherwise.
|
|
|
|
"""
|
|
|
|
if wu.error_mask&boinc_db.WU_ERROR_COULDNT_SEND_RESULT:
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logCritical("[%s] Error: couldn't send a result\n", wu.name)
|
2009-06-02 05:20:46 +00:00
|
|
|
return True
|
|
|
|
if wu.error_mask&boinc_db.WU_ERROR_TOO_MANY_ERROR_RESULTS:
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logCritical("[%s] Error: too many error results\n", wu.name)
|
2009-06-02 05:20:46 +00:00
|
|
|
return True
|
|
|
|
if wu.error_mask&boinc_db.WU_ERROR_TOO_MANY_TOTAL_RESULTS:
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logCritical("[%s] Error: too many total results\n", wu.name)
|
2009-06-02 05:20:46 +00:00
|
|
|
return True
|
|
|
|
if wu.error_mask&boinc_db.WU_ERROR_TOO_MANY_SUCCESS_RESULTS:
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logCritical("[%s] Error: too many success results\n", wu.name)
|
2009-06-02 05:20:46 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def do_pass(self, app):
|
|
|
|
"""
|
|
|
|
This method scans the database for workunits that need to be
|
|
|
|
assimilated. It handles all processing rules passed in on the command
|
|
|
|
line, except for -noinsert, which must be handled in assimilate_handler.
|
|
|
|
Calls check_stop_trigger before doing any work.
|
|
|
|
"""
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
did_something=False
|
|
|
|
# check for stop trigger
|
|
|
|
self.check_stop_trigger()
|
|
|
|
self.pass_count += 1
|
|
|
|
n = 0
|
|
|
|
|
|
|
|
units = database.Workunits.find(app=app,assimilate_state=boinc_db.ASSIMILATE_READY)
|
|
|
|
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logDebug("pass %d, units %d\n", self.pass_count, len(units))
|
2009-06-02 05:20:46 +00:00
|
|
|
|
|
|
|
# look for workunits with correct appid and
|
|
|
|
# assimilate_state==ASSIMILATE_READY
|
|
|
|
for wu in units:
|
|
|
|
# if the user has turned on the WU mod flag, adhere to it
|
|
|
|
if self.wu_id_mod > 0 and self.wu_id_remainder > 0:
|
|
|
|
if wu.id % self.wu_id_mod != self.wu_id_remainder:
|
|
|
|
continue
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
# track how many jobs have been processed
|
|
|
|
# stop if the limit is reached
|
|
|
|
n += 1
|
|
|
|
if self.one_pass_N_WU > 0 and n > self.one_pass_N_WU:
|
|
|
|
return did_something
|
|
|
|
|
|
|
|
# only mark as dirty if the database is modified
|
|
|
|
if self.update_db:
|
|
|
|
did_something=True
|
|
|
|
|
|
|
|
canonical_result = None
|
|
|
|
results = None
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logDebug("[%s] assimilating: state=%d\n", wu.name, wu.assimilate_state)
|
2009-06-02 05:20:46 +00:00
|
|
|
results = database.Results.find(workunit=wu)
|
2009-05-29 22:47:31 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
# look for canonical result for workunit in results
|
2009-06-12 03:06:01 +00:00
|
|
|
for result in results:
|
|
|
|
if result == wu.canonical_result:
|
|
|
|
canonical_result=result
|
2009-06-02 05:20:46 +00:00
|
|
|
|
2009-06-16 19:38:35 +00:00
|
|
|
if canonical_result == None and wu.error_mask == 0:
|
|
|
|
# If no canonical result found and WU had no other errors,
|
|
|
|
# something is wrong, e.g. result records got deleted prematurely.
|
|
|
|
# This is probably unrecoverable, so mark the WU as having
|
|
|
|
# an assimilation error and keep going.
|
2009-06-02 05:20:46 +00:00
|
|
|
wu.error_mask = boinc_db.WU_ERROR_NO_CANONICAL_RESULT
|
2009-06-16 19:38:35 +00:00
|
|
|
wu.commit()
|
2009-06-02 05:20:46 +00:00
|
|
|
|
|
|
|
# assimilate handler
|
|
|
|
self.assimilate_handler(wu, results, canonical_result)
|
|
|
|
|
|
|
|
# TODO: check for DEFER_ASSIMILATION as a return value from assimilate_handler
|
|
|
|
|
|
|
|
if self.update_db:
|
|
|
|
# tag wu as ASSIMILATE_DONE
|
|
|
|
wu.assimilate_state = boinc_db.ASSIMILATE_DONE
|
|
|
|
wu.transition_time = int(time.time())
|
|
|
|
wu.commit()
|
|
|
|
|
|
|
|
# return did something result
|
|
|
|
return did_something
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-12 03:06:01 +00:00
|
|
|
def parse_args(self, args):
|
2009-06-02 05:20:46 +00:00
|
|
|
"""
|
|
|
|
Parses arguments provided on the command line and sets
|
|
|
|
those argument values as member variables. Arguments
|
|
|
|
are parsed as their true types, so integers will be ints,
|
|
|
|
not strings.
|
|
|
|
"""
|
|
|
|
|
|
|
|
args.reverse()
|
|
|
|
while(len(args)):
|
|
|
|
arg = args.pop()
|
|
|
|
if arg == '-sleep_interval':
|
|
|
|
arg = args.pop()
|
|
|
|
self.sleep_interval = float(arg)
|
|
|
|
elif arg == '-one_pass':
|
|
|
|
self.one_pass = True
|
|
|
|
elif arg == '-one_pass_N_WU':
|
|
|
|
arg = args.pop()
|
|
|
|
self.one_pass_N_WU = int(arg)
|
|
|
|
elif arg == '-noinsert':
|
|
|
|
self.noinsert = True
|
|
|
|
elif arg == '-dont_update_db':
|
|
|
|
self.update_db = False
|
|
|
|
elif arg == '-mod':
|
|
|
|
self.wu_id_mod = int(args.pop())
|
|
|
|
self.wu_id_remainder = int(args.pop())
|
|
|
|
elif arg == '-d':
|
|
|
|
arg = args.pop()
|
|
|
|
self.log.set_debug_level(arg)
|
|
|
|
elif arg == '-app':
|
|
|
|
arg = args.pop()
|
|
|
|
self.appname = arg
|
|
|
|
else:
|
2009-06-12 03:06:01 +00:00
|
|
|
self.logCritical("Unrecognized arg: %s\n", arg)
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
This function runs the class in a loop unless the
|
|
|
|
one_pass or one_pass_WU_N flags are set. Before execution
|
|
|
|
parse_args() is called, the xml config file is loaded and
|
|
|
|
the SIGINT signal is hooked to the sigint_handler method.
|
|
|
|
"""
|
2009-06-12 03:06:01 +00:00
|
|
|
self.parse_args(sys.argv[1:])
|
2009-06-02 05:20:46 +00:00
|
|
|
self.config = configxml.default_config().config
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
# retrieve app where name = app.name
|
|
|
|
database.connect()
|
|
|
|
app=database.Apps.find1(name=self.appname)
|
|
|
|
database.close()
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, self.sigint_handler)
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
# do one pass or execute main loop
|
|
|
|
if self.one_pass:
|
|
|
|
self.do_pass(app)
|
|
|
|
else:
|
|
|
|
# main loop
|
|
|
|
while(1):
|
|
|
|
database.connect()
|
|
|
|
workdone = self.do_pass(app)
|
|
|
|
database.close()
|
|
|
|
if not workdone:
|
|
|
|
time.sleep(self.sleep_interval)
|
2009-06-12 03:06:01 +00:00
|
|
|
|
|
|
|
def _writeLog(self, mode, *args):
|
|
|
|
"""
|
|
|
|
A private helper function for writeing to the log
|
|
|
|
"""
|
|
|
|
self.log.printf(mode, *args)
|
|
|
|
|
|
|
|
def logCritical(self, *messageArgs):
|
|
|
|
"""
|
|
|
|
A helper function for logging critical messages
|
|
|
|
"""
|
|
|
|
self._writeLog(sched_messages.CRITICAL, *messageArgs)
|
|
|
|
|
|
|
|
def logNormal(self, *messageArgs):
|
|
|
|
"""
|
|
|
|
A helper function for logging normal messages
|
|
|
|
"""
|
|
|
|
self._writeLog(sched_messages.NORMAL, *messageArgs)
|
|
|
|
|
|
|
|
def logDebug(self, *messageArgs):
|
|
|
|
"""
|
|
|
|
A helper function for logging debug messages
|
|
|
|
"""
|
|
|
|
self._writeLog(sched_messages.DEBUG, *messageArgs)
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
# --------------------------------------------
|
|
|
|
# Add the following to your assimilator file:
|
2004-01-01 04:12:08 +00:00
|
|
|
|
2009-06-02 05:20:46 +00:00
|
|
|
#if __name__ == '__main__':
|
|
|
|
# asm = YourAssimilator()
|
|
|
|
# asm.run()
|
2004-01-01 04:12:08 +00:00
|
|
|
|