-
Notifications
You must be signed in to change notification settings - Fork 1
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 #53 from mbta/ph-off-switch
feat: "Off switch" config in S3
- Loading branch information
Showing
6 changed files
with
120 additions
and
3 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
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,31 @@ | ||
defmodule Screens.Override.Fetch do | ||
@moduledoc false | ||
|
||
@default_opts [timeout: 2000, recv_timeout: 2000] | ||
|
||
def fetch_config_from_s3(opts \\ []) do | ||
url = "https://mbta-dotcom.s3.amazonaws.com/screens/config/" <> config_path_for_environment() | ||
headers = [] | ||
|
||
with {:ok, response} <- HTTPoison.get(url, headers, Keyword.merge(@default_opts, opts)), | ||
%{status_code: 200, body: body} <- response, | ||
{:ok, parsed} <- Jason.decode(body), | ||
%{"disabled_screen_ids" => disabled_screen_ids, "globally_disabled" => globally_disabled} <- | ||
parsed do | ||
{:ok, | ||
%{ | ||
globally_disabled: globally_disabled, | ||
disabled_screen_ids: MapSet.new(disabled_screen_ids) | ||
}} | ||
else | ||
_ -> :error | ||
end | ||
end | ||
|
||
defp config_path_for_environment do | ||
case Application.get_env(:screens, :environment_name) do | ||
"prod" -> "prod.json" | ||
_ -> "dev.json" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule Screens.Override.State do | ||
@moduledoc false | ||
|
||
@initial_fetch_wait_ms 500 | ||
@refresh_ms 15 * 1000 | ||
@default_config %{globally_disabled: false, disabled_screen_ids: MapSet.new()} | ||
|
||
use GenServer | ||
|
||
def start_link(opts \\ []) do | ||
GenServer.start_link(__MODULE__, :ok, opts) | ||
end | ||
|
||
def lookup(pid \\ __MODULE__, screen_id) do | ||
GenServer.call(pid, {:lookup, screen_id}) | ||
end | ||
|
||
def schedule_refresh(pid, ms \\ @refresh_ms) do | ||
Process.send_after(pid, :refresh, ms) | ||
:ok | ||
end | ||
|
||
### | ||
|
||
@impl true | ||
def init(:ok) do | ||
schedule_refresh(self(), @initial_fetch_wait_ms) | ||
{:ok, @default_config} | ||
end | ||
|
||
@impl true | ||
def handle_call({:lookup, _screen_id}, _from, %{globally_disabled: true} = state) do | ||
{:reply, true, state} | ||
end | ||
|
||
def handle_call( | ||
{:lookup, screen_id}, | ||
_from, | ||
%{globally_disabled: false, disabled_screen_ids: disabled_screen_ids} = state | ||
) do | ||
{:reply, MapSet.member?(disabled_screen_ids, screen_id), state} | ||
end | ||
|
||
@impl true | ||
def handle_info(:refresh, _state) do | ||
new_state = | ||
case Screens.Override.Fetch.fetch_config_from_s3() do | ||
{:ok, config} -> config | ||
:error -> @default_config | ||
end | ||
|
||
schedule_refresh(self()) | ||
{:noreply, new_state} | ||
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,17 @@ | ||
defmodule Screens.Override.Supervisor do | ||
@moduledoc false | ||
|
||
use Supervisor | ||
|
||
def start_link([]) do | ||
Supervisor.start_link(__MODULE__, :ok, name: __MODULE__) | ||
end | ||
|
||
def init(:ok) do | ||
children = [ | ||
worker(Screens.Override.State, [[name: Screens.Override.State]]) | ||
] | ||
|
||
supervise(children, strategy: :one_for_one) | ||
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
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