-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an `Appsignal::CheckIn.heartbeat` helper that emits a single heartbeat for the check-in identifier given. When given `continuous: true` as the second argument, it spawns a separate thread that emits a heartbeat every thirty seconds. This is a convenience method for the use case where the heartbeat is only meant as a check that the process is alive. Split functions that deal with different event kinds out of the scheduler and test them independently.
- Loading branch information
Showing
8 changed files
with
463 additions
and
40 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::CheckIn.heartbeat` method to send a single heartbeat check-in event from your application. This can be used, for example, in your application's main loop: | ||
|
||
```ruby | ||
loop do | ||
Appsignal::CheckIn.heartbeat("job_processor") | ||
process_job | ||
end | ||
``` | ||
|
||
Heartbeats are deduplicated and sent asynchronously, without blocking the current thread. Regardless of how often the `.heartbeat` method 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: | ||
|
||
```ruby | ||
def main | ||
start_app | ||
Appsignal::CheckIn.heartbeat("my_app", continuous: true) | ||
end | ||
``` |
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,72 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
module CheckIn | ||
# @api private | ||
class Event | ||
class << self | ||
def new(check_in_type:, identifier:, digest: nil, kind: nil) | ||
{ | ||
:identifier => identifier, | ||
:digest => digest, | ||
:kind => kind, | ||
:timestamp => Time.now.utc.to_i, | ||
:check_in_type => check_in_type | ||
}.compact | ||
end | ||
|
||
def cron(identifier:, digest:, kind:) | ||
new( | ||
:check_in_type => "cron", | ||
:identifier => identifier, | ||
:digest => digest, | ||
:kind => kind | ||
) | ||
end | ||
|
||
def heartbeat(identifier:) | ||
new( | ||
:check_in_type => "heartbeat", | ||
:identifier => identifier | ||
) | ||
end | ||
|
||
def redundant?(event, other) | ||
return false if | ||
other[:check_in_type] != event[:check_in_type] || | ||
other[:identifier] != event[:identifier] | ||
|
||
return false if event[:check_in_type] == "cron" && ( | ||
other[:digest] != event[:digest] || | ||
other[:kind] != event[:kind] | ||
) | ||
|
||
return false if | ||
event[:check_in_type] != "cron" && | ||
event[:check_in_type] != "heartbeat" | ||
|
||
true | ||
end | ||
|
||
def describe(events) | ||
if events.empty? | ||
# This shouldn't happen. | ||
"no check-in events" | ||
elsif events.length > 1 | ||
"#{events.length} check-in events" | ||
else | ||
event = events.first | ||
if event[:check_in_type] == "cron" | ||
"cron check-in `#{event[:identifier] || "unknown"}` " \ | ||
"#{event[:kind] || "unknown"} event (digest #{event[:digest] || "unknown"})" | ||
elsif event[:check_in_type] == "heartbeat" | ||
"heartbeat check-in `#{event[:identifier] || "unknown"}` event" | ||
else | ||
"unknown check-in event" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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
Oops, something went wrong.