-
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.
Implement scheduler and heartbeat check-ins
Implement a scheduler for check-in events, ensuring that the events are sent asynchronously from the thread in which they are scheduled. Implement a heartbeat check-in helper, which sends heartbeat check-in events. When given the `continuous=True` flag, the events will be sent continously from a separate thread during the duration of the process.
- Loading branch information
Showing
20 changed files
with
1,012 additions
and
460 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,26 @@ | ||
--- | ||
bump: minor | ||
type: add | ||
--- | ||
|
||
Add support for heartbeat check-ins. | ||
|
||
Use the `appsignal.check_in.heartbeat` function to send a single heartbeat check-in event from your application. This can be used, for example, in your application's main loop: | ||
|
||
```python | ||
from appsignal.check_in import heartbeat | ||
|
||
while True: | ||
heartbeat("job_processor") | ||
process_job() | ||
``` | ||
|
||
Heartbeats are deduplicated and sent asynchronously, without blocking the current thread. Regardless of how often the `.heartbeat` function is called, at most one heartbeat with the same identifier will be sent every ten seconds. | ||
|
||
Pass `continuous=True` as the second argument to send heartbeats continuously during the entire lifetime of the current process. This can be used, for example, after your application has finished its boot process: | ||
|
||
```python | ||
def main(): | ||
start_app() | ||
heartbeat("my_app", continuous=True) | ||
``` |
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,6 @@ | ||
--- | ||
bump: patch | ||
type: change | ||
--- | ||
|
||
Send check-ins concurrently. When calling `appsignal.check_in.cron`, instead of blocking the current thread while the check-in events are sent, schedule them to be sent in a separate thread. |
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,2 +1,5 @@ | ||
from .cron import Cron, cron | ||
from .heartbeat import heartbeat | ||
|
||
|
||
__all__ = ["Cron", "cron", "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
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,78 +1,72 @@ | ||
from __future__ import annotations | ||
|
||
from time import time | ||
from typing import Literal, TypedDict, Union | ||
|
||
from typing import TYPE_CHECKING, TypedDict | ||
from typing_extensions import NotRequired | ||
|
||
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] | ||
digest: NotRequired[str] | ||
kind: NotRequired[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( | ||
|
||
def cron(identifier: str, digest: str, kind: EventKind) -> Event: | ||
return Event( | ||
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"] | ||
): | ||
timestamp=int(time()), | ||
check_in_type="cron", | ||
) | ||
|
||
|
||
def heartbeat(identifier: str) -> Event: | ||
return Event( | ||
identifier=identifier, timestamp=int(time()), check_in_type="heartbeat" | ||
) | ||
|
||
|
||
def is_redundant(event: Event, existing_event: Event) -> bool: | ||
if ( | ||
event["check_in_type"] not in ["cron", "heartbeat"] | ||
or event["check_in_type"] != existing_event["check_in_type"] | ||
or event["identifier"] != existing_event["identifier"] | ||
): | ||
return False | ||
|
||
if self["check_in_type"] == "cron" and ( | ||
self["digest"] != other["digest"] or | ||
self["kind"] != other["kind"] | ||
): | ||
if event["check_in_type"] == "cron" and ( # noqa: SIM103 | ||
event.get("digest") != existing_event.get("digest") | ||
or event.get("kind") != existing_event.get("kind") | ||
): | ||
return False | ||
|
||
return True | ||
|
||
@classmethod | ||
def describe(cls, events: List[Self]) -> str: | ||
if not events: | ||
return True | ||
|
||
|
||
def describe(events: list[Event]) -> str: | ||
if not events: | ||
# This shouldn't happen. | ||
return "no check-in events" | ||
elif len(events) > 1: | ||
if len(events) > 1: | ||
return f"{len(events)} check-in events" | ||
else: | ||
event = events[0] | ||
if event["check_in_type"] == "cron": | ||
return ( | ||
|
||
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" | ||
) | ||
if event["check_in_type"] == "heartbeat": | ||
return f"heartbeat check-in `{event.get('identifier', 'unknown')}` event" | ||
|
||
# This shouldn't happen. | ||
return "unknown check-in event" # type: ignore[unreachable] |
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,17 +1,56 @@ | ||
from __future__ import annotations | ||
|
||
from threading import Event, Thread | ||
|
||
from .event import heartbeat as heartbeat_event | ||
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) | ||
_HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS = 30.0 | ||
|
||
|
||
def _heartbeat_continuous_interval_seconds() -> float: | ||
return _HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS | ||
|
||
|
||
def _set_heartbeat_continuous_interval_seconds(seconds: float) -> None: | ||
global _HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS | ||
_HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS = seconds | ||
|
||
|
||
def _reset_heartbeat_continuous_interval_seconds() -> None: | ||
global _HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS | ||
_HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS = 30.0 | ||
|
||
|
||
_started_continuous_heartbeats: list[tuple[Event, Thread]] = [] | ||
|
||
|
||
def _kill_continuous_heartbeats() -> None: | ||
for event, thread in _started_continuous_heartbeats: | ||
event.set() | ||
thread.join() | ||
|
||
_started_continuous_heartbeats.clear() | ||
|
||
|
||
def _start_continuous_heartbeat(name: str) -> None: | ||
kill = Event() | ||
|
||
def _run_continuous_heartbeat() -> None: | ||
while True: | ||
if kill.wait(_heartbeat_continuous_interval_seconds()): | ||
break | ||
|
||
heartbeat(name) | ||
|
||
thread = Thread(target=_run_continuous_heartbeat) | ||
thread.start() | ||
_started_continuous_heartbeats.append((kill, thread)) | ||
|
||
|
||
def heartbeat(name: str, continuous: bool = False) -> None: | ||
if continuous: | ||
thread = Thread(target=_continuous_heartbeat, args=(name,)) | ||
thread.start() | ||
scheduler.schedule(Event.heartbeat(name)) | ||
_start_continuous_heartbeat(name) | ||
|
||
scheduler().schedule(heartbeat_event(name)) |
Oops, something went wrong.