Example #1
0
    async def run(self, args):
        # start cluster client and box
        httpd_mod = import_module(args.module)
        if hasattr(httpd_mod, 'Handler'):
            assert issubclass(httpd_mod.Handler, BaseHandler)
            mod_handler = httpd_mod.Handler()
        else:
            mod_handler = BaseHandler()
        parser = argparse.ArgumentParser(prog='bbox.py httpd')
        mod_handler.add_arguments(parser)
        sub_args = parser.parse_args(args.mod_args)

        if not args.boxid:
            args.boxid = uuid.uuid4().hex

        ssl_context = get_ssl_context(args.ssl)
        await get_cluster().start()

        http_app = await mod_handler.get_app(sub_args)
        _, handler = await bbox_server.start_server(args)

        http_handler = http_app.make_handler()

        host, port = args.bind.split(':')
        logger.warn('httpd starts at %s', args.bind)
        loop = asyncio.get_event_loop()
        await loop.create_server(http_handler, host, port, ssl=ssl_context)
        asyncio.ensure_future(self.wait_ttl(args.ttl))
        await mod_handler.start(sub_args)

        self.handler = handler
        self.mod_handler = mod_handler
        self.http_handler = http_handler
Example #2
0
    async def run(self, args):
        cfg = get_ticket()
        if cfg.language != 'python3':
            print('language must be python3', file=sys.stderr)
            sys.exit(1)

        mod = import_module(args.module)

        if hasattr(mod, 'Handler'):
            handler = mod.Handler()
        else:
            handler = BaseHandler()

        parser = argparse.ArgumentParser(prog='bbox.py run')
        handler.add_arguments(parser)
        sub_args = parser.parse_args(args.task_params)
        try:
            await get_cluster().start()
            r = await handler.run(sub_args)
            if r:
                logger.debug('task return %s', r)
        finally:
            c = get_cluster()
            c.cont = False
            await asyncio.sleep(0.1)
            c.close()
Example #3
0
    async def run(self, args: Namespace) -> None:

        if get_ticket().language != 'python3':
            print('language must be python3', file=sys.stderr)
            sys.exit(1)

        if not args.boxid:
            args.boxid = uuid.uuid4().hex

        loop = asyncio.get_event_loop()
        loop.set_exception_handler(coroutine_exc_handler)

        mod_handlers = []
        for modspec in args.module:
            mod = import_module(modspec)

            if hasattr(mod, 'Handler'):
                mod_handlers.append(mod.Handler())
            else:
                mod_handlers.append(BaseHandler())

        # start cluster client
        await get_cluster().start()
        src, handler = await bbox_server.start_server(args)

        for h in mod_handlers:
            await h.start(args)
        self.handler = handler
        self.mod_handlers = mod_handlers

        asyncio.ensure_future(self.wait_ttl(args.ttl))
Example #4
0
    async def run(self, args):
        cfg = get_ticket()
        if cfg.language != 'python3':
            print('language must be python3', file=sys.stderr)
            sys.exit(1)

        if not args.boxid:
            args.boxid = uuid.uuid4().hex

        mod_handlers = []
        for modspec in args.module:
            mod = import_module(modspec)

            if hasattr(mod, 'Handler'):
                mod_handlers.append(mod.Handler())
            else:
                mod_handlers.append(BaseHandler())

        # start cluster client
        await get_cluster().start()
        src, handler = await bbox_server.start_server(args)

        for h in mod_handlers:
            await h.start(args)
        self.handler = handler
        self.mod_handlers = mod_handlers

        asyncio.ensure_future(self.wait_ttl(args.ttl))
Example #5
0
    async def run(self, args: Namespace) -> None:
        ticket = get_ticket()
        if ticket.language != 'python3':
            print('language must be python3', file=sys.stderr)
            sys.exit(1)

        mod = import_module(args.module)

        if hasattr(mod, 'Handler'):
            handler = mod.Handler()
        else:
            handler = BaseHandler()

        loop = asyncio.get_event_loop()

        loop.add_signal_handler(signal.SIGINT, self.handle_stop_sig, handler)

        loop.add_signal_handler(signal.SIGTERM, self.handle_stop_sig, handler)

        loop.set_exception_handler(coroutine_exc_handler)

        parser = argparse.ArgumentParser(prog='bbox.py run')
        handler.add_arguments(parser)
        sub_args = parser.parse_args(args.task_params)
        try:
            await get_cluster().start()
            r = await handler.run(sub_args)
            if r is not None:
                logger.debug('task return %s', r)
        finally:
            handler.shutdown()
            c = get_cluster()
            c.cont = False
            await asyncio.sleep(0.1)
            c.close()
Example #6
0
 def handle_stop_sig(self, handler: BaseHandler) -> None:
     try:
         logger.debug('sigint met, the handle %s should stop lately',
                      handler)
         get_cluster().stop()
         handler.cont = False
         wakeup_sleep_tasks()
         loop = asyncio.get_event_loop()
         loop.remove_signal_handler(signal.SIGINT)
         loop.remove_signal_handler(signal.SIGTERM)
         exit_after = int(os.getenv('BBOX_TASK_EXIT_WAIT', 10))
         loop.call_later(
             exit_after,
             sys_exit)  # force exit10 or env defined seconds later
     except:
         logger.error('error on handle sigint', exc_info=True)
         raise
Example #7
0
    async def run(self, args):
        if get_ticket().language != 'python3':
            print('language must be python3', file=sys.stderr)
            sys.exit(1)

        cors = []
        handlers = []
        for spec in args.taskspec:
            args = shlex.split(spec)
            module, task_params = args[0], args[1:]
            mod = import_module(module)
            if hasattr(mod, 'Handler'):
                handler = mod.Handler()
            else:
                handler = BaseHandler()

            parser = argparse.ArgumentParser(
                prog='bbox.py mrun {}'.format(module))
            handler.add_arguments(parser)
            sub_args = parser.parse_args(task_params)
            handlers.append(handler)
            cors.append(self.run_handler(handler, sub_args))

        loop = asyncio.get_event_loop()

        loop.add_signal_handler(signal.SIGINT, self.handle_stop_sig, handlers)

        loop.add_signal_handler(signal.SIGTERM, self.handle_stop_sig, handlers)

        loop.set_exception_handler(coroutine_exc_handler)

        try:
            await get_cluster().start()
            await asyncio.gather(*cors)
        finally:
            handler.shutdown()
            c = get_cluster()
            c.cont = False
            await asyncio.sleep(0.1)
            c.close()