-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Done] Improve celery integration (#92)
- Loading branch information
Showing
14 changed files
with
308 additions
and
110 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .base_task import * # NOQA | ||
from .celery_task_logger import * # NOQA | ||
from .config import * # NOQA | ||
from .kubernetes import * # NOQA |
This file contains 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 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 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,31 @@ | ||
from celery import Celery | ||
from celery import current_app | ||
|
||
from clean_python import Json | ||
from clean_python import ValueObject | ||
from clean_python.celery import BaseTask | ||
|
||
__all__ = ["CeleryConfig"] | ||
|
||
|
||
class CeleryConfig(ValueObject): | ||
timezone: str = "Europe/Amsterdam" | ||
broker_url: str | ||
broker_transport_options: Json = {"socket_timeout": 2} | ||
broker_connection_retry_on_startup: bool = True | ||
result_backend: str | None = None | ||
worker_prefetch_multiplier: int = 1 | ||
task_always_eager: bool = False | ||
task_eager_propagates: bool = False | ||
task_acks_late: bool = True | ||
task_default_queue: str = "default" | ||
task_default_priority: int = 0 | ||
task_queue_max_priority: int = 10 | ||
task_track_started: bool = True | ||
|
||
def apply(self, strict_typing: bool = True) -> Celery: | ||
app = current_app if current_app else Celery() | ||
app.task_cls = BaseTask | ||
app.strict_typing = strict_typing | ||
app.config_from_object(self) | ||
return app |
This file contains 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 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,22 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
from clean_python.celery import CeleryConfig | ||
from clean_python.celery import CeleryTaskLogger | ||
from clean_python.celery import set_task_logger | ||
from clean_python.testing.debugger import setup_debugger | ||
|
||
from .logger import MultilineJsonFileGateway | ||
from .tasks import sleep_task # NOQA | ||
|
||
app = CeleryConfig( | ||
broker_url="amqp://cleanpython:cleanpython@localhost/cleanpython", | ||
result_backend="rpc://", | ||
).apply() | ||
# the file path is set from the test fixture | ||
logging_path = os.environ.get("CLEAN_PYTHON_TEST_LOGGING") | ||
if logging_path: | ||
set_task_logger(CeleryTaskLogger(MultilineJsonFileGateway(Path(logging_path)))) | ||
debug_port = os.environ.get("CLEAN_PYTHON_TEST_DEBUG") | ||
if debug_port: | ||
setup_debugger(port=int(debug_port)) |
This file contains 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,34 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
from clean_python import Filter | ||
from clean_python import Json | ||
from clean_python import PageOptions | ||
from clean_python import SyncGateway | ||
|
||
__all__ = ["MultilineJsonFileGateway"] | ||
|
||
|
||
class MultilineJsonFileGateway(SyncGateway): | ||
def __init__(self, path: Path) -> None: | ||
self.path = path | ||
|
||
def clear(self): | ||
if self.path.exists(): | ||
self.path.unlink() | ||
|
||
def filter( | ||
self, filters: list[Filter], params: PageOptions | None = None | ||
) -> list[Json]: | ||
assert not filters | ||
assert not params | ||
if not self.path.exists(): | ||
return [] | ||
with self.path.open("r") as f: | ||
return [json.loads(line) for line in f] | ||
|
||
def add(self, item: Json) -> Json: | ||
with self.path.open("a") as f: | ||
f.write(json.dumps(item)) | ||
f.write("\n") | ||
return item |
This file contains 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,34 @@ | ||
import time | ||
|
||
from celery import shared_task | ||
from celery import Task | ||
from celery.exceptions import Ignore | ||
from celery.exceptions import Reject | ||
|
||
from clean_python import ctx | ||
|
||
|
||
@shared_task(bind=True, name="testing") | ||
def sleep_task(self: Task, seconds: float, return_value=None, event="success"): | ||
event = event.lower() | ||
if event == "success": | ||
time.sleep(int(seconds)) | ||
elif event == "crash": | ||
import ctypes | ||
|
||
ctypes.string_at(0) # segfault | ||
elif event == "ignore": | ||
raise Ignore() | ||
elif event == "reject": | ||
raise Reject() | ||
elif event == "retry": | ||
raise self.retry(countdown=seconds, max_retries=1) | ||
elif event == "context": | ||
return { | ||
"tenant_id": ctx.tenant.id, | ||
"correlation_id": str(ctx.correlation_id), | ||
} | ||
else: | ||
raise ValueError(f"Unknown event '{event}'") | ||
|
||
return {"value": return_value} |
This file contains 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
Oops, something went wrong.