Exemplo n.º 1
0
 async def dispatch_subscribers(
     self,
     event_name: str,
     agent_id: AgentId,
     args: Tuple[Any, ...] = tuple()) -> None:
     log_fmt = 'DISPATCH_SUBSCRIBERS(ev:{}, ag:{})'
     log_args = (event_name, agent_id)
     if self.root_app['config']['debug']['log-events']:
         log.debug(log_fmt, *log_args)
     scheduler = get_scheduler_from_app(self.root_app)
     for subscriber in self.subscribers[event_name]:
         cb = subscriber.callback
         try:
             if asyncio.iscoroutine(cb):
                 await scheduler.spawn(cb)
             elif asyncio.iscoroutinefunction(cb):
                 await scheduler.spawn(
                     cb(subscriber.context, agent_id, event_name, *args))
             else:
                 cb = functools.partial(cb, subscriber.context, agent_id,
                                        event_name, *args)
                 self.loop.call_soon(cb)
         except asyncio.CancelledError:
             raise
         except Exception:
             log.exception(log_fmt + ': unexpected-error', *log_args)
Exemplo n.º 2
0
async def setup_feeds(app):
    scheduler = get_scheduler_from_app(app)
    await scheduler.spawn(pub_squeue_items(app))
    await scheduler.spawn(sub_intensity_feed(app))
    await scheduler.spawn(sub_results_path_feed(app))
    await scheduler.spawn(sub_sensor_reading_feed(app))

    return app
Exemplo n.º 3
0
 def get_all_jobs(self, request):
     scheduler = get_scheduler_from_app(self.cbpi.app)
     result = []
     for j in scheduler:
         try:
             result.append(dict(name=j.name, type=j.type, time=j.start_time))
         except:
             pass
     return web.json_response(data=result)
Exemplo n.º 4
0
 async def dispatch(self, event_name, agent_id, args=tuple()):
     log.debug(f"DISPATCH({event_name}/{agent_id})")
     scheduler = get_scheduler_from_app(self.root_app)
     for handler in self.handlers[event_name]:
         cb = handler.callback
         if asyncio.iscoroutine(cb) or asyncio.iscoroutinefunction(cb):
             await scheduler.spawn(cb(handler.app, agent_id, *args))
         else:
             cb = functools.partial(cb, handler.app, agent_id, *args)
             self.loop.call_soon(cb)
Exemplo n.º 5
0
 def get_all_jobs(self, request):
     scheduler = get_scheduler_from_app(self.cbpi.app)
     result = []
     for j in scheduler:
         try:
             print(
                 datetime.datetime.fromtimestamp(
                     j.start_time).strftime('%Y-%m-%d %H:%M:%S'))
             result.append(dict(name=j.name, type=j.type,
                                time=j.start_time))
         except:
             pass
         # await j.close()
     return web.json_response(data=result)
Exemplo n.º 6
0
async def test_atomic(test_client):
    @atomic
    async def handler(request):
        await asyncio.sleep(0)
        return web.Response()

    app = web.Application()
    app.router.add_get('/', handler)
    aiojobs_setup(app)

    client = await test_client(app)
    resp = await client.get('/')
    assert resp.status == 200

    scheduler = get_scheduler_from_app(app)

    assert scheduler.active_count == 0
    assert scheduler.pending_count == 0
Exemplo n.º 7
0
async def test_atomic_from_view(test_client):
    app = web.Application()

    class MyView(web.View):
        @atomic
        async def get(self):
            return web.Response()

    app.router.add_route("*", "/", MyView)
    aiojobs_setup(app)

    client = await test_client(app)
    resp = await client.get('/')
    assert resp.status == 200

    scheduler = get_scheduler_from_app(app)

    assert scheduler.active_count == 0
    assert scheduler.pending_count == 0
Exemplo n.º 8
0
 def get_all_jobs(self, request):
     """
     ---
     description: Get all running Jobs
     tags:
     - System
     responses:
         "200":
             description: successful operation
     """
     scheduler = get_scheduler_from_app(self.cbpi.app)
     result = []
     for j in scheduler:
         try:
             result.append(dict(name=j.name, type=j.type,
                                time=j.start_time))
         except:
             pass
     return web.json_response(data=result)
Exemplo n.º 9
0
 async def get(self):
     assert get_scheduler_from_request(self.request) ==\
         get_scheduler_from_app(app)
     return web.Response()
Exemplo n.º 10
0
async def on_cleanup(app):
    logger.info('clean up app')
    scheduler = get_scheduler_from_app(app)
    logger.info(f'closing scheduler: {scheduler}')
    await scheduler.close()