Skip to content

Commit

Permalink
CeleryApp allows overriding the task class. (#78)
Browse files Browse the repository at this point in the history
* CeleryApp allows overriding the task class.

Fixes #76

* Don't set test app as current
  • Loading branch information
maciej-gol authored May 21, 2023
1 parent 9f76a5c commit 66ad4aa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tenant_schemas_celery/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ def restore_schema(task, **kwargs):

class CeleryApp(Celery):
registry_cls = 'tenant_schemas_celery.registry:TenantTaskRegistry'

task_cls = 'tenant_schemas_celery.task:TenantTask'

def create_task_cls(self):
return self.subclass_with_self(
"tenant_schemas_celery.task:TenantTask",
self.task_cls,
abstract=True,
name="TenantTask",
attribute="_app",
Expand Down
32 changes: 32 additions & 0 deletions tenant_schemas_celery/app_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from tenant_schemas_celery.app import CeleryApp
from tenant_schemas_celery.task import TenantTask


class DummyTask(TenantTask):
...


def test_celery_app_should_allow_overriding_task_cls_as_object() -> None:
class App(CeleryApp):
task_cls = DummyTask

app = App(set_as_current=False)

@app.task()
def some_task() -> None:
...

assert isinstance(some_task, DummyTask)


def test_celery_app_should_allow_overriding_task_cls_as_string() -> None:
class App(CeleryApp):
task_cls = f"{DummyTask.__module__}:{DummyTask.__name__}"

app = App(set_as_current=False)

@app.task()
def some_task() -> None:
...

assert isinstance(some_task, DummyTask)

0 comments on commit 66ad4aa

Please sign in to comment.