-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
287 additions
and
108 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 was deleted.
Oops, something went wrong.
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 @@ | ||
from .cron import Cron, cron | ||
from .heartbeat import heartbeat |
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,47 @@ | ||
from __future__ import annotations | ||
|
||
from binascii import hexlify | ||
from os import urandom | ||
from typing import Any, Callable, Literal, TypeVar | ||
|
||
from .event import Event | ||
from .scheduler import scheduler | ||
|
||
T = TypeVar("T") | ||
|
||
class Cron: | ||
identifier: str | ||
digest: str | ||
|
||
def __init__(self, identifier: str) -> None: | ||
self.identifier = identifier | ||
self.digest = hexlify(urandom(8)).decode("utf-8") | ||
|
||
def start(self) -> None: | ||
scheduler.schedule(Event.cron(self.identifier, self.digest, "start")) | ||
|
||
def finish(self) -> None: | ||
scheduler.schedule(Event.cron(self.identifier, self.digest, "finish")) | ||
|
||
def __enter__(self) -> None: | ||
self.start() | ||
|
||
def __exit__( | ||
self, exc_type: Any = None, exc_value: Any = None, traceback: Any = None | ||
) -> Literal[False]: | ||
if exc_type is None: | ||
self.finish() | ||
|
||
return False | ||
|
||
|
||
def cron(identifier: str, fn: Callable[[], T] | None = None) -> None | T: | ||
cron = Cron(identifier) | ||
output = None | ||
|
||
if fn is not None: | ||
cron.start() | ||
output = fn() | ||
|
||
cron.finish() | ||
return output |
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,78 @@ | ||
from __future__ import annotations | ||
|
||
from time import time | ||
|
||
from typing import TYPE_CHECKING, TypedDict | ||
|
||
if TYPE_CHECKING: | ||
from typing import Literal, Optional, Union, Self, List | ||
|
||
EventKind = Union[Literal["start"], Literal["finish"]] | ||
|
||
EventCheckInType = Union[Literal["cron"], Literal["heartbeat"]] | ||
|
||
class Event(TypedDict): | ||
identifier: str | ||
digest: Optional[str] | ||
kind: Optional[EventKind] | ||
timestamp: int | ||
check_in_type: EventCheckInType | ||
|
||
def __init__(self, **kwargs: Event) -> None: | ||
super().__init__(**{ | ||
**kwargs, | ||
"timestamp": int(time()) | ||
}) | ||
|
||
@classmethod | ||
def cron(cls, identifier: str, digest: str, kind: EventKind) -> Self: | ||
return cls( | ||
identifier=identifier, | ||
digest=digest, | ||
kind=kind, | ||
check_in_type="cron" | ||
) | ||
|
||
@classmethod | ||
def heartbeat(cls, identifier: str) -> Self: | ||
return cls( | ||
identifier=identifier, | ||
check_in_type="heartbeat" | ||
) | ||
|
||
def is_redundant(self, other: Self) -> bool: | ||
if ( | ||
self["check_in_type"] not in ["cron", "heartbeat"] or | ||
self["check_in_type"] != other["check_in_type"] or | ||
self["identifier"] != other["identifier"] | ||
): | ||
return False | ||
|
||
if self["check_in_type"] == "cron" and ( | ||
self["digest"] != other["digest"] or | ||
self["kind"] != other["kind"] | ||
): | ||
return False | ||
|
||
return True | ||
|
||
@classmethod | ||
def describe(cls, events: List[Self]) -> str: | ||
if not events: | ||
# This shouldn't happen. | ||
return "no check-in events" | ||
elif len(events) > 1: | ||
return f"{len(events)} check-in events" | ||
else: | ||
event = events[0] | ||
if event["check_in_type"] == "cron": | ||
return ( | ||
f"cron check-in `{event.get('identifier', 'unknown')}` " | ||
f"{event.get('kind', 'unknown')} event " | ||
f"(digest {event.get('digest', 'unknown')})" | ||
) | ||
elif event["check_in_type"] == "heartbeat": | ||
return f"heartbeat check-in `{event.get('identifier', 'unknown')}` event" | ||
else: | ||
# This shouldn't happen. | ||
return "unknown check-in event" |
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,17 @@ | ||
from .scheduler import scheduler | ||
from .event import Event | ||
from threading import Thread | ||
from time import sleep | ||
|
||
_HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS = 30 | ||
|
||
def _continuous_heartbeat(name: str) -> None: | ||
while True: | ||
sleep(_HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS) | ||
heartbeat(name) | ||
|
||
def heartbeat(name: str, continuous: bool = False) -> None: | ||
if continuous: | ||
thread = Thread(target=_continuous_heartbeat, args=(name,)) | ||
thread.start() | ||
scheduler.schedule(Event.heartbeat(name)) |
Oops, something went wrong.