-
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.
Merge pull request #227 from appsignal/implement-scheduler-and-heartb…
…eat-checkins Implement scheduler and heartbeat checkins
- Loading branch information
Showing
21 changed files
with
1,171 additions
and
439 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 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,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from __future__ import annotations | ||
|
||
from binascii import hexlify | ||
from os import urandom | ||
from typing import Any, Callable, Literal, TypeVar | ||
|
||
from .event import cron as cron_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(cron_event(self.identifier, self.digest, "start")) | ||
|
||
def finish(self) -> None: | ||
scheduler().schedule(cron_event(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 |
Oops, something went wrong.