def wait_for_hit_response(puid, quid, cstage, request_params): global failover_retrylimit, failover_interval, wait4res_duration, poll4res_interval global hit_host, hit_urlloc, cs_dbhost, cs_dbname, clock execres = False scnt = cnt = 0 msgstr = "" while cnt < failover_retrylimit and execres == False: # send a request to HITS resp = mdlib.httpsend(hit_host, hit_urlloc, request_params, clock) mdlib.log("* HIT request has been registered") # response from HITS if resp[0] == 200: # announce the hit task link to the listeners if cstage.listener != None: announce_hittask_links(puid, quid, cstage.listener) # check CS_event table to get the results from AMT. sqlstmt = "select msg from CS_event where action='regHIT' and pid={0} and qid={1}".format( puid, quid) while scnt < (wait4res_duration / poll4res_interval): hitdata = mdlib.get_single_mysql_data(cs_dbhost, cs_dbname, sqlstmt) if hitdata != None: #print data mdlib.log( "* HIT results have been received msg={0}".format( hitdata[0])) msgstr = hitdata[0] execres = True break else: if scnt == 0: mdlib.log( "* waiting for HIT result, scnt={0}/{1}".format( scnt, (wait4res_duration / poll4res_interval))) scnt = scnt + 1 time.sleep(poll4res_interval) # every 5 sec. elif resp[0] == 404: mdlib.log( '! will not retry since there is no service page (errcode=404)' ) cnt = failover_retrylimit # no more retry else: mdlib.log('! HITS returned error: code={0} msg={1}'.format( resp[0], resp[1])) # prepare next iteration cnt = cnt + 1 if execres == False: time.sleep(failover_interval) scnt = 0 del resp return msgstr, execres
def main(): dbhost = mdlib.get_dbhost() dbname = "medusa" print '* starting mdscript_runner, host=%s dbname=%s' % (dbhost, dbname) while True: # check if there is a new request sqlstmt = "select cmd,medscript from CS_cmdqueue order by timestamp desc" con, cur = mdlib.get_raw_mysql_data(dbhost, dbname, sqlstmt) dat = cur.fetchone() i = 0 while dat != None: #dat[0], dat[1] cmd = dat[0] taskpath = dat[1] if cmd == 'start': # fork and run medusa program. pid = os.fork() if pid == 0: # child mdserv.task_main(taskpath) exit() else: # parent #mdlib.log('* created crowd-sensing task id={0}.'.format(pid)) print '* created crowd-sensing task id={0}.'.format(pid) elif cmd == 'stop': # for future use. os.system('./sig2daemon.sh {0}'.format(taskpath)); dat = cur.fetchone() con.close() sqlstmt = "delete from CS_cmdqueue" mdlib.get_single_mysql_data(dbhost, dbname, sqlstmt) # polling interval: 3 seconds. time.sleep(3) #mdlib.log('* exits medusa task runner.') print '* exits medusa task runner.'
def main(): dbhost = mdlib.get_dbhost() dbname = "medusa" print '* starting mdscript_runner, host=%s dbname=%s' % (dbhost, dbname) while True: # check if there is a new request sqlstmt = "select cmd,medscript from CS_cmdqueue order by timestamp desc" con, cur = mdlib.get_raw_mysql_data(dbhost, dbname, sqlstmt) dat = cur.fetchone() i = 0 while dat != None: #dat[0], dat[1] cmd = dat[0] taskpath = dat[1] if cmd == 'start': # fork and run medusa program. pid = os.fork() if pid == 0: # child mdserv.task_main(taskpath) exit() else: # parent #mdlib.log('* created crowd-sensing task id={0}.'.format(pid)) print '* created crowd-sensing task id={0}.'.format(pid) elif cmd == 'stop': # for future use. os.system('./sig2daemon.sh {0}'.format(taskpath)) dat = cur.fetchone() con.close() sqlstmt = "delete from CS_cmdqueue" mdlib.get_single_mysql_data(dbhost, dbname, sqlstmt) # polling interval: 3 seconds. time.sleep(3) #mdlib.log('* exits medusa task runner.') print '* exits medusa task runner.'
def wait_for_mst_response(puid, quid, rparams_dict, cstage_config_output): global failover_retrylimit, failover_interval, wait4res_duration, poll4res_interval global spc_host, spc_urlloc, cs_dbhost, cs_dbname, clock execres = False scnt = cnt = 0 msgstr = None while cnt < failover_retrylimit and execres == False: scnt = 0 while scnt < (wait4res_duration / poll4res_interval): # check CS_event table to get the results from Aqua Clients. sqlstmt = "select msg from CS_event where action='completeTask' and pid={0} and qid={1} and userid='{2}'" \ .format(puid, quid, ENV["W_WID"]) retdat = mdlib.get_single_mysql_data(cs_dbhost, cs_dbname, sqlstmt) if retdat != None: mdlib.log("* RS results user={0}, msg={1}".format( ENV["W_WID"], retdat[0])) msgstr = retdat[0] execres = True break else: if scnt == 0: mdlib.log("* waiting: cnt={0}/{1},{2}th try".format( scnt, (wait4res_duration / poll4res_interval), cnt)) scnt = scnt + 1 time.sleep(poll4res_interval) # cnt = cnt + 1 if execres == False: time.sleep(failover_interval) # retry scnt = 0 retrycnt = 0 ruser = ENV["W_WID"] resp = mdlib.httpsend(spc_host, spc_urlloc, rparams_dict, clock) if resp[0] == 200: retrycnt = retrycnt + 1 mdlib.log( "* retry sms cmd msg will be sent to userid={0}".format( ruser)) else: mdlib.log('! retry request failed, uid={0}, code={1}, msg={2}'. format(ruser, resp[0], res[1])) return msgstr, execres
def wait_for_hit_response(puid, quid, cstage, request_params): global failover_retrylimit, failover_interval, wait4res_duration, poll4res_interval global hit_host, hit_urlloc, cs_dbhost, cs_dbname, clock execres = False scnt = cnt = 0 msgstr = "" while cnt < failover_retrylimit and execres == False: # send a request to HITS resp = mdlib.httpsend(hit_host, hit_urlloc, request_params, clock) mdlib.log("* HIT request has been registered") # response from HITS if resp[0] == 200: # announce the hit task link to the listeners if cstage.listener != None: announce_hittask_links(puid, quid, cstage.listener) # check CS_event table to get the results from AMT. sqlstmt = "select msg from CS_event where action='regHIT' and pid={0} and qid={1}".format(puid, quid) while scnt < (wait4res_duration/poll4res_interval): hitdata = mdlib.get_single_mysql_data(cs_dbhost, cs_dbname, sqlstmt) if hitdata != None: #print data mdlib.log("* HIT results have been received msg={0}".format(hitdata[0])) msgstr = hitdata[0] execres = True break else: if scnt == 0: mdlib.log("* waiting for HIT result, scnt={0}/{1}".format(scnt, (wait4res_duration/poll4res_interval))) scnt = scnt + 1 time.sleep(poll4res_interval) # every 5 sec. elif resp[0] == 404: mdlib.log('! will not retry since there is no service page (errcode=404)') cnt = failover_retrylimit # no more retry else: mdlib.log('! HITS returned error: code={0} msg={1}'.format(resp[0], resp[1])) # prepare next iteration cnt = cnt + 1 if execres == False: time.sleep(failover_interval) scnt = 0 del resp return msgstr, execres
def announce_hittask_links(pid, qid, listeners): # get url. sqlstmt = "select msg from CS_event where action='gotoHIT' and pid={0} and qid={1}".format(pid, qid) content = mdlib.get_single_mysql_data(cs_dbhost, cs_dbname, sqlstmt) if content != None: # send it to the listeners emails = listeners.split(',') for i in range(len(emails)): mdlib.send_email("Medusa HIT task has been posted", content[0], emails[i]) mdlib.log("* Weblink for HIT task has been announced: {0}".format(listeners)) else: mdlib.log("! No registered link, please verify Worker Manager is running properly")
def wait_for_mst_response(puid, quid, rparams_dict, cstage_config_output): global failover_retrylimit, failover_interval, wait4res_duration, poll4res_interval global spc_host, spc_urlloc, cs_dbhost, cs_dbname, clock execres = False scnt = cnt = 0 msgstr = None while cnt < failover_retrylimit and execres == False: scnt = 0 while scnt < (wait4res_duration/poll4res_interval): # check CS_event table to get the results from Aqua Clients. sqlstmt = "select msg from CS_event where action='completeTask' and pid={0} and qid={1} and userid='{2}'" \ .format(puid, quid, ENV["W_WID"]) retdat = mdlib.get_single_mysql_data(cs_dbhost, cs_dbname, sqlstmt) if retdat != None: mdlib.log("* RS results user={0}, msg={1}".format(ENV["W_WID"], retdat[0])) msgstr = retdat[0] execres = True break else: if scnt == 0: mdlib.log("* waiting: cnt={0}/{1},{2}th try".format(scnt, (wait4res_duration/poll4res_interval), cnt)) scnt = scnt + 1 time.sleep(poll4res_interval) # cnt = cnt + 1 if execres == False: time.sleep(failover_interval) # retry scnt = 0 retrycnt = 0 ruser = ENV["W_WID"] resp = mdlib.httpsend(spc_host, spc_urlloc, rparams_dict, clock) if resp[0] == 200: retrycnt = retrycnt + 1 mdlib.log("* retry sms cmd msg will be sent to userid={0}".format(ruser)) else: mdlib.log('! retry request failed, uid={0}, code={1}, msg={2}'.format(ruser, resp[0], res[1])) return msgstr, execres
def pullout_db(puid, quid): global failover_retrylimit, failover_interval, wait4res_duration, poll4res_interval global spc_host, spc_urlloc, cs_dbhost, cs_dbname, clock execres = False scnt = cnt = 0 msgstr = None sqlstmt = "select msg from CS_event where action='completeTask' and pid={0} and qid={1} and userid='{2}'" \ .format(puid, quid, ENV["W_WID"]) retdat = mdlib.get_single_mysql_data(cs_dbhost, cs_dbname, sqlstmt) if retdat != None: mdlib.log("* RS results user={0}, msg={1}".format(ENV["W_WID"], retdat[0])) msgstr = retdat[0] execres = True return msgstr, execres