-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(worker): Add TaskWorker mode for TaskBroker RPC #582
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
base: main
Are you sure you want to change the base?
Changes from all commits
d4591fd
b9a31d1
43d6cac
defc790
514bfa5
282142f
ccf83f8
d91cd45
e7fae19
31e60c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import os | ||
|
|
||
| _TASKWORKER_ONLY_PROJECT_IDS: set[str] = { | ||
| p.strip() for p in os.getenv("PROJECT_IDS_TO_ONLY_TRY_TASKWORKER_PROCESSING", "").split(",") if p.strip() | ||
| } | ||
|
|
||
|
|
||
| def is_taskworker_only_project(project_id: str) -> bool: | ||
| return project_id in _TASKWORKER_ONLY_PROJECT_IDS | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import os | ||
| import platform | ||
| import resource | ||
| import time | ||
|
|
||
| from collections.abc import Generator | ||
| from contextlib import contextmanager | ||
|
|
||
| from arroyo.backends.kafka import KafkaProducer | ||
| from taskbroker_client.app import TaskbrokerApp | ||
| from taskbroker_client.metrics import MetricsBackend, Tags | ||
| from taskbroker_client.router import TaskRouter | ||
|
|
||
| from launchpad.utils.statsd import create_dogstatsd_client | ||
|
|
||
| _RUSAGE_TO_BYTES = 1 if platform.system() == "Darwin" else 1024 | ||
|
|
||
|
|
||
| def _convert_tags(tags: Tags | None) -> list[str] | None: | ||
| if tags is None: | ||
| return None | ||
| return [f"{k}:{v}" for k, v in tags.items()] | ||
|
|
||
|
|
||
| class TaskworkerMetricsBackend(MetricsBackend): | ||
| def __init__(self) -> None: | ||
| self._dogstatsd = create_dogstatsd_client("launchpad.taskworker") | ||
|
|
||
| def incr( | ||
| self, | ||
| name: str, | ||
| value: int | float = 1, | ||
| tags: Tags | None = None, | ||
| sample_rate: float | None = None, | ||
| ) -> None: | ||
| kwargs: dict = {"tags": _convert_tags(tags)} | ||
| if sample_rate is not None: | ||
| kwargs["sample_rate"] = sample_rate | ||
| self._dogstatsd.increment(name, int(value), **kwargs) | ||
|
|
||
| def distribution( | ||
| self, | ||
| name: str, | ||
| value: int | float, | ||
| tags: Tags | None = None, | ||
| unit: str | None = None, | ||
| sample_rate: float | None = None, | ||
| ) -> None: | ||
| kwargs: dict = {"tags": _convert_tags(tags)} | ||
| if sample_rate is not None: | ||
| kwargs["sample_rate"] = sample_rate | ||
| self._dogstatsd.distribution(name, value, **kwargs) | ||
|
|
||
| @contextmanager | ||
| def timer( | ||
| self, | ||
| key: str, | ||
| tags: Tags | None = None, | ||
| sample_rate: float | None = None, | ||
| stacklevel: int = 0, | ||
| ) -> Generator[None]: | ||
| start = time.monotonic() | ||
| try: | ||
| yield | ||
| finally: | ||
| duration_ms = (time.monotonic() - start) * 1000 | ||
| self.distribution(key, duration_ms, tags=tags, unit="millisecond", sample_rate=sample_rate) | ||
|
|
||
| @contextmanager | ||
| def track_memory_usage( | ||
| self, | ||
| key: str, | ||
| tags: Tags | None = None, | ||
| ) -> Generator[None]: | ||
| before = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss | ||
| try: | ||
| yield | ||
| finally: | ||
| after = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss | ||
| self.distribution(key, (after - before) * _RUSAGE_TO_BYTES, tags=tags, unit="byte") | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
NicoHinderling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class CustomRouter(TaskRouter): | ||
| def route_namespace(self, name: str) -> str: | ||
| return "taskworker-launchpad" | ||
|
|
||
|
|
||
| def producer_factory(topic: str) -> KafkaProducer: | ||
| bootstrap_servers = os.getenv("KAFKA_BOOTSTRAP_SERVERS", "127.0.0.1:9092") | ||
| config = { | ||
| "bootstrap.servers": bootstrap_servers, | ||
| "compression.type": "lz4", | ||
| "message.max.bytes": 50000000, | ||
| } | ||
| return KafkaProducer(config) | ||
|
|
||
|
|
||
| app = TaskbrokerApp( | ||
| name="launchpad", | ||
| producer_factory=producer_factory, | ||
| router_class=CustomRouter(), | ||
NicoHinderling marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll want worker runtime metrics too. You'll need a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alright did a take to implement 🙏
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That looks ok to me. Does launchpad not have an existing metrics API that you could wrap?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes this is our current metrics implementation https://github.com/getsentry/launchpad/blob/main/src/launchpad/utils/statsd.py went ahead and updated this branch to make sure we didn't duplicate logic |
||
| metrics_class=TaskworkerMetricsBackend(), | ||
| ) | ||
|
|
||
| app.set_modules(["launchpad.worker.tasks"]) | ||
Uh oh!
There was an error while loading. Please reload this page.