def on_message(self, headers, body): # FIXME, don't hardcode keepalive topic name: if headers["destination"] == "/topic/keepalive": logging.debug("got keepalive message") response = { "host": socket.gethostname(), "script": os.path.basename(__file__), "timestamp": datetime.now().isoformat(), } stomp.send(body=json.dumps(response), destination="/topic/keepalive_response") return () debug_msg = { "script": os.path.basename(__file__), "host": socket.gethostname(), "timestamp": datetime.now().isoformat(), "message": "received message from %s, before thread" % headers["destination"], } stomp.send(body=json.dumps(debug_msg), destination="/topic/keepalive_response") logging.info("Received stomp message: {message}".format(message=body)) received_obj = None try: received_obj = json.loads(body) except ValueError as e: logging.error("Received invalid JSON: %s." % body) return handle_builder_event(received_obj) logging.info("Destination: %s" % headers.get("destination")) # Acknowledge that the message has been processed self.message_received = True
def on_message(self, headers, body): # FIXME, don't hardcode keepalive topic name: if headers['destination'] == '/topic/keepalive': # by default this log message will not # be visible (logging level is INFO) # but can be made visible if necessary: logging.debug('got keepalive message') response = { "host": socket.gethostname(), "script": os.path.basename(__file__) } stomp.send(body=json.dumps(response), destination="/topic/keepalive_response") return () debug_msg = { "script": os.path.basename(__file__), "host": socket.gethostname(), "timestamp": datetime.now().isoformat(), "message": "received message from %s before thread" % headers['destination'] } stomp.send(body=json.dumps(debug_msg), destination="/topic/keepalive_response") logging.debug(debug_msg) logging.info("on_message() Message received") # Acknowledge that the message has been received self.message_received = True t = threading.Thread(target=do_work, args=(body, )) t.start()
def on_message(self, headers, body): # FIXME, don't hardcode keepalive topic name: if headers['destination'] == '/topic/keepalive': # by default this log message will not # be visible (logging level is INFO) # but can be made visible if necessary: logging.debug('got keepalive message') response = { "host": socket.gethostname(), "script": os.path.basename(__file__) } stomp.send(body=json.dumps(response), destination="/topic/keepalive_response") return() debug_msg = {"script": os.path.basename(__file__), "host": socket.gethostname(), "timestamp": datetime.now().isoformat(), "message": "received message from %s before thread" % headers['destination']} stomp.send(body=json.dumps(debug_msg), destination="/topic/keepalive_response") logging.info("on_message() Message received") # Acknowledge that the message has been received self.message_received = True t = threading.Thread(target=do_work, args=(body,)) t.start()
def on_message(self, headers, body): # FIXME : The maps defined above seem to be an anti-pattern. Also, it's very odd # IMHO that we're invoking 'global' here. The variable is in scope and # we're not attempting any read or assignment. global r_bioc_map global shell_ext global packagebuilder_home logMsg(" [x] Received %r" % (body,)) try: received_obj = json.loads(body) except ValueError: logMsg("Caught Value error, not a valid JSON object?") sys.stdout.flush() return () if "job_id" in received_obj.keys(): # ignore malformed messages job_id = received_obj["job_id"] bioc_version = received_obj["bioc_version"] r_version = bioc_r_map[bioc_version] jobs_dir = os.path.join(packagebuilder_home, "jobs") if not os.path.exists(jobs_dir): os.mkdir(jobs_dir) job_dir = os.path.join(jobs_dir, job_id) if not os.path.exists(job_dir): os.mkdir(job_dir) r_libs_dir = os.path.join(job_dir, "R-libs") if not os.path.exists(r_libs_dir): os.mkdir(r_libs_dir) jobfilename = os.path.join(packagebuilder_home, job_dir, "manifest.json") jobfile = open(jobfilename, "w") jobfile.write(body) jobfile.close logMsg("Wrote job info to %s." % jobfilename) shell_cmd = os.path.join(packagebuilder_home, "%s%s" % (builder_id, shell_ext)) logMsg("shell_cmd = %s" % shell_cmd) builder_log = open(os.path.join(job_dir, "builder.log"), "w") pid = subprocess.Popen( [shell_cmd, jobfilename, bioc_version], stdout=builder_log, stderr=subprocess.STDOUT ).pid # todo - somehow close builder_log filehandle if possible msg_obj = {} msg_obj["builder_id"] = builder_id msg_obj["body"] = "Got build request..." msg_obj["first_message"] = True msg_obj["job_id"] = job_id msg_obj["client_id"] = received_obj["client_id"] msg_obj["bioc_version"] = bioc_version json_str = json.dumps(msg_obj) stomp.send(destination="/topic/builderevents", body=json_str, headers={"persistent": "true"}) # logMsg("Receipt: %s" % this_frame.headers.get('receipt-id')) else: logMsg("Invalid JSON (missing job_id key)") sys.stdout.flush() # Acknowledge that the message has been processed self.message_received = True
def on_message(self, headers, body): # FIXME, don't hardcode keepalive topic name: if headers['destination'] == '/topic/keepalive': logging.debug('got keepalive message') response = {"host": socket.gethostname(), "script": os.path.basename(__file__), "timestamp": datetime.now().isoformat()} stomp.send(body=json.dumps(response), destination="/topic/keepalive_response") return() debug_msg = {"script": os.path.basename(__file__), "host": socket.gethostname(), "timestamp": datetime.now().isoformat(), "message": "received message from %s" % headers['destination']} stomp.send(body=json.dumps(debug_msg), destination="/topic/keepalive_response") logging.info("Received stomp message with body: {message}".format(message=body)) destination = headers.get('destination') logging.info("Message is intended for destination: {dst}".format(dst = destination)) received_obj = None if not is_connection_usable(): logging.info("on_message() Closing connection.") connection.close() try: logging.debug("on_message() Parsing JSON.") received_obj = json.loads(body) logging.info("on_message() Successfully parsed JSON") except ValueError as e: logging.error("on_message() JSON is invalid: %s." % body) return if ('job_id' in received_obj.keys()): if (destination == '/topic/buildjobs'): handle_job_start(received_obj) elif (destination == '/topic/builderevents'): handle_builder_event(received_obj) logging.info("on_message() Destination handled.") else: logging.warning("on_message() Invalid json (no job_id key).") # Acknowledge that the message has been processed self.message_received = True
obj = {} issue_id = sys.argv[1] url = sys.argv[2] segs = url.split("/") pkgname = segs[4] pkgname_bare = pkgname.split("_")[0] obj['force'] = True obj['bioc_version'] = BIOC_VERSION obj['r_version'] = BIOC_R_MAP[BIOC_VERSION] obj['svn_url'] = url obj['repository'] = 'scratch' now = pacific.localize(now0) timestamp1 = now.strftime("%Y%m%d%H%M%S") timestamp2 = now.strftime("%a %b %d %Y %H:%M:%S") timestamp2 = timestamp2 + " GMT-%s (%s)" % (offset, tzname) obj['job_id'] = "%s_%s" % (pkgname_bare, timestamp1) obj['time'] = timestamp2 obj['client_id'] = "single_package_builder_autobuild:%s:%s" % (issue_id, pkgname) json = json.dumps(obj) logging.debug("Received JSON object: '%s'", json) try: stomp = getNewStompConnection('', MyListener()) except: logging.info("Cannot connect") raise this_frame = stomp.send(TOPICS['jobs'], json)
TIMEOUT = 60 # timeout in seconds logging.basicConfig(format='%(levelname)s: %(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO) logging.getLogger("stomp.py").setLevel(logging.WARNING) class MyListener(ConnectionListener): def on_message(self, headers, message): logging.info(message) try: logging.info("Attempting to connect using new communication module") stomp = getNewStompConnection('', MyListener()) # stomp.set_listener('', PrintingListener()) stomp.subscribe(destination="/topic/keepalive_response", id=1, ack='auto') logging.info("Connection established using new communication module") except Exception as e: logging.error("main() Could not connect to ActiveMQ: %s." % e) raise while True: stomp.send(destination="/topic/keepalive", body="stay alive!") # level is set to info to keep logs quiet, change to info if # you want to see the following. logging.debug("Sent keepalive message.") time.sleep(TIMEOUT)
def do_work(body): debug_msg = { "script": os.path.basename(__file__), "host": socket.gethostname(), "timestamp": datetime.now().isoformat(), "message": "received message in thread" } stomp.send(body=json.dumps(debug_msg), destination="/topic/keepalive_response") logging.info("on_message() Message acknowledged") try: received_obj = json.loads(body) except ValueError: logging.error("on_message() ValueError: invalid JSON?") return () if ('job_id' in received_obj.keys()): # ignore malformed messages try: job_id = received_obj['job_id'] # job_base = job_id.rsplit("_", 1)[0] job_base = received_obj['client_id'].rsplit(":")[1] bioc_version = received_obj['bioc_version'] job_dir = os.path.join(ENVIR['spb_home'], "jobs") if not os.path.exists(job_dir): os.mkdir(job_dir) # create package specific directory under jobs/ job_dir_main = os.path.join(job_dir, job_base) if not os.path.exists(job_dir_main): os.mkdir(job_dir_main) # create package specific R-libs directory for package dependencies r_libs_dir = os.path.join(job_dir_main, "R-libs") if not os.path.exists(r_libs_dir): os.mkdir(r_libs_dir) # create package/github commit specific directory url_name = received_obj['svn_url'].split("/") url_user = url_name[3] url_pkg = url_name[4] cmd = "/".join([ "https://api.github.com/repos", url_user, url_pkg, "commits/HEAD" ]) #request = Request(cmd) try: #response = urlopen(request) #res = response.read() request = requests.get(cmd) res = request.text run_dir = json.loads(res)['sha'][0:7] except URLError, err_url: logging.info('Cannot access github log: %s', err_url) logging.debug('URL: %s', cmd) run_dir = job_id job_dir = os.path.join(job_dir_main, run_dir) logging.info("Package Working Directory: %s", job_dir) if not os.path.exists(job_dir): os.mkdir(job_dir) jobfilename = os.path.join(ENVIR['spb_home'], job_dir, "manifest.json") jobfile = open(jobfilename, "w") jobfile.write(body) jobfile.close() logging.info("on_message() job_dir: '%s'.", job_dir) # create other imports os.environ['SPB_HOME'] = ENVIR['spb_home'] # create BBS-specific imports os.environ['BBS_HOME'] = ENVIR['bbs_home'] os.environ['BBS_SSH_CMD'] = ENVIR['bbs_ssh_cmd'] + " -qi " + ENVIR[ 'bbs_RSA_key'] + " -o StrictHostKeyChecking=no" os.environ['BBS_R_HOME'] = ENVIR['bbs_R_home'] os.environ['BBS_R_CMD'] = ENVIR['bbs_R_cmd'] os.environ['BBS_BIOC_VERSION'] = ENVIR['bbs_Bioc_version'] os.environ['BBS_RSYNC_CMD'] = ENVIR['bbs_rsync_cmd'] os.environ['BBS_RSYNC_RSH_CMD'] = os.environ.get( 'BBS_RSYNC_CMD') + " -e " + os.environ.get('BBS_SSH_CMD') os.environ['BBS_MODE'] = ENVIR['bbs_mode'] os.environ['BBS_BIOC_VERSIONED_REPO_PATH'] = os.environ.get( 'BBS_BIOC_VERSION') + "/" + os.environ.get('BBS_MODE') os.environ['BBS_STAGE2_R_SCRIPT'] = os.environ.get( 'BBS_HOME') + "/" + os.environ.get( 'BBS_BIOC_VERSIONED_REPO_PATH') + "/STAGE2.R" os.environ['BBS_NON_TARGET_REPOS_FILE'] = os.environ.get( 'BBS_HOME') + "/" + os.environ.get( 'BBS_BIOC_VERSIONED_REPO_PATH') + "/non_target_repos.txt" os.environ['BBS_CENTRAL_RHOST'] = ENVIR['bbs_central_rhost'] os.environ['BBS_CENTRAL_RUSER'] = ENVIR['bbs_central_ruser'] os.environ['BBS_CENTRAL_RDIR'] = "/home/" + os.environ.get( 'BBS_CENTRAL_RUSER') + "/public_html/BBS/" + os.environ.get( 'BBS_BIOC_VERSIONED_REPO_PATH') os.environ['BBS_CENTRAL_BASEURL'] = "http://" + os.environ.get( 'BBS_CENTRAL_RHOST') + "/BBS/" + os.environ.get( 'BBS_BIOC_VERSIONED_REPO_PATH') os.environ['BBS_CURL_CMD'] = ENVIR['bbs_curl_cmd'] os.environ['LANG'] = ENVIR['bbs_lang'] # R CMD check variables os.environ['R_CHECK_ENVIRON'] = ENVIR['r_check_environ'] os.environ['BIOC_DEVEL_PASSWORD'] = ENVIR['bioc_devel_password'] shell_cmd = [ "python", "-m", "workers.builder", jobfilename, bioc_version ] builder_log = open(os.path.join(job_dir, "builder.log"), "w") logging.info( "on_message() Attempting to run commands from directory: '%s'", os.getcwd()) logging.info("on_message() shell_cmd: '%s'", shell_cmd) logging.info("on_message() jobfilename: '%s'", jobfilename) logging.info("on_message() builder_log: '%s'", builder_log) blderProcess = subprocess.Popen(shell_cmd, stdout=builder_log, stderr=builder_log) ## TODO - somehow close builder_log filehandle if possible msg_obj = {} msg_obj['builder_id'] = BUILDER_ID msg_obj['status'] = "Got Build Request" msg_obj['body'] = "Got build request..." msg_obj['first_message'] = True msg_obj['job_id'] = job_id msg_obj['client_id'] = received_obj['client_id'] msg_obj['bioc_version'] = bioc_version json_str = json.dumps(msg_obj) stomp.send(destination=TOPICS['events'], body=json_str, headers={"persistent": "true"}) logging.debug("on_message() Reply sent") blderProcess.wait() logging.info( "on_message() blderProcess finished with status {s}.".format( s=blderProcess.returncode)) builder_log.close() logging.info("on_message() builder_log closed.") except Exception as e: logging.error("on_message() Caught exception: {e}".format(e=e)) return ()
def do_work(body): debug_msg = {"script": os.path.basename(__file__), "host": socket.gethostname(), "timestamp": datetime.now().isoformat(), "message": "received message in thread"} stomp.send(body=json.dumps(debug_msg), destination="/topic/keepalive_response") logging.info("on_message() Message acknowledged") try: received_obj = json.loads(body) except ValueError: logging.error("on_message() ValueError: invalid JSON?") return() if ('job_id' in received_obj.keys()): # ignore malformed messages try: job_id = received_obj['job_id'] # job_base = job_id.rsplit("_", 1)[0] job_base = received_obj['client_id'].rsplit(":")[1] bioc_version = received_obj['bioc_version'] job_dir = os.path.join(ENVIR['spb_home'], "jobs") if not os.path.exists(job_dir): os.mkdir(job_dir) # create package specific directory under jobs/ job_dir_main = os.path.join(job_dir, job_base) if not os.path.exists(job_dir_main): os.mkdir(job_dir_main) # create package specific R-libs directory for package dependencies r_libs_dir = os.path.join(job_dir_main, "R-libs") if not os.path.exists(r_libs_dir): os.mkdir(r_libs_dir) # create package/github commit specific directory url_name = received_obj['svn_url'].split("/") url_user = url_name[3] url_pkg = url_name[4] cmd = "/".join(["https://api.github.com/repos",url_user, url_pkg, "commits/HEAD"]) request = Request(cmd) try: response = urlopen(request) res = response.read() run_dir = json.loads(res)['sha'][0:7] except URLError, err_url: logging.info('Cannot access github log: %s', err_url) run_dir = job_id job_dir = os.path.join(job_dir_main, run_dir) logging.info("Package Working Directory: %s", job_dir) if not os.path.exists(job_dir): os.mkdir(job_dir) jobfilename = os.path.join(ENVIR['spb_home'], job_dir, "manifest.json") jobfile = open(jobfilename, "w") jobfile.write(body) jobfile.close() logging.info("on_message() job_dir: '%s'.", job_dir) # create other imports os.environ['SPB_HOME'] = ENVIR['spb_home'] # create BBS-specific imports os.environ['BBS_HOME'] = ENVIR['bbs_home'] os.environ['BBS_SSH_CMD'] = ENVIR['bbs_ssh_cmd'] + " -qi " + ENVIR['bbs_RSA_key'] + " -o StrictHostKeyChecking=no" os.environ['BBS_R_HOME'] = ENVIR['bbs_R_home'] os.environ['BBS_R_CMD'] = ENVIR['bbs_R_cmd'] os.environ['BBS_BIOC_VERSION'] = ENVIR['bbs_Bioc_version'] os.environ['BBS_RSYNC_CMD'] = ENVIR['bbs_rsync_cmd'] os.environ['BBS_RSYNC_RSH_CMD'] = os.environ.get('BBS_RSYNC_CMD') + " -e " + os.environ.get('BBS_SSH_CMD') os.environ['BBS_MODE'] = ENVIR['bbs_mode'] os.environ['BBS_BIOC_VERSIONED_REPO_PATH'] = os.environ.get('BBS_BIOC_VERSION') + "/" + os.environ.get('BBS_MODE') os.environ['BBS_STAGE2_R_SCRIPT'] = os.environ.get('BBS_HOME') + "/" + os.environ.get('BBS_BIOC_VERSIONED_REPO_PATH') + "/STAGE2.R" os.environ['BBS_NON_TARGET_REPOS_FILE'] = os.environ.get('BBS_HOME') + "/" + os.environ.get('BBS_BIOC_VERSIONED_REPO_PATH') + "/non_target_repos.txt" os.environ['BBS_CENTRAL_RHOST'] = ENVIR['bbs_central_rhost'] os.environ['BBS_CENTRAL_RUSER'] = ENVIR['bbs_central_ruser'] os.environ['BBS_CENTRAL_RDIR'] = "/home/" + os.environ.get('BBS_CENTRAL_RUSER') + "/public_html/BBS/" + os.environ.get('BBS_BIOC_VERSIONED_REPO_PATH') os.environ['BBS_CENTRAL_BASEURL'] = "http://" + os.environ.get('BBS_CENTRAL_RHOST') + "/BBS/" + os.environ.get('BBS_BIOC_VERSIONED_REPO_PATH') os.environ['BBS_CURL_CMD'] = ENVIR['bbs_curl_cmd'] os.environ['LANG'] = ENVIR['bbs_lang'] # R CMD check variables os.environ['_R_CHECK_TIMINGS_']="0" os.environ['_R_CHECK_EXECUTABLES_']="FALSE" os.environ['_R_CHECK_EXECUTABLES_EXCLUSIONS_']="FALSE" # R CMD BiocCheck variable os.environ['BIOC_DEVEL_PASSWORD'] = ENVIR['bioc_devel_password'] shell_cmd = ["python", "-m", "workers.builder", jobfilename, bioc_version] builder_log = open(os.path.join(job_dir, "builder.log"), "w") logging.info("on_message() Attempting to run commands from directory: '%s'", os.getcwd()) logging.info("on_message() shell_cmd: '%s'", shell_cmd) logging.info("on_message() jobfilename: '%s'", jobfilename) logging.info("on_message() builder_log: '%s'", builder_log) blderProcess = subprocess.Popen(shell_cmd, stdout=builder_log, stderr=builder_log) ## TODO - somehow close builder_log filehandle if possible msg_obj = {} msg_obj['builder_id'] = BUILDER_ID msg_obj['status'] = "Got Build Request" msg_obj['body'] = "Got build request..." msg_obj['first_message'] = True msg_obj['job_id'] = job_id msg_obj['client_id'] = received_obj['client_id'] msg_obj['bioc_version'] = bioc_version json_str = json.dumps(msg_obj) stomp.send(destination=TOPICS['events'], body=json_str, headers={"persistent": "true"}) logging.info("on_message() Reply sent") blderProcess.wait() logging.info("on_message() blderProcess finished with status {s}.".format(s=blderProcess.returncode)) builder_log.close() logging.info("on_message() builder_log closed.") except Exception as e: logging.error("on_message() Caught exception: {e}".format(e=e)) return()