Skip to content

Commit 7eaba05

Browse files
committed
Enable heartbeat on application start
1 parent c9ad8fb commit 7eaba05

File tree

11 files changed

+91
-33
lines changed

11 files changed

+91
-33
lines changed

freezer_eye/config/test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
use Mix.Config
2+
3+
config :freezer_eye,
4+
enable_heartbeat_on_startup: true
5+
6+
config :fe_reporting, :adafruit_io,
7+
username: "foo",
8+
secret_key: "bar",
9+
heartbeat_feed: "test-feed"

freezer_eye/lib/freezer_eye/config.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ defmodule FreezerEye.Config do
55

66
@behaviour __MODULE__.Impl
77

8-
defstruct [:heartbeat_interval]
8+
defstruct [:heartbeat_interval, :enable_heartbeat_on_startup?]
99

1010
@type t :: %__MODULE__{
11-
heartbeat_interval: pos_integer()
11+
heartbeat_interval: pos_integer(),
12+
enable_heartbeat_on_startup?: boolean
1213
}
1314

1415
@impl true

freezer_eye/lib/freezer_eye/config/env_impl.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ defmodule FreezerEye.Config.EnvImpl do
99
@spec fetch!() :: Config.t() | none
1010
def fetch! do
1111
%Config{
12-
heartbeat_interval: Application.fetch_env!(:freezer_eye, :heartbeat_interval)
12+
heartbeat_interval: Application.fetch_env!(:freezer_eye, :heartbeat_interval),
13+
enable_heartbeat_on_startup?:
14+
Application.get_env(:freezer_eye, :enable_heartbeat_on_startup, true)
1315
}
1416
end
1517
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule FreezerEye.DefaultImpl.Manager do
2+
@moduledoc false
3+
use GenServer
4+
5+
alias FreezerEye.Config
6+
alias FreezerEye.DefaultImpl.Heartbeats
7+
8+
def start_link(opts) when is_list(opts) do
9+
GenServer.start_link(__MODULE__, opts)
10+
end
11+
12+
@impl true
13+
def init(_init_opts) do
14+
{:ok, [], {:continue, :start}}
15+
end
16+
17+
@impl true
18+
def handle_continue(:start, state) do
19+
%Config{enable_heartbeat_on_startup?: enable_heartbeat_on_startup?} = Config.fetch!()
20+
21+
if enable_heartbeat_on_startup? do
22+
Heartbeats.start_default()
23+
end
24+
25+
{:noreply, state}
26+
end
27+
end

freezer_eye/lib/freezer_eye/default_impl/supervisor.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule FreezerEye.DefaultImpl.Supervisor do
33
use Supervisor
44

55
alias FreezerEye.DefaultImpl.Heartbeats
6+
alias FreezerEye.DefaultImpl.Manager
67

78
def start_link(opts) when is_list(opts) do
89
Supervisor.start_link(__MODULE__, opts, name: __MODULE__)
@@ -11,7 +12,8 @@ defmodule FreezerEye.DefaultImpl.Supervisor do
1112
@impl true
1213
def init(_init_opts) do
1314
children = [
14-
Heartbeats
15+
Heartbeats,
16+
Manager
1517
]
1618

1719
Supervisor.init(children, strategy: :one_for_one)

freezer_eye/test/freezer_eye/config/env_impl_test.exs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ defmodule FreezerEye.Config.EnvImplTest do
1111

1212
property "fetch!/0 returns a config struct with the proper values" do
1313
config_value_generators = [
14-
heartbeat_interval: positive_integer()
14+
heartbeat_interval: positive_integer(),
15+
enable_heartbeat_on_startup: boolean()
1516
]
1617

1718
check all config <-
@@ -28,7 +29,9 @@ defmodule FreezerEye.Config.EnvImplTest do
2829

2930
if has_required_keys?(config, [:heartbeat_interval]) do
3031
assert %Config{
31-
heartbeat_interval: Keyword.fetch!(config, :heartbeat_interval)
32+
heartbeat_interval: Keyword.fetch!(config, :heartbeat_interval),
33+
enable_heartbeat_on_startup?:
34+
Keyword.get(config, :enable_heartbeat_on_startup, true)
3235
} == EnvImpl.fetch!()
3336
else
3437
assert_raise ArgumentError, fn ->

integration_tester/config/config.exs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,4 @@
22
# and its dependencies with the aid of the Mix.Config module.
33
use Mix.Config
44

5-
# This configuration is loaded before any dependency and is restricted
6-
# to this project. If another project depends on this project, this
7-
# file won't be loaded nor affect the parent project. For this reason,
8-
# if you want to provide default values for your application for
9-
# third-party users, it should be done in your "mix.exs" file.
10-
11-
# You can configure your application as:
12-
#
13-
# config :integration_tester, key: :value
14-
#
15-
# and access this configuration in your application as:
16-
#
17-
# Application.get_env(:integration_tester, :key)
18-
#
19-
# You can also configure a third-party app:
20-
#
21-
# config :logger, level: :info
22-
#
23-
24-
# It is also possible to import configuration files, relative to this
25-
# directory. For example, you can emulate configuration per environment
26-
# by uncommenting the line below and defining dev.exs, test.exs and such.
27-
# Configuration from the imported file will override the ones defined
28-
# here (which is why it is important to import them last).
29-
#
30-
# import_config "#{Mix.env()}.exs"
5+
import_config "#{Mix.env()}.exs"

integration_tester/config/dev.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use Mix.Config

integration_tester/config/prod.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use Mix.Config

integration_tester/config/test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use Mix.Config
2+
3+
config :freezer_eye,
4+
heartbeat_interval: 200,
5+
enable_heartbeat_on_startup: false

0 commit comments

Comments
 (0)