async def main(): async with asyncari.connect(ast_url, ast_app, ast_username, ast_password) as client: await client.taskgroup.spawn(on_start, client) # Run the WebSocket async for m in client: print("** EVENT **", m)
async def main(): async with asyncari.connect(ast_url, ast_app, ast_username, ast_password) as client: await setup(client) client.on_channel_event('StasisStart', stasis_start_cb) # Run the WebSocket await anyio.sleep_forever()
async def main(): async with asyncari.connect(ast_url, ast_app, ast_username, ast_password) as client: client.on_channel_event('StasisStart', on_start) client.on_channel_event('StasisEnd', on_end) # Run the WebSocket async for m in client: print("** EVENT **", m)
async def main(): async with asyncari.connect(ast_url, ast_app, ast_username, ast_password) as _client: global client client = _client await setup() await client.on_channel_event('StasisStart', on_start, client.taskgroup) # Run the WebSocket async for m in client: pprint(("** EVENT **", m, m._orig_msg))
async def run(obj, checks, as_list): """ Run a one-shot call. """ if not checks: checks = list(k for k,v in obj.calls.items() if as_list or not v.test.skip) if not checks: raise click.UsageError("No tests known. Missing config file?") if as_list: for c in checks: c = obj.calls[c] print(c.name, "m" if c.test.skip else "-", c.info, sep="\t") return ast = obj.cfg.asterisk url = "http://%s:%d/" % (ast.host,ast.port) async with asyncari.connect(url, ast.app, username=ast.username, password=ast.password) as client: client._calltest_config = obj.cfg async with anyio.create_task_group() as tg: for c in checks: await tg.spawn(obj.calls[c], client)
async def serve(cfg, checks): ast = cfg.asterisk url = "http://%s:%d/" % (ast.host, ast.port) stats = {} socks = set() app = Quart("calltest.server", root_path="/tmp") @app.route("/", methods=['GET']) @app.route("/list", defaults={'with_ok': True}, methods=['GET']) async def index(with_ok=False): s = attrdict() s.fail = list(k for k, v in stats.items() if v.fail_count >= checks[k].test.fail) s.warn = list( k for k, v in stats.items() if checks[k].test.fail > v.fail_count >= checks[k].test.warn) s.note = list(k for k, v in stats.items() if checks[k].test.warn > v.fail_count > 0 or v.fail_count == 0 and v.fail_map) ok = list(k for k, v in stats.items() if v.fail_count == 0 and v.n_run > 0) if with_ok: s.ok = ok s.skip = list(k for k, v in stats.items() if checks[k].test.skip) s.n_skip = len(s.skip) s.n_fail = len(s.fail) s.n_warn = len(s.warn) s.n_note = len(s.note) s.n_ok = len(ok) return jsonify(s) async def alert(**msg): nonlocal socks ds = set() for s in socks: try: await s.send(msg) except Exception: ds.add(s) socks -= ds @app.route("/test/<test>", methods=['GET']) async def test_detail(test): c = checks[test] return jsonify(c.state) @app.route("/test/<test>/start", methods=['PUT']) async def test_start(test): c = checks[test] res = await c.test_start() return jsonify({"success": res}) @app.route("/test/<test>/stop", methods=['PUT']) async def test_stop(test): c = checks[test] res = await c.test_stop(fail=False) return jsonify({"success": res}) @app.route("/test/<test>/fail", methods=['PUT']) async def test_fail(test): c = checks[test] res = await c.test_stop(fail=True) return jsonify({"success": res}) @app.websocket('/ws') async def ws(): try: sock = websocket._get_current_object() socks.add(sock) while True: data = await sock.receive() print("IN", data) finally: socks.discard(sock) async def updated(call): await alert(action="update", name=call.name, state=call.state) stats[call.name] = call.state async with asyncari.connect(url, ast.app, username=ast.username, password=ast.password) as client: client._calltest_config = cfg async with anyio.create_task_group() as tg: await tg.spawn(partial(run, app, **cfg.server, debug=True)) for c in checks.values(): await tg.spawn(partial(c.run, client, updated=updated)) pass # end loop pass # end taskgroup
async def main(): async with asyncari.connect(ast_url, ast_app, ast_username, ast_password) as client: await clean_bridges(client)