-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from RHDZMOTA/DEV
2022-09-06 RELEASE-PRD (1)
- Loading branch information
Showing
10 changed files
with
129 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ipython==8.4.0 | ||
celery[redis]==5.1.2 |
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 +1 @@ | ||
1.2.0 | ||
2.0.0 |
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,12 @@ | ||
from celery import Celery | ||
|
||
from .settings import ( | ||
RHDZMOTA_CELERY_BROKER_HOST | ||
) | ||
|
||
|
||
# Celery application instance | ||
app = Celery( | ||
"tasks", | ||
broker=f"redis://{RHDZMOTA_CELERY_BROKER_HOST}", | ||
) |
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,18 @@ | ||
|
||
from .celery import app | ||
from .heartbeat.daemon import Daemon | ||
|
||
|
||
@app.task | ||
def daemon_publisher(**kwargs): | ||
print(kwargs) | ||
|
||
|
||
# Daemon instance | ||
daemon = Daemon(name="hello-world", publisher=daemon_publisher) | ||
daemon.broadcast() | ||
|
||
|
||
@app.task | ||
def worker(name: str) -> str: | ||
return f"Hello, {name or 'world'}!" |
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
Empty file.
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,73 @@ | ||
from threading import Thread | ||
from typing import Any, Callable, Optional, Union, TYPE_CHECKING | ||
|
||
from .daemon_heart import DaemonHeart | ||
|
||
|
||
if TYPE_CHECKING: | ||
# TODO: Quickfix guided by https://stackoverflow.com/questions/63714223/correct-type-annotation-for-a-celery-task | ||
from celery.task import Task | ||
from celery.local import PromiseProxy | ||
|
||
CELERY_TASK_TYPE = Union[Task, PromiseProxy] | ||
else: | ||
CELERY_TASK_TYPE = Any | ||
|
||
|
||
class Daemon: | ||
|
||
@staticmethod | ||
def summon(name: str, publisher: CELERY_TASK_TYPE, **heart_configs): | ||
def decorator(worker: CELERY_TASK_TYPE): | ||
def wrapper(*args, **kwargs): | ||
# Initialize the daemon and broadcast heartbeat | ||
daemon = Daemon(name=name, publisher=publisher, **heart_configs) | ||
daemon.broadcast() | ||
# Execute the worker function | ||
output = worker.delay(*args, **kwargs) | ||
# Kill the daemon | ||
daemon.kill() | ||
return output | ||
return wrapper | ||
return decorator | ||
|
||
def __init__( | ||
self, | ||
name: str, | ||
publisher: CELERY_TASK_TYPE, | ||
**heart_configs | ||
): | ||
self.name = name | ||
self.publisher = publisher | ||
self.broadcast_thread: Optional[Thread] = None | ||
self.heart = DaemonHeart.beat( | ||
app_name=name, | ||
**{ # type: ignore | ||
"mode": DaemonHeart.Mode.MONITOR, | ||
"enable_beat_logs": True, | ||
"enable_pulse_monitor": True, | ||
"pulse_monitor_frequency": None, | ||
"pulse_monitor_sensibility_factor": 1.5, | ||
**heart_configs | ||
} | ||
) | ||
|
||
def kill(self): | ||
# Stop heart | ||
self.heart.stroke() | ||
self.heart.join() | ||
self.heart.close() | ||
# Stop broadcast | ||
self.broadcast_thread.join() if self.broadcast_thread is not None else None | ||
|
||
def broadcast(self): | ||
self.broadcast_thread: Thread = Thread( | ||
target=lambda: self._broadcast(), | ||
daemon=True | ||
) | ||
self.broadcast_thread.start() | ||
|
||
def _broadcast(self): | ||
while not self.heart.no_pulse.is_set(): | ||
payload = self.heart.queue_beat_logs.get(block=True) | ||
self.publisher.delay(**payload) |
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 @@ | ||
celery -A rhdzmota.celery_worker_hello worker --loglevel=INFO --concurrency=2 -O fair -P prefork |