def test_timeouts(self): res = {'unreg': []} def func_reg(ID, service, address, pid): res['reg'] = (ID, service, address, pid) def func_unreg(ID): res['unreg'].append(ID) add_event_handler("worker-local-start", func_reg) add_event_handler("worker-local-stop", func_unreg) P = PingDB() w1 = (make_kasaya_id(), "foo", "tcp://127.0.0.1:1234", 789) w2 = (make_kasaya_id(), "baz", "tcp://127.0.0.1:1235", 790) P.ping_ex(*w1) P.ping_ex(*w2) gevent.sleep(0) # two pings self.assertEqual(len(P._pingdb), 2) set_value('WORKER_HEARTBEAT', 1) # check pings P.check_all() gevent.sleep(0) self.assertEqual(len(P._pingdb), 2) self.assertEqual(len(res['unreg']), 0) # timeout gevent.sleep(3) P.check_all() gevent.sleep(0) self.assertEqual(len(P._pingdb), 0) self.assertEqual(len(res['unreg']), 2) self.assertItemsEqual([w1[0], w2[0]], res['unreg']) # ping of life res['reg'] = None res['unreg'] = [] P.ping_ex(*w1) P.ping_ex(*w2) gevent.sleep(3) P.ping(w1[0]) P.check_all() gevent.sleep(0) self.assertItemsEqual([w2[0]], res['unreg']) self.assertEqual(len(res['unreg']), 1)
def __init__(self): super(KasayaDaemon, self).__init__(is_host=True) # event handlers add_event_handler("host-join", self.on_remote_kasayad_start) add_event_handler("host-leave", self.on_remote_kasayad_stop) self.hostname = system.get_hostname() LOG.info("Starting local kasaya daemon with ID: [%s]" % self.ID) self.DB = NetworkStateDB() # database self.BC = UDPBroadcast(self.ID) # broadcaster self.SYNC = Synchronizer(self.DB, self.ID) # synchronisation self.WORKER = SyncWorker(server=self, database=self.DB) self.BC.set_own_ip(self.WORKER.own_ip)
def test_ping_pong(self): res = {} def func_reg(ID, service, address, pid): res['reg'] = (ID, service, address, pid) #print ("reg", res)#ID, service, address, pid) def func_unreg(ID): res['unr'] = ID add_event_handler("worker-local-start", func_reg) add_event_handler("worker-local-stop", func_unreg) P = PingDB() w1 = make_kasaya_id() res['unr'] = None # simple ping will not add worker to db P.ping(w1) self.assertEqual(len(P._pingdb), 0) P.ping_ex(w1, "foo", "tcp://127.0.0.1:1234", 567) gevent.sleep(0) self.assertEqual(len(P._pingdb), 1) # has been notification function triggered? self.assertItemsEqual(res['reg'], [w1, 'foo', 'tcp://127.0.0.1:1234', 567]) # dont call unregister self.assertEqual(res['unr'], None) # bad ping data res['reg'] = None P.ping_ex(w1, "bar", "tcp://127.0.0.1:1234", 567) gevent.sleep(0) self.assertEqual(res['reg'], None) self.assertEqual(res['unr'], w1) self.assertEqual(len(P._pingdb), 0)
def __init__(self, server, database): self.DAEMON = server self.DB = database #self.BC = broadcaster #self.pinger = PingDB() # bind events add_event_handler("worker-local-start", self.worker_start_local) add_event_handler("worker-local-stop", self.worker_stop_local) add_event_handler("worker-local-wait", self.worker_prepare) add_event_handler("worker-remote-join", self.worker_start_remote) add_event_handler("worker-remote-leave", self.worker_stop_remote) add_event_handler("connection-end", self.handle_connection_end) # cache self.__services = None # kasayad <--> kasayad communication self.intersync = MessageLoop('tcp://0.0.0.0:' + str(settings.KASAYAD_CONTROL_PORT)) self.intersync.register_message(messages.CTL_CALL, self.handle_global_control_request) # local worker <-> kasayad dialog on public port self.intersync.register_message(messages.WORKER_LIVE, self.handle_worker_live) self.intersync.register_message(messages.WORKER_LEAVE, self.handle_worker_leave) self.intersync.register_message(messages.QUERY, self.handle_name_query, raw_msg_response=True) # service control tasks self.ctl = ControlTasks(allow_redirect=True) self.ctl.register_task("svbus.status", self.CTL_global_services) self.ctl.register_task("worker.stop", self.CTL_worker_stop) self.ctl.register_task("worker.stats", self.CTL_worker_stats) self.ctl.register_task("worker.exists", self.CTL_worker_exists) self.ctl.register_task("service.start", self.CTL_service_start) self.ctl.register_task("service.stop", self.CTL_service_stop) self.ctl.register_task("host.rescan", self.CTL_host_rescan)
def __init__(self, servicename=None, load_config=True, skip_loading_modules=False): super(WorkerDaemon, self).__init__() # config loader if servicename is None: load_config = True if load_config: LOG.info("Loading service.conf") if servicename is None: servicename = self.__load_config() self.servicename = servicename self.__skip_loading_modules = skip_loading_modules # worker status # 0 - initialized # 1 - starting or waiting for reconnect to kasaya # 2 - working # 3 - stopping # 4 - dead self.status = 0 LOG.info("Starting worker daemon, service [%s], ID: [%s]" % (self.servicename, self.ID)) adr = "tcp://%s:%i" % (settings.BIND_WORKER_TO, settings.WORKER_MIN_PORT) self.loop = MessageLoop(adr, settings.WORKER_MAX_PORT) add_event_handler("sender-conn-closed", self.kasaya_connection_broken) add_event_handler("sender-conn-started", self.kasaya_connection_started) self.SYNC = KasayaLocalClient(autoreconnect=True, sessionid=self.ID) self.SYNC.setup(servicename, self.loop.address, self.ID, os.getpid()) LOG.debug("Binded to socket [%s]" % (",".join(self.loop.binded_ip_list()))) # registering handlers self.loop.register_message(messages.SYNC_CALL, self.handle_sync_call, raw_msg_response=True) self.loop.register_message(messages.CTL_CALL, self.handle_control_request) # heartbeat self.__hbloop = True #exposing methods self.exposed_methods = [] # control tasks self.ctl = ControlTasks() self.ctl.register_task("stop", self.CTL_stop) self.ctl.register_task("start", self.CTL_start) self.ctl.register_task("stats", self.CTL_stats) self.ctl.register_task("tasks", self.CTL_methods) # stats #self._sb_errors = 0 # internal service bus errors self._tasks_succes = 0 # succesfully processed tasks self._tasks_error = 0 # task which triggered exceptions self._tasks_nonex = 0 # non existing tasks called self._tasks_control = 0 # control tasks received self._start_time = datetime.datetime.now() # time of worker start