def _get_app(unis, layout, size=None): conf = {"auth": True, "secret": "a4534asdfsberwregoifgjh948u12"} db = _Database() rt = Runtime(unis, proxy={'defer_update': True}) rt.addService(UnisGrapher) if size: _build_graph(rt, size) else: rt.nodes.load() rt.links.load() if not layout or not rt.graph.load(layout): rt.graph.spring(30) rt.graph.dump('layout.json') auth = AuthHandler(conf, db) compiler = CompileHandler(conf, db, rt) validator = ValidateHandler(conf, db, rt) sketches = SketchHandler(conf, db) graph = GraphHandler(conf, db, rt) #subscribe = SubscriptionHandler(conf) ensure_ssl = SSLCheck(conf) #app = falcon.API(middleware=[ensure_ssl]) app = falcon.API() app.add_route('/', graph) app.add_route('/c', compiler) app.add_route('/a', auth) app.add_route('/v', validator) app.add_route('/l', sketches) app.add_route('/l/{usr}', sketches) #app.add_route('/s', subscribe) return app
class TestingDaemon: def __init__(self, conf, log_file="logs/esmond_uploader.log"): self.conf = conf self.archive_url = conf['archive_url'] self.log_file = conf['log_file'] self.unis = conf['unis'] self.mesh_config = conf['mesh_config'] self.jobs = [] self.threads = [] logging.basicConfig(filename=self.log_file, level=logging.INFO) logging.info('Log Initialized.') return def begin(self): logger = logging.getLogger() fh = logging.FileHandler(self.log_file) logger.addHandler(fh) self._setup() #with daemon.DaemonContext(files_preserve = [fh.stream]): logging.info("********** Esmond Uploader Utility **********") logging.info( "MAIN THREAD \nConfig - \n\ Archive Host:%s\n\ Unis: %s\n\ Mesh: %s", self.archive_url, self.unis, self.mesh_config) logging.info("Starting jobs") for job in self.jobs: self._log("Init thread for " + job['description']) if len(job['members']['members']) > 2: members = job['members']['members'] pairs = [(member1, member2) for member1 in members for member2 in members] [print(p) for p in pairs] for p in pairs: test_thread = Thread(target=self._init_test_thread, args=( job, p[0], p[1], self.conf, )).start() time.sleep(2) if test_thread is not None: self.threads.append(test_thread) else: print("Thread for " + p[0] + "-" + p[1] + " none type thread") return def _setup(self): ''' Setup the main service. - read and apply config in Meshconfig document. - connect to Runtime Exit if configuration fails. ''' try: self.rt = Runtime(self.unis) self.rt.addService("unis.services.data.DataService") except Exception as e: print(e) logging.info("COULD NOT CONNECT TO UNIS") raise AttributeError("COULD NOT CONNECT TO UNIS") sys.exit(0) self._handle_mesh() return def _handle_mesh(self): try: mesh = requests.get(self.mesh_config).json() except: print("could not get mesh config. ensure url is correct.") self.mesh = mesh for test in mesh['tests']: self.jobs.append(test) return def _log(self, msg): now = strftime("%Y-%m-%d %H:%M:%S", gmtime()) return logging.info(msg + " | " + now) def _init_test_thread(self, job, member1, member2, conf): source = member1 destination = member2 test_type = job['description'] interval = job['parameters']['interval'] if 'interval' in job[ 'parameters'] else 120 logging.info("Trying test for %s - %s", source, destination) try: if test_type not in TESTS.keys(): return run = TESTS[test_type](self.archive_url, source=source, destination=destination, runtime=self.rt) self._log("THREAD OK") except Exception as e: self._log("Test not defined for " + test_type + "| " + source + " - " + destination + ". Could not start thread") return logging.info( "LOG: %s test thread - updating every %s seconds.\n Src: %s, Dst: %s", test_type, interval, source, destination) while True: print("Attempting to fetch " + test_type + " from " + source + " -> " + destination) data = run.fetch(time_range=3600, upload=True) if data is None: return print("Bad Thread") print("Fetch routine done, waiting for next interval") time.sleep(interval)