Skip to content

Commit bb92891

Browse files
author
Andrei Neagu
committed
refactor naming
1 parent ef3b687 commit bb92891

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

packages/service-library/src/servicelib/long_running_tasks/_redis_store.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _get_redis_key_task_data_match(self) -> str:
5555
def _get_redis_key_task_data_hash(self, task_id: TaskId) -> str:
5656
return f"{self.namespace}:{_STORE_TYPE_TASK_DATA}:{task_id}"
5757

58-
def _get_key_cancelled_tasks(self) -> str:
58+
def _get_key_to_remove(self) -> str:
5959
return f"{self.namespace}:{_STORE_TYPE_CANCELLED_TASKS}"
6060

6161
# TaskData
@@ -120,25 +120,25 @@ async def delete_task_data(self, task_id: TaskId) -> None:
120120

121121
# to cancel
122122

123-
async def set_to_cancel(
123+
async def set_to_remove(
124124
self, task_id: TaskId, with_task_context: TaskContext
125125
) -> None:
126-
"""marks a task_id to be cancelled"""
126+
"""marks a task_id to be removed"""
127127
await handle_redis_returns_union_types(
128128
self._redis.hset(
129-
self._get_key_cancelled_tasks(), task_id, json_dumps(with_task_context)
129+
self._get_key_to_remove(), task_id, json_dumps(with_task_context)
130130
)
131131
)
132132

133-
async def remove_to_cancel(self, task_id: TaskId) -> None:
134-
"""removes a task_id from the ones to be cancelled"""
133+
async def delete_to_remove(self, task_id: TaskId) -> None:
134+
"""deletes a task_id from the ones to be removed"""
135135
await handle_redis_returns_union_types(
136-
self._redis.hdel(self._get_key_cancelled_tasks(), task_id)
136+
self._redis.hdel(self._get_key_to_remove(), task_id)
137137
)
138138

139-
async def get_all_to_cancel(self) -> dict[TaskId, TaskContext]:
140-
"""returns all task_ids that are to be cancelled"""
139+
async def get_all_to_remove(self) -> dict[TaskId, TaskContext]:
140+
"""returns all task_ids that are to be removed"""
141141
result: dict[str, str | None] = await handle_redis_returns_union_types(
142-
self._redis.hgetall(self._get_key_cancelled_tasks())
142+
self._redis.hgetall(self._get_key_to_remove())
143143
)
144144
return {task_id: json_loads(context) for task_id, context in result.items()}

packages/service-library/src/servicelib/long_running_tasks/task.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,12 @@ async def _stale_tasks_monitor(self) -> None:
267267

268268
async def _cancelled_tasks_removal(self) -> None:
269269
"""
270-
A task can be cancelled by the client, which implies it does not for sure
271-
run in the same process as the one processing the request.
272-
273-
This is a periodic task that ensures the cancellation occurred.
270+
Periodicallu checks which tasks are maked for removal and attempts to remove the
271+
task if it's handled by this process.
274272
"""
275273
self._started_event_task_cancelled_tasks_removal.set()
276274

277-
cancelled_tasks = await self._tasks_data.get_all_to_cancel()
275+
cancelled_tasks = await self._tasks_data.get_all_to_remove()
278276
for task_id in cancelled_tasks:
279277
await self._attempt_cancel_and_remove_local_task(task_id)
280278

@@ -396,7 +394,7 @@ async def _attempt_cancel_and_remove_local_task(self, task_id: TaskId) -> None:
396394
task_to_cancel = self._created_tasks.pop(task_id, None)
397395
if task_to_cancel is not None:
398396
await cancel_wait_task(task_to_cancel)
399-
await self._tasks_data.remove_to_cancel(task_id)
397+
await self._tasks_data.delete_to_remove(task_id)
400398
await self._tasks_data.delete_task_data(task_id)
401399

402400
async def remove_task(
@@ -414,7 +412,7 @@ async def remove_task(
414412
raise
415413
return
416414

417-
await self._tasks_data.set_to_cancel(
415+
await self._tasks_data.set_to_remove(
418416
tracked_task.task_id, tracked_task.task_context
419417
)
420418

packages/service-library/tests/long_running_tasks/test_long_running_tasks__store.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ async def test_workflow(store: RedisStore, task_data: TaskData) -> None:
5050
assert await store.list_tasks_data() == []
5151

5252
# cancelled tasks
53-
assert await store.get_all_to_cancel() == {}
53+
assert await store.get_all_to_remove() == {}
5454

55-
await store.set_to_cancel(task_data.task_id, task_data.task_context)
55+
await store.set_to_remove(task_data.task_id, task_data.task_context)
5656

57-
assert await store.get_all_to_cancel() == {
57+
assert await store.get_all_to_remove() == {
5858
task_data.task_id: task_data.task_context
5959
}
6060

@@ -89,15 +89,15 @@ async def test_workflow_multiple_redis_stores_with_different_namespaces(
8989

9090
for store in redis_stores:
9191
assert await store.list_tasks_data() == []
92-
assert await store.get_all_to_cancel() == {}
92+
assert await store.get_all_to_remove() == {}
9393

9494
for store in redis_stores:
9595
await store.add_task_data(task_data.task_id, task_data)
96-
await store.set_to_cancel(task_data.task_id, {})
96+
await store.set_to_remove(task_data.task_id, {})
9797

9898
for store in redis_stores:
9999
assert await store.list_tasks_data() == [task_data]
100-
assert await store.get_all_to_cancel() == {task_data.task_id: {}}
100+
assert await store.get_all_to_remove() == {task_data.task_id: {}}
101101

102102
for store in redis_stores:
103103
await store.delete_task_data(task_data.task_id)

packages/service-library/tests/long_running_tasks/test_long_running_tasks_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ async def test__cancelled_tasks_worker_equivalent_of_cancellation_from_a_differe
480480
total_sleep=10,
481481
task_context=empty_context,
482482
)
483-
await long_running_manager.tasks_manager._tasks_data.set_to_cancel( # noqa: SLF001
483+
await long_running_manager.tasks_manager._tasks_data.set_to_remove( # noqa: SLF001
484484
task_id, with_task_context=empty_context
485485
)
486486

0 commit comments

Comments
 (0)