-
Notifications
You must be signed in to change notification settings - Fork 19
Feature/ddd phase 4 services #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5a14a9b
--wip-- [skip ci]
TeKrop e7b940a
--wip-- [skip ci]
TeKrop 5eaca90
--wip-- [skip ci]
TeKrop b315ac2
--wip-- [skip ci]
TeKrop 3641690
--wip-- [skip ci]
TeKrop 879acc7
--wip-- [skip ci]
TeKrop 9bc1406
--wip-- [skip ci]
TeKrop 9353761
--wip-- [skip ci]
TeKrop 4a98139
--wip-- [skip ci]
TeKrop 944e617
--wip-- [skip ci]
TeKrop 3f6cf18
--wip-- [skip ci]
TeKrop 314abe7
--wip-- [skip ci]
TeKrop 7a011be
--wip-- [skip ci]
TeKrop 0789b65
--wip-- [skip ci]
TeKrop fa34375
--wip-- [skip ci]
TeKrop abeee2f
fix: typing update
TeKrop 6893800
fix: adjusted hero endpoints cache behaviour
TeKrop a2223ef
--wip-- [skip ci]
TeKrop f2d807a
--wip-- [skip ci]
TeKrop c178877
fix: fixed issue with api-cache and ordering
TeKrop 9fee133
Merge branch 'main' into feature/ddd-phase-4-services
TeKrop fe4aa1f
fix: fixed human readable timeout
TeKrop 5bd4527
fix: review
TeKrop 0b104ff
fix: sonar issues
TeKrop 759b437
fix: ruff format
TeKrop f28e5a4
fix: review
TeKrop ada8798
fix: review
TeKrop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| """Stateless parser functions for heroes hitpoints data (HP, armor, shields) from CSV""" | ||
|
|
||
| from app.adapters.csv import CSVReader | ||
|
|
||
| HITPOINTS_KEYS = {"health", "armor", "shields"} | ||
|
|
||
|
|
||
| def parse_heroes_hitpoints() -> dict[str, dict]: | ||
| """Parse heroes hitpoints (health/armor/shields) from the heroes CSV file. | ||
|
|
||
| Returns: | ||
| Dict mapping hero key to hitpoints data. | ||
| Example: {"ana": {"hitpoints": {"health": 200, "armor": 0, "shields": 0, "total": 200}}} | ||
| """ | ||
| csv_reader = CSVReader() | ||
| csv_data = csv_reader.read_csv_file("heroes") | ||
|
|
||
| return {row["key"]: {"hitpoints": _get_hitpoints(row)} for row in csv_data} | ||
|
|
||
|
|
||
| def _get_hitpoints(row: dict) -> dict: | ||
| """Extract hitpoints data from a hero CSV row.""" | ||
| hitpoints = {hp_key: int(row[hp_key]) for hp_key in HITPOINTS_KEYS} | ||
| hitpoints["total"] = sum(hitpoints.values()) | ||
| return hitpoints |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """AsyncIO task queue adapter — Phase 4 in-process background tasks with deduplication""" | ||
|
|
||
| import asyncio | ||
| import time | ||
| from typing import TYPE_CHECKING, Any, ClassVar | ||
|
|
||
| from app.metaclasses import Singleton | ||
| from app.monitoring.metrics import ( | ||
| background_tasks_duration_seconds, | ||
| background_tasks_queue_size, | ||
| background_tasks_total, | ||
| ) | ||
| from app.overfast_logger import logger | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Awaitable, Callable, Coroutine | ||
|
|
||
|
|
||
| class AsyncioTaskQueue(metaclass=Singleton): | ||
| """In-process task queue backed by asyncio.create_task(). | ||
|
|
||
| Uses a class-level set for deduplication so concurrent requests don't | ||
| trigger duplicate refreshes for the same entity. | ||
|
|
||
| When a ``coro`` is provided to ``enqueue``, it is executed as a real | ||
| background task. Phase 5 will replace this adapter with an arq-backed | ||
| one that dispatches to a worker process instead, but the interface stays | ||
| the same. | ||
| """ | ||
|
|
||
| _pending_jobs: ClassVar[set[str]] = set() | ||
|
|
||
| async def enqueue( # NOSONAR | ||
| self, | ||
| task_name: str, | ||
| *_args: Any, | ||
| job_id: str | None = None, | ||
| coro: Coroutine[Any, Any, Any] | None = None, | ||
| on_complete: Callable[[str], Awaitable[None]] | None = None, | ||
| on_failure: Callable[[str, Exception], Awaitable[None]] | None = None, | ||
| **_kwargs: Any, | ||
| ) -> str: | ||
| """Schedule a background task if not already pending.""" | ||
| effective_id = job_id or task_name | ||
| if effective_id in self._pending_jobs: | ||
| logger.debug(f"[TaskQueue] Skipping duplicate job: {effective_id}") | ||
| if coro is not None: | ||
| coro.close() # avoid "coroutine was never awaited" warning | ||
| return effective_id | ||
|
|
||
| self._pending_jobs.add(effective_id) | ||
TeKrop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| background_tasks_queue_size.labels(task_type=task_name).inc() | ||
|
|
||
| async def _run() -> None: | ||
| start = time.monotonic() | ||
| status = "success" | ||
| try: | ||
| logger.info( | ||
| f"[TaskQueue] Running task '{task_name}' (job_id={effective_id})" | ||
| ) | ||
| if coro is not None: | ||
| await coro | ||
| if on_complete is not None: | ||
| await on_complete(effective_id) | ||
| except Exception as exc: # noqa: BLE001 | ||
| status = "failure" | ||
| logger.warning( | ||
| f"[TaskQueue] Task '{task_name}' (job_id={effective_id}) failed: {exc}" | ||
| ) | ||
| if on_failure is not None: | ||
| await on_failure(effective_id, exc) | ||
| finally: | ||
| elapsed = time.monotonic() - start | ||
| background_tasks_total.labels(task_type=task_name, status=status).inc() | ||
| background_tasks_duration_seconds.labels(task_type=task_name).observe( | ||
| elapsed | ||
| ) | ||
| background_tasks_queue_size.labels(task_type=task_name).dec() | ||
| self._pending_jobs.discard(effective_id) | ||
|
|
||
| task = asyncio.create_task(_run(), name=effective_id) | ||
| task.add_done_callback(lambda _: None) | ||
| return effective_id | ||
|
|
||
| async def is_job_pending_or_running(self, job_id: str) -> bool: # NOSONAR | ||
| """Return True if a job with this ID is already in-flight.""" | ||
| return job_id in self._pending_jobs | ||
TeKrop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.