Skip to content

Commit 87b0665

Browse files
committed
set tags from scope
1 parent b4c4165 commit 87b0665

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

taskbadger/mug.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,23 @@ class Scope:
8383
def __init__(self):
8484
self.stack = []
8585
self.context = {}
86+
self.tags = {}
8687

8788
def __enter__(self):
88-
self.stack.append(self.context)
89+
self.stack.append((self.context, self.tags))
8990
self.context = self.context.copy()
91+
self.tags = self.tags.copy()
9092
return self
9193

9294
def __exit__(self, *args):
93-
self.context = self.stack.pop()
95+
self.context, self.tags = self.stack.pop()
9496

9597
def __setitem__(self, key, value):
9698
self.context[key] = value
9799

100+
def tag(self, tags: dict[str, str]):
101+
self.tags.update(tags)
102+
98103

99104
class MugMeta(type):
100105
@property

taskbadger/sdk.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,15 @@ def create_task(
130130
max_runtime=max_runtime,
131131
stale_timeout=stale_timeout,
132132
)
133-
scope_data = Badger.current.scope().context
134-
if scope_data or data:
133+
scope = Badger.current.scope()
134+
if scope.context or data:
135135
data = data or {}
136-
task.data = {**scope_data, **data}
136+
task.data = {**scope.context, **data}
137137
if actions:
138138
task.additional_properties = {"actions": [a.to_dict() for a in actions]}
139-
if tags:
140-
task.tags = TaskRequestTags.from_dict(tags)
139+
if scope.tags or tags:
140+
tags = tags or {}
141+
task.tags = TaskRequestTags.from_dict({**scope.tags, **tags})
141142
kwargs = _make_args(body=task)
142143
if monitor_id:
143144
kwargs["x_taskbadger_monitor"] = monitor_id

tests/test_celery.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ def add_with_task_args(self, a, b):
7777
(2, 2),
7878
taskbadger_name="new_name",
7979
taskbadger_value_max=10,
80-
taskbadger_kwargs={"data": {"foo": "bar"}},
80+
taskbadger_kwargs={"data": {"foo": "bar"}, "tags": {"bar": "baz"}},
8181
)
8282
assert result.get(timeout=10, propagate=True) == 4
8383

84-
create.assert_called_once_with("new_name", value_max=10, data={"foo": "bar"}, status=StatusEnum.PENDING)
84+
create.assert_called_once_with(
85+
"new_name", value_max=10, data={"foo": "bar"}, tags={"bar": "baz"}, status=StatusEnum.PENDING
86+
)
8587

8688

8789
def test_celery_task_with_kwargs(celery_session_app, celery_session_worker, bind_settings):

tests/test_scope.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,58 @@ def test_scope_singleton():
99
assert Badger.current == GLOBAL_MUG
1010
scope = Badger.current.scope()
1111
assert scope.context == {}
12+
assert scope.tags == {}
1213
assert scope.stack == []
1314
assert scope == Badger.current.scope()
1415

1516

1617
def test_scope_context():
1718
scope = Badger.current.scope()
1819
assert scope.context == {}
20+
assert scope.tags == {}
1921
assert scope.stack == []
2022
with scope:
21-
assert scope.stack == [{}]
23+
assert scope.stack == [({}, {})]
2224
scope.context["foo"] = "bar"
25+
scope.tags["name"] = "value"
2326
with scope:
24-
assert scope.stack == [{}, {"foo": "bar"}]
27+
assert scope.stack == [({}, {}), ({"foo": "bar"}, {"name": "value"})]
2528
assert scope.context == {"foo": "bar"}
29+
assert scope.tags == {"name": "value"}
2630
scope.context["bar"] = "bazz"
31+
scope.tags["bar"] = "bazz"
2732
with scope:
2833
assert scope.context == {"foo": "bar", "bar": "bazz"}
34+
assert scope.tags == {"name": "value", "bar": "bazz"}
2935
scope.context.clear()
36+
scope.tags.clear()
3037
assert scope.context == {"foo": "bar"}
31-
assert scope.stack == [{}]
38+
assert scope.tags == {"name": "value"}
39+
assert scope.stack == [({}, {})]
3240
assert scope.context == {}
41+
assert scope.tags == {}
3342
assert scope.stack == []
3443

3544

3645
@pytest.fixture(autouse=True)
37-
def init_skd():
46+
def _init_skd():
3847
init("org", "project", "token")
3948

4049

4150
def test_create_task_with_scope(httpx_mock):
4251
with Badger.current.scope() as scope:
4352
scope["foo"] = "bar"
4453
scope["bar"] = "bazz"
54+
scope.tag({"name": "value"})
4555
httpx_mock.add_response(
4656
url="https://taskbadger.net/api/org/project/tasks/",
4757
method="POST",
4858
match_headers={"Authorization": "Bearer token"},
49-
match_content=b'{"name": "name", "status": "pending", "data": {"foo": "bar", "bar": "buzzer"}}',
59+
match_content=b'{"name": "name", "status": "pending", '
60+
b'"data": {"foo": "bar", "bar": "buzzer"}, '
61+
b'"tags": {"name": "value", "name1": "value1"}}',
5062
json=_json_task_response(),
5163
status_code=201,
5264
)
53-
task = create_task("name", data={"bar": "buzzer"})
65+
task = create_task("name", data={"bar": "buzzer"}, tags={"name1": "value1"})
5466
_verify_task(task)

0 commit comments

Comments
 (0)