async def test_http_health_bad(app: Application, unused_tcp_port: int, loop: asyncio.AbstractEventLoop) -> None: http = Server('127.0.0.1', unused_tcp_port, Handler) app.add('http', http) result = await app.health() assert 'http' in result assert result['http'] is not None assert isinstance(result['http'], BaseException)
async def _start_redis(app: Application, url: str, connect_max_attempts=10, connect_retry_delay=1.0) -> Redis: db = Redis(url, connect_max_attempts=connect_max_attempts, connect_retry_delay=connect_retry_delay) app.add('redis', db) await app.run_prepare() await db.start() return db
async def _start_postgres(app: Application, postgres: Tuple[str, int], connect_max_attempts=10, connect_retry_delay=1.0) -> PgDb: dsn = 'postgres://postgres@%s:%d/postgres' % (postgres[0], postgres[1]) db = PgDb(dsn, connect_max_attempts=connect_max_attempts, connect_retry_delay=connect_retry_delay) app.add('db', db) await app.run_prepare() await db.start() return db
async def app(tracer_server, loop): tracer_host = '127.0.0.1' tracer_port = (await tracer_server()).port tracer_addr = 'http://%s:%s/' % (tracer_host, tracer_port) app = Application(loop=loop) app.setup_logging(tracer_driver='zipkin', tracer_addr=tracer_addr, tracer_name='test') yield app await app.run_shutdown()
async def _start_amqp(app: Application, url: str, channels, connect_max_attempts=10, connect_retry_delay=1.0) -> Amqp: amqp = Amqp(url, channels=channels, connect_max_attempts=connect_max_attempts, connect_retry_delay=connect_retry_delay) app.add('amqp', amqp) await app.run_prepare() return amqp
async def test_postgres_health_ok(app: Application, postgres: str, loop: asyncio.AbstractEventLoop) -> None: db = Postgres(postgres) app.add('postgres', db) async def start(): await app.run_prepare() await db.start() res = async_call(loop, start) await asyncio.sleep(1) result = await app.health() assert 'postgres' in result assert result['postgres'] is None if res['fut'] is not None: res['fut'].cancel()
async def test_amqp_health_ok(app: Application, rabbitmq: str, loop: asyncio.AbstractEventLoop) -> None: amqp = Amqp(rabbitmq) app.add('amqp', amqp) async def start(): await app.run_prepare() await amqp.start() res = async_call(loop, start) await asyncio.sleep(1) result = await app.health() assert 'amqp' in result assert result['amqp'] is None if res['fut'] is not None: res['fut'].cancel()
async def test_http_health_ok(app: Application, unused_tcp_port: int, loop: asyncio.AbstractEventLoop) -> None: http = Server('127.0.0.1', unused_tcp_port, Handler) app.add('http', http) async def start(): await app.run_prepare() await http.start() res = async_call(loop, start) await asyncio.sleep(1) result = await app.health() assert 'http' in result assert result['http'] is None if res['fut'] is not None: res['fut'].cancel()
async def test_postgres_health_bad(app: Application, unused_tcp_port: int, loop: asyncio.AbstractEventLoop) -> None: url = 'postgres://postgres@%s:%s/postgres' % ('127.0.0.1', unused_tcp_port) db = Postgres(url) app.add('postgres', db) async def start(): await app.run_prepare() await db.start() res = async_call(loop, start) await asyncio.sleep(1) result = await app.health() assert 'postgres' in result assert result['postgres'] is not None assert isinstance(result['postgres'], BaseException) if res['fut'] is not None: res['fut'].cancel()
async def test_amqp_health_bad(app: Application, unused_tcp_port: int, loop: asyncio.AbstractEventLoop) -> None: url = 'amqp://*****:*****@%s:%s/' % ('127.0.0.1', unused_tcp_port) amqp = Amqp(url) app.add('amqp', amqp) async def start(): await app.run_prepare() await amqp.start() res = async_call(loop, start) await asyncio.sleep(1) result = await app.health() assert 'amqp' in result assert result['amqp'] is not None assert isinstance(result['amqp'], BaseException) if res['fut'] is not None: res['fut'].cancel()
body: bytes, envelope: aioamqp.envelope.Envelope, properties: aioamqp.properties.Properties) -> None: await self.ack(ctx, envelope.delivery_tag) print('received message', body) class PubChannel(Channel): async def start(self): await self.open() with self.amqp.app.tracer.new_trace() as ctx: ctx.name('start_pub') print('sending message "hello"') await self.publish(ctx, b'hello', '', self.amqp.channel('consumer').queue) if __name__ == '__main__': logging.basicConfig(level=logging.INFO) loop = asyncio.get_event_loop() cfg = Config(os.environ) app = Application(loop=loop) app.add('srv', Amqp(cfg.amqp_url, [ ConsumerChannel(), PubChannel(), ]), stop_after=[]) app.run()
async def app(loop): app = Application(loop=loop) yield app await app.run_shutdown()
class HttpHandler(Handler): async def prepare(self): self.server.add_route('GET', '/', self.home_handler) self.server.set_error_handler(self.error_handler) async def error_handler(self, ctx: Span, request: web_request.Request, error: Exception) -> web.Response: self.app.log_err(error) if isinstance(error, web.HTTPException): return error return web.Response(body='Internal Error', status=500) async def home_handler(self, ctx: Span, request: web_request.Request) -> web.Response: return web.Response(text='OK') if __name__ == '__main__': logging.basicConfig(level=logging.INFO) loop = asyncio.get_event_loop() cfg = Config(os.environ) app = Application(loop=loop) app.add('srv', Server(host=cfg.host, port=cfg.port, handler=HttpHandler), stop_after=[]) app.run()
await asyncio.sleep(0.2) await self.bot.send_message(context_span, chat.id, 'what?' + str(context_span)) async def start(self, context_span, chat, match): await chat.send_text(context_span, 'hello') async def echo(self, context_span, chat, match): await chat.reply(context_span, match.group(1)) if __name__ == '__main__': logging.basicConfig(level=logging.INFO) loop = asyncio.get_event_loop() app = Application(loop=loop) app.add('http_server', http.Server('127.0.0.1', 8080, HttpHandler)) app.add('db', db.PgDb('postgres://*****:*****@localhost:15432/db', pool_min_size=2, pool_max_size=19, pool_max_queries=50000, pool_max_inactive_connection_lifetime=300., connect_max_attempts=10, connect_retry_delay=1), stop_after=['http_server']) app.add( 'tg', chat.Telegram( api_token='143877684:AAFZ4C-wrlyhT-zIPZMwZnrxmasvg2kzaUw', handler=TelegramHandler,
'descr': 'Database connection string in following format ' 'redis://host:port/dbname?encoding=utf-8' } } async def do_something(app: Application, ctx: Span) -> None: """ do not run this task infinitely!!! there is no graceful shutdown! """ await app.db.execute(ctx, 'test_set', 'SET', 'key', time.time()) res = await app.db.execute(ctx, 'test_set', 'GET', 'key') print('query result', res) if __name__ == '__main__': logging.basicConfig(level=logging.INFO) loop = asyncio.get_event_loop() cfg = Config(os.environ) app = Application(loop=loop) app.add( 'db', Redis(url=cfg.db_url), stop_after=[] ) app.on_start = partial(do_something, app) app.run()
async def do_something(app: Application, ctx: Span) -> None: """ do not run this task infinitely!!! there is no graceful shutdown! """ res = await app.db.query_one(ctx, 'example_query_id', 'SELECT $1::float as now, pg_sleep(1)', time.time()) print('query result', res['now']) if __name__ == '__main__': logging.basicConfig(level=logging.INFO) loop = asyncio.get_event_loop() cfg = Config(os.environ) app = Application(loop=loop) app.add('db', Postgres(url=cfg.db_url, pool_min_size=2, pool_max_size=19, pool_max_queries=50000, pool_max_inactive_connection_lifetime=300., connect_max_attempts=10, connect_retry_delay=1.0), stop_after=[]) app.on_start = partial(do_something, app) app.run()