def test_task_registered(self): """Test that @action decorator properly registers the task with the current app.""" @action(name='i_should_be_registered') def test_task_should_be_reg(self): return from celery._state import get_current_app assert 'i_should_be_registered' in get_current_app().tasks
def test_task_no_kwargs(self): """Test that @action decorator works w/o kwargs.""" #with using_test_app() as app: @action def test_no_kwargs_task(self, context): return 42 from celery._state import get_current_app assert "{}.test_no_kwargs_task".format(__name__) in get_current_app().tasks
def setUp(self): self.app = get_current_app() self.old_broker_host = self.app.conf.BROKER_HOST or '' # Modifying the broken host name simulates the task broker being # 'unresponsive' # We need to make this modification in 3 places because of version # backwards compatibility self._set_broker_host('amqp://') self.app.conf['CELERY_TASK_PUBLISH_RETRY'] = False self.app._pool = None # Deleting the cache AMQP class so that it gets recreated with the new # BROKER_URL del self.app.amqp self.task = ParrotTask
def setup_default_app(app, use_trap=False): """Setup default app for testing. Ensures state is clean after the test returns. """ prev_current_app = _state.get_current_app() prev_default_app = _state.default_app prev_finalizers = set(_state._on_app_finalizers) prev_apps = weakref.WeakSet(_state._apps) if use_trap: with set_trap(app): yield else: yield _state.set_default_app(prev_default_app) _state._tls.current_app = prev_current_app if app is not prev_current_app: app.close() _state._on_app_finalizers = prev_finalizers _state._apps = prev_apps
def _unpickle_task_v2(name, module=None): if module: import_module(module) return get_current_app().tasks[name]
def _unpickle_task(name): return get_current_app().tasks[name]
def _app_or_default(app=None): if app is None: return _state.get_current_app() return app
def task_by_cons(): app = _state.get_current_app() return app.tasks[ name or app.gen_task_name(fun.__name__, fun.__module__) ]
def _get_default_app(self, *args, **kwargs): from celery._state import get_current_app return get_current_app() # omit proxy
def task_by_cons(): app = _state.get_current_app() return app.tasks[ name or app.gen_task_name(fun.__name__, fun.__module__)]
def test_get_default_app(self): self.patch('celery._state.get_current_app') cmd = MockCommand(app=self.app) from celery._state import get_current_app self.assertIs(cmd._get_default_app(), get_current_app())
def test_get_default_app(self, app, patching): patching('celery._state.get_current_app') cmd = MockCommand(app=app) from celery._state import get_current_app assert cmd._get_default_app() is get_current_app()
def app(self): """Instantiate the app if it wasn't already and return it. This lazy approach gives opportunity for further initializations before the app is imported.""" return self._app_factory() or get_current_app()
def bugreport(app=None): """Return information useful in bug reports.""" return (app or _state.get_current_app()).bugreport()
def _unpickle_appattr(reverse_name, args): """Given an attribute name and a list of args, gets the attribute from the current app and calls it.""" return get_current_app()._rgetattr(reverse_name)(*args)
from celery._state import get_current_app app = get_current_app() @app.task() def add(x, y): print(x + y)
def __init__(self, app, no_color, workdir, quiet=False): """Initialize the CLI context.""" self.app = app or get_current_app() self.no_color = no_color self.quiet = quiet self.workdir = workdir