-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add update queues for SCUs #747
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,30 @@ | ||
defmodule PaEss.ScuQueue do | ||
use GenStage | ||
|
||
def start_link(id) do | ||
GenStage.start_link(__MODULE__, nil, name: stage_name(id)) | ||
end | ||
|
||
def enqueue_message(id, message) do | ||
GenStage.call(stage_name(id), {:enqueue_message, message}) | ||
end | ||
|
||
@impl true | ||
def init(_) do | ||
{:producer, %{}} | ||
end | ||
|
||
@impl true | ||
def handle_call({:enqueue_message, message}, _from, state) do | ||
{:reply, :ok, [message], state} | ||
end | ||
|
||
@impl true | ||
def handle_demand(_demand, state) do | ||
{:noreply, [], state} | ||
end | ||
|
||
def stage_name(id) do | ||
:"ScuQueue/#{id}" | ||
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,92 @@ | ||
defmodule PaEss.ScuUpdater do | ||
use GenStage | ||
require Logger | ||
|
||
def start_link(id) do | ||
GenStage.start_link(__MODULE__, id, name: :"ScuUpdater/#{id}") | ||
end | ||
|
||
@impl true | ||
def init(id) do | ||
{:consumer, %{}, subscribe_to: [{PaEss.ScuQueue.stage_name(id), []}]} | ||
end | ||
|
||
@impl true | ||
def handle_events([{:message, scu_id, payload}], _from, state) do | ||
body = Jason.encode!(payload) | ||
|
||
if send_to_scu(scu_id, "/message", body) == :ok do | ||
send_to_signs_ui(scu_id, "/message", body) | ||
end | ||
|
||
{:noreply, [], state} | ||
end | ||
|
||
def handle_events([{:background, scu_id, payload}], _from, state) do | ||
body = Jason.encode!(payload) | ||
|
||
if send_to_scu(scu_id, "/background", body) == :ok do | ||
send_to_signs_ui(scu_id, "/background", body) | ||
end | ||
|
||
{:noreply, [], state} | ||
end | ||
|
||
defp send_to_scu(scu_id, path, body) do | ||
http_poster = Application.get_env(:realtime_signs, :http_poster_mod) | ||
scu_ip_map = Application.get_env(:realtime_signs, :scu_ip_map) | ||
scully_api_key = Application.get_env(:realtime_signs, :scully_api_key) | ||
|
||
if scu_ip_map do | ||
http_poster.post( | ||
"https://#{Map.fetch!(scu_ip_map, scu_id)}#{path}", | ||
body, | ||
[{"Content-type", "application/json"}, {"x-api-key", scully_api_key}], | ||
hackney: [pool: :arinc_pool] | ||
) | ||
|> case do | ||
{:ok, %HTTPoison.Response{status_code: status}} when status in 200..299 -> | ||
:ok | ||
|
||
{:ok, %HTTPoison.Response{status_code: status}} -> | ||
Logger.warn("scu_error: status=#{inspect(status)}") | ||
:error | ||
|
||
{:error, %HTTPoison.Error{reason: reason}} -> | ||
Logger.warn("scu_error: #{inspect(reason)}") | ||
:error | ||
end | ||
else | ||
:ok | ||
end | ||
end | ||
|
||
defp send_to_signs_ui(scu_id, path, body) do | ||
http_poster = Application.get_env(:realtime_signs, :http_poster_mod) | ||
sign_ui_url = Application.get_env(:realtime_signs, :sign_ui_url) | ||
sign_ui_api_key = Application.get_env(:realtime_signs, :sign_ui_api_key) | ||
|
||
if sign_ui_url do | ||
http_poster.post( | ||
"https://#{sign_ui_url}#{path}", | ||
body, | ||
[ | ||
{"Content-type", "application/json"}, | ||
{"x-api-key", sign_ui_api_key}, | ||
{"x-scu-id", scu_id} | ||
], | ||
hackney: [pool: :arinc_pool] | ||
) | ||
|> case do | ||
{:ok, %HTTPoison.Response{status_code: status}} when status in 200..299 -> | ||
nil | ||
|
||
{:ok, %HTTPoison.Response{status_code: status}} -> | ||
Logger.warn("signs_ui_error: status=#{inspect(status)}") | ||
|
||
{:error, %HTTPoison.Error{reason: reason}} -> | ||
Logger.warn("signs_ui_error: #{inspect(reason)}") | ||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, why do we need to send the scu_id to Signs UI as a header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This endpoint doesn't exist yet, but Signs UI will need this in order to attribute the data to the right sign, since the payload itself is not sufficient to differentiate it globally.