def main(): config = { '_log': LOG, 'AMQP_URI': 'amqp://*****:*****@127.0.0.1:5672' } service_runner = ServiceRunner(config) service_runner.add_service(DatabaseService) service_runner.start() runnlet = eventlet.spawn(service_runner.wait) while True: try: runnlet.wait() except OSError as exc: if exc.errno == errno.EINTR: continue raise except KeyboardInterrupt: try: service_runner.stop() except KeyboardInterrupt: service_runner.kill() else: break
def runserver(self): def _shutdown(signum, frame): eventlet.spawn_n(runner.stop) runner = ServiceRunner(config=config.to_dict()) # for service_cls in BaseService.__subclasses__(): # runner.add_service(service_cls) runner.add_service(InvboxService) signal.signal(signal.SIGTERM, _shutdown) runner.start() runnlet = eventlet.spawn(runner.wait) while True: try: runnlet.wait() except OSError as exc: if exc.errno == errno.EINTR: continue raise except KeyboardInterrupt: print() try: runner.stop() except KeyboardInterrupt: print() # as above runner.kill() else: break
def test_runner_lifecycle(fake_module): events = set() class Container(object): def __init__(self, service_cls, config): self.service_name = service_cls.__name__ self.service_cls = service_cls def start(self): events.add(('start', self.service_cls.name, self.service_cls)) def stop(self): events.add(('stop', self.service_cls.name, self.service_cls)) def kill(self): events.add(('kill', self.service_cls.name, self.service_cls)) def wait(self): events.add(('wait', self.service_cls.name, self.service_cls)) fake_module.ServiceContainer = Container config = {'SERVICE_CONTAINER_CLS': 'fake_module.ServiceContainer'} runner = ServiceRunner(config) runner.add_service(TestService1) runner.add_service(TestService2) runner.start() assert events == { ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), } events = set() runner.stop() assert events == { ('stop', 'foobar_1', TestService1), ('stop', 'foobar_2', TestService2), } events = set() runner.kill() assert events == { ('kill', 'foobar_1', TestService1), ('kill', 'foobar_2', TestService2), } events = set() runner.wait() assert events == { ('wait', 'foobar_1', TestService1), ('wait', 'foobar_2', TestService2), }
def test_runner_lifecycle(): events = set() class Container(object): def __init__(self, service_cls, worker_ctx_cls, config): self.service_name = service_cls.__name__ self.service_cls = service_cls self.worker_ctx_cls = worker_ctx_cls def start(self): events.add(('start', self.service_cls.name, self.service_cls)) def stop(self): events.add(('stop', self.service_cls.name, self.service_cls)) def kill(self): events.add(('kill', self.service_cls.name, self.service_cls)) def wait(self): events.add(('wait', self.service_cls.name, self.service_cls)) config = {} runner = ServiceRunner(config, container_cls=Container) runner.add_service(TestService1) runner.add_service(TestService2) runner.start() assert events == { ('start', 'foobar_1', TestService1), ('start', 'foobar_2', TestService2), } events = set() runner.stop() assert events == { ('stop', 'foobar_1', TestService1), ('stop', 'foobar_2', TestService2), } events = set() runner.kill() assert events == { ('kill', 'foobar_1', TestService1), ('kill', 'foobar_2', TestService2), } events = set() runner.wait() assert events == { ('wait', 'foobar_1', TestService1), ('wait', 'foobar_2', TestService2), }
def main(): config_dict = get_config_yaml(CONFIG_YAML_PATH) c = update_config_yaml(config_dict, CONFIG_YAML_PATH) runner = ServiceRunner(c) runner.add_service(FetchURLService) # container_a = get_container(runner, FetchURLService) runner.start() try: runner.wait() except KeyboardInterrupt: runner.kill() runner.stop()
def main(): config_dict = get_config_yaml(CONFIG_YAML_PATH) c = update_config_yaml(config_dict, CONFIG_YAML_PATH) wus = WatchURLService() wus.get_config() runner = ServiceRunner(c) runner.add_service(WatchURLService) # container_a = get_container(runner, WatchURLService) runner.start() try: runner.wait() except KeyboardInterrupt: runner.kill() runner.stop()
def test_busy_check_on_teardown(): # max_workers needs to be provided, as it's used in a semaphore count config = MagicMock({'max_workers': 4}) kill_called = Mock() class MockedContainer(ServiceContainer): def kill(self, exc): kill_called(type(exc)) super(MockedContainer, self).kill(exc) sr = ServiceRunner(config, container_cls=MockedContainer) sr.add_service(ExampleService) sr.start() sr.kill(Exception()) with wait_for_call(5, kill_called) as kill_called_waited: kill_called_waited.assert_called_with(Exception)
def test_busy_check_on_teardown(): # max_workers needs to be provided, as it's used in a semaphore count config = {'max_workers': 4} kill_called = Mock() class MockedContainer(ServiceContainer): def kill(self): kill_called() super(MockedContainer, self).kill() sr = ServiceRunner(config, container_cls=MockedContainer) sr.add_service(ExampleService) sr.start() sr.kill() with wait_for_call(5, kill_called) as kill_called_waited: assert kill_called_waited.call_count == 1
def handle(self, *args, **options): runner = ServiceRunner(settings.AMQP_CONFIG) runner.add_service(MasterService) def shutdown(signum, frame): eventlet.spawn_n(runner.kill) signal.signal(signal.SIGTERM, shutdown) runner.start() try: runner.wait() except KeyboardInterrupt: try: runner.stop() except KeyboardInterrupt: runner.kill()
def run(services, config, backdoor_port=None, rename=None): service_runner = ServiceRunner(config) for service_cls in services: if rename and rename.get(service_cls.name): service_cls.name = rename[service_cls.name] service_runner.add_service(service_cls) def shutdown(signum, frame): # signal handlers are run by the MAINLOOP and cannot use eventlet # primitives, so we have to call `stop` in a greenlet eventlet.spawn_n(service_runner.stop) signal.signal(signal.SIGTERM, shutdown) if backdoor_port is not None: setup_backdoor(service_runner, backdoor_port) service_runner.start() # if the signal handler fires while eventlet is waiting on a socket, # the __main__ greenlet gets an OSError(4) "Interrupted system call". # This is a side-effect of the eventlet hub mechanism. To protect nameko # from seeing the exception, we wrap the runner.wait call in a greenlet # spawned here, so that we can catch (and silence) the exception. runnlet = eventlet.spawn(service_runner.wait) while True: try: runnlet.wait() except OSError as exc: if exc.errno == errno.EINTR: # this is the OSError(4) caused by the signalhandler. # ignore and go back to waiting on the runner continue raise except KeyboardInterrupt: print() # looks nicer with the ^C e.g. bash prints in the terminal try: service_runner.stop() except KeyboardInterrupt: print() # as above service_runner.kill() else: # runner.wait completed break
def run(services, config, backdoor_port=None): service_runner = ServiceRunner(config) for service_cls in services: service_runner.add_service(service_cls) def shutdown(signum, frame): # signal handlers are run by the MAINLOOP and cannot use eventlet # primitives, so we have to call `stop` in a greenlet eventlet.spawn_n(service_runner.stop) signal.signal(signal.SIGTERM, shutdown) if backdoor_port is not None: setup_backdoor(service_runner, backdoor_port) service_runner.start() # if the signal handler fires while eventlet is waiting on a socket, # the __main__ greenlet gets an OSError(4) "Interrupted system call". # This is a side-effect of the eventlet hub mechanism. To protect nameko # from seeing the exception, we wrap the runner.wait call in a greenlet # spawned here, so that we can catch (and silence) the exception. runnlet = eventlet.spawn(service_runner.wait) while True: try: runnlet.wait() except OSError as exc: if exc.errno == errno.EINTR: # this is the OSError(4) caused by the signalhandler. # ignore and go back to waiting on the runner continue raise except KeyboardInterrupt: print() # looks nicer with the ^C e.g. bash prints in the terminal try: service_runner.stop() except KeyboardInterrupt: print() # as above service_runner.kill() else: # runner.wait completed break
def run_nameko(args): # Running nameko imports here so that Eventgen as a module does not require nameko to run. import eventlet eventlet.monkey_patch() from nameko.runners import ServiceRunner # In order to make this run locally as well as within a container-ized environment, we're to pull variables # from both environment variables and CLI arguments, where CLI will take precendence. config = parse_env_vars() config = parse_cli_vars(config, args) config = rectify_config(config) print "Config used: {}".format(config) # Wait up to 30s for RMQ service to be up wait_for_response(config["AMQP_URI"], config["AMQP_WEBPORT"]) # Start Nameko service runner = ServiceRunner(config=config) if args.role == "controller": from eventgen_nameko_controller import EventgenController runner.add_service(EventgenController) else: from eventgen_nameko_server import EventgenServer runner.add_service(EventgenServer) runner.start() runnlet = eventlet.spawn(runner.wait) while True: try: runnlet.wait() except OSError as exc: if exc.errno == errno.EINTR: # this is the OSError(4) caused by the signalhandler. # ignore and go back to waiting on the runner continue raise except KeyboardInterrupt: print() # looks nicer with the ^C e.g. bash prints in the terminal try: runner.stop() except KeyboardInterrupt: print() # as above runner.kill() else: # runner.wait completed break
def run(): config = {'AMQP_URI': 'amqp://localhost'} runner = ServiceRunner(config) runner.add_service(AlarmService) runner.add_service(HttpEntrypointService) runner.add_service(LightService) runner.add_service(LocalStorageService) runner.add_service(SpotifyService) runner.add_service(VolumeService) runner.start() runnlet = eventlet.spawn(runner.wait) try: runnlet.wait() except KeyboardInterrupt: try: runner.stop() except KeyboardInterrupt: runner.kill()
def run_all(): ''' 启动所有服务 ''' try: services = {} for f in root.iterdir(): if f.is_dir() and f.name.endswith('Service'): name = f.name service = importlib.import_module(name + '.service') services[name] = service from nameko.runners import ServiceRunner runner = ServiceRunner( config={'AMQP_URI': 'amqp://*****:*****@192.168.2.215'}) for name, service in services.items(): cls = getattr(service, name) runner.add_service(cls) runner.start() runner.wait() # runner.stop() except Exception as e: runner.kill()
def run_all(): services = {} for f in root.iterdir(): if f.is_dir() and f.name.endswith("Service") and f.name.startswith( "Admin"): name = f.name service = importlib.import_module(name + '.service') services[name] = service from nameko.runners import ServiceRunner runner = ServiceRunner(config={"AMQP_URI": "amqp://*****:*****@ip"}) for name, service in services.items(): cls = getattr(service, name) print(name, service, cls) runner.add_service(cls) print('wsadsa') runner.start() try: runner.wait() except KeyboardInterrupt: runner.kill() runner.stop()
# primitives, so we have to call `stop` in a greenlet eventlet.spawn_n(runner.stop) signal.signal(signal.SIGTERM, shutdown) logger = logging.getLogger(__name__) # start both services runner.start() runnlet = eventlet.spawn(runner.wait) while True: try: runnlet.wait() except OSError as exc: if exc.errno == errno.EINTR: # this is the OSError(4) caused by the signalhandler. # ignore and go back to waiting on the runner continue raise except KeyboardInterrupt: print() # looks nicer with the ^C e.g. bash prints in the terminal try: runner.stop() except KeyboardInterrupt: print() # as above runner.kill() else: # runner.wait completed break # # stop both services # # runner.stop()
service_runner.start() print("Started service") try: runlet.wait() except OSError as exc: if exc.errno == errno.EINTR: # this is the OSError(4) caused by the signalhandler. # ignore and go back to waiting on the runner continue raise except KeyboardInterrupt: print( ) # looks nicer with the ^C e.g. bash prints in the terminal try: service_runner.stop() except KeyboardInterrupt: print() # as above service_runner.kill() else: # runlet.wait completed break print("Stopping service") service_runner.stop() except: print( "Exception. Pausing for a little and then retrying to start service" ) time.sleep(5)