Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion taskbadger/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def task_publish_handler(sender=None, headers=None, body=None, **kwargs):
return

celery_system = Badger.current.settings.get_system_by_id("celery")
auto_track = celery_system and celery_system.auto_track_tasks
auto_track = celery_system and celery_system.track_task(sender)
manual_track = headers.get("taskbadger_track")
if not manual_track and not auto_track:
return
Expand Down
32 changes: 28 additions & 4 deletions taskbadger/systems/celery.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
import re

from taskbadger.systems import System


class CelerySystemIntegration(System):
identifier = "celery"

def __init__(self, auto_track_tasks=True):
def __init__(self, auto_track_tasks=True, includes=None, excludes=None):
"""
Args:
auto_track_tasks: Automatically track all Celery tasks regardless of whether they are using the
`taskbadger.celery.Task` base class.
includes: A list of task names to include in tracking. These can be either the full task name
(e.g. `myapp.tasks.export_data`) or a regular expression (e.g. `export_.*`). If a task name
matches both an include and an exclude, it will be excluded.
excludes: A list of task names to exclude from tracking. As with `includes`, these can be either
the full task name or a regular expression. Exclusions take precedence over inclusions.
"""
self.auto_track_tasks = auto_track_tasks
if auto_track_tasks:
# Importing this here ensures that the Celery signal handlers are registered
import taskbadger.celery # noqa
self.includes = includes
self.excludes = excludes

def track_task(self, task_name):
if not self.auto_track_tasks:
return False

if self.excludes:
for exclude in self.excludes:
if re.fullmatch(exclude, task_name):
return False

if self.includes:
for include in self.includes:
if re.fullmatch(include, task_name):
break
else:
return False

return True
17 changes: 17 additions & 0 deletions tests/test_celery_system_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,20 @@ def add_normal(self, a, b):
assert get_task.call_count == 1
assert update.call_count == 2
assert Badger.current.session().client is None


@pytest.mark.parametrize(
"include,exclude,expected",
[
(None, None, True),
(["myapp.tasks.export_data"], None, True),
([".*export_data"], [], True),
([".*export_da"], [], False),
(["myapp.tasks.export_data"], ["myapp.tasks.export_data"], False),
([".*"], ["myapp.tasks.export_data"], False),
([".*"], [".*tasks.*"], False),
],
)
def test_task_name_matching(include, exclude, expected: bool):
integration = CelerySystemIntegration(includes=include, excludes=exclude)
assert integration.track_task("myapp.tasks.export_data") is expected