-
Notifications
You must be signed in to change notification settings - Fork 82
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 #960 from appsignal/check-in-scheduler
Check-in scheduler
- Loading branch information
Showing
22 changed files
with
763 additions
and
172 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,6 @@ | ||
--- | ||
bump: patch | ||
type: change | ||
--- | ||
|
||
Send check-ins concurrently. When calling `Appsignal.CheckIn.cron`, instead of blocking the current process while the check-in events are sent, schedule them to be sent in a separate process. |
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,19 @@ | ||
defmodule Appsignal.CheckIn do | ||
alias Appsignal.CheckIn.Cron | ||
|
||
@spec cron(String.t()) :: :ok | ||
def cron(identifier) do | ||
Cron.finish(Cron.new(identifier)) | ||
end | ||
|
||
@spec cron(String.t(), (-> out)) :: out when out: var | ||
def cron(identifier, fun) do | ||
cron = Cron.new(identifier) | ||
|
||
Cron.start(cron) | ||
output = fun.() | ||
Cron.finish(cron) | ||
|
||
output | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Appsignal.CheckIn.Cron do | ||
alias __MODULE__ | ||
alias Appsignal.CheckIn.Cron.Event | ||
|
||
@scheduler Application.compile_env( | ||
:appsignal, | ||
:appsignal_checkin_scheduler, | ||
Appsignal.CheckIn.Scheduler | ||
) | ||
@type t :: %Cron{identifier: String.t(), digest: String.t()} | ||
|
||
defstruct [:identifier, :digest] | ||
|
||
@spec new(String.t()) :: t | ||
def new(identifier) do | ||
%Cron{ | ||
identifier: identifier, | ||
digest: random_digest() | ||
} | ||
end | ||
|
||
defp random_digest do | ||
Base.encode16(:crypto.strong_rand_bytes(8), case: :lower) | ||
end | ||
|
||
@spec start(Cron.t()) :: :ok | ||
def start(cron) do | ||
@scheduler.schedule(Event.new(cron, :start)) | ||
end | ||
|
||
@spec finish(Cron.t()) :: :ok | ||
def finish(cron) do | ||
@scheduler.schedule(Event.new(cron, :finish)) | ||
end | ||
end | ||
|
||
defmodule Appsignal.CheckIn.Cron.Event do | ||
alias __MODULE__ | ||
alias Appsignal.CheckIn.Cron | ||
|
||
@derive Jason.Encoder | ||
|
||
@type kind :: :start | :finish | ||
@type t :: %Event{ | ||
identifier: String.t(), | ||
digest: String.t(), | ||
kind: kind, | ||
timestamp: integer, | ||
check_in_type: :cron | ||
} | ||
|
||
defstruct [:identifier, :digest, :kind, :timestamp, :check_in_type] | ||
|
||
@spec new(Cron.t(), kind) :: t | ||
def new(%Cron{identifier: identifier, digest: digest}, kind) do | ||
%Event{ | ||
identifier: identifier, | ||
digest: digest, | ||
kind: kind, | ||
timestamp: System.system_time(:second), | ||
check_in_type: :cron | ||
} | ||
end | ||
end |
Oops, something went wrong.