def test_attach(): callable = Mock(name="callable") category = "category" with patch("faust.utils.venusian._attach") as _attach: with patch("faust.utils.venusian._on_found") as _on_found: venusian.attach(callable, category) _attach.assert_called_with(callable, _on_found, category=category)
def test_attach(): callable = Mock(name='callable') category = 'category' with patch('faust.utils.venusian._attach') as _attach: with patch('faust.utils.venusian._on_found') as _on_found: venusian.attach(callable, category) _attach.assert_called_with(callable, _on_found, category=category)
def _decorator(fun: PageArg) -> Type[View]: view: Optional[Type[View]] = None if inspect.isclass(fun): view = cast(Type[View], fun) if not issubclass(view, View): raise TypeError( 'When decorating class, it must be subclass of View') if view is None: view = view_base.from_handler(cast(ViewGetHandler, fun)) view.view_name = name or view.__name__ view.view_path = path self.web.add_view(view) venusian.attach(view, category=SCAN_PAGE) return view
def service(self, cls: Type[ServiceT]) -> Type[ServiceT]: """Decorate :class:`mode.Service` to be started with the app. Examples: .. sourcecode:: python from mode import Service @app.service class Foo(Service): ... """ venusian.attach(cls, category=SCAN_SERVICE) self._extra_services.append(cls) return cls
def _inner(fun: AgentFun) -> AgentT: agent = self.conf.Agent(fun, name=name, app=self, channel=channel, concurrency=concurrency, supervisor_strategy=supervisor_strategy, sink=sink, isolated_partitions=isolated_partitions, on_error=self._on_agent_error, help=fun.__doc__, **kwargs) self.agents[agent.name] = agent venusian.attach(agent, category=SCAN_AGENT) return agent
def _task(self, fun: TaskArg, on_leader: bool = False) -> TaskArg: app = self @wraps(fun) async def _wrapped() -> None: should_run = app.is_leader() if on_leader else True if should_run: # pass app only if decorated function takes an argument if inspect.signature(fun).parameters: task_takes_app = cast(Callable[[AppT], Awaitable], fun) return await task_takes_app(app) else: task = cast(Callable[[], Awaitable], fun) return await task() venusian.attach(_wrapped, category=SCAN_TASK) self._tasks.append(_wrapped) return _wrapped
def task(self, fun: TaskArg) -> TaskArg: """Define an async def function to be started with the app. This is like :meth:`timer` but a one-shot task only executed at worker startup (after recovery and the worker is fully ready for operation). The function may take zero, or one argument. If the target function takes an argument, the ``app`` argument is passed:: >>> @app.task >>> async def on_startup(app): ... print('STARTING UP: %r' % (app,)) Nullary functions are also supported:: >>> @app.task >>> async def on_startup(): ... print('STARTING UP') """ venusian.attach(fun, category=SCAN_TASK) self._tasks.append(fun) return fun
def _inner(fun: Callable[..., Awaitable[Any]]) -> Type[AppCommand]: cmd = _base.from_handler(*options, **kwargs)(fun) venusian.attach(cmd, category=SCAN_COMMAND) return cmd
def _decorator(fun: PageArg) -> Type[Site]: site = Site.from_handler(path, base=base)(fun) self.pages.append(('', site)) venusian.attach(site, category=SCAN_PAGE) return site