-
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
19 changed files
with
877 additions
and
458 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
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,17 +1,48 @@ | ||
from .scheduler import scheduler | ||
from .event import Event | ||
from threading import Thread | ||
from time import sleep | ||
from .event import heartbeat as heartbeat_event | ||
from threading import Thread, Event | ||
|
||
_HEARTBEAT_CONTINUOUS_INTERVAL_SECONDS = 30 | ||
from typing import List, Tuple | ||
|
||
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)) | ||
print("schedule is ", _heartbeat_continuous_interval_seconds()) | ||
_start_continuous_heartbeat(name) | ||
|
||
scheduler().schedule(heartbeat_event(name)) |
Oops, something went wrong.