Skip to content

Commit

Permalink
Add operators data periodic fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Ventura committed Oct 2, 2024
1 parent 7138c28 commit 365616b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions telemetry_api/.env.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ALIGNED_CONFIG_FILE="../contracts/script/output/devnet/alignedlayer_deployment_output.json"
OPERATOR_FETCHER_WAIT_TIME_MS=5000
ENVIRONMENT=devnet
1 change: 1 addition & 0 deletions telemetry_api/ecto_setup_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ source .env
env_vars=(
"ENVIRONMENT"
"ALIGNED_CONFIG_FILE"
"OPERATOR_FETCHER_WAIT_TIME_MS"
)

for var in "${env_vars[@]}"; do
Expand Down
11 changes: 4 additions & 7 deletions telemetry_api/lib/telemetry_api/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ defmodule TelemetryApi.Application do
# Start a worker by calling: TelemetryApi.Worker.start_link(arg)
# {TelemetryApi.Worker, arg},
# Start to serve requests, typically the last entry
TelemetryApiWeb.Endpoint
TelemetryApiWeb.Endpoint,
TelemetryApi.Periodic.OperatorFetcher
]

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: TelemetryApi.Supervisor]

# Now we fetch operators data from smart contract to fill db
with {:ok, pid} <- Supervisor.start_link(children, opts),
{:ok, _} <- TelemetryApi.Operators.fetch_all_operators() do
{:ok, pid}
end

Supervisor.start_link(children, opts)
end

# Tell Phoenix to update the endpoint configuration
Expand Down
32 changes: 32 additions & 0 deletions telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule TelemetryApi.Periodic.OperatorFetcher do
use Task
alias TelemetryApi.Operators

wait_time_str = System.get_env("OPERATOR_FETCHER_WAIT_TIME_MS") ||
raise """
environment variable OPERATOR_FETCHER_WAIT_TIME_MS is missing.
"""

@wait_time_ms (
case Integer.parse(wait_time_str) do
:error -> raise("OPERATOR_FETCHER_WAIT_TIME_MS is not a number")
{num, _} -> num
end
)

def start_link(_) do
Task.start_link(&poll_serivce/0)
end

defp poll_serivce() do
receive do
after
@wait_time_ms ->
case Operators.fetch_all_operators() do
{:ok, _} -> :ok
{:error, message} -> IO.inspect "Couldn't fetch operators: #{IO.inspect message}"
end
poll_serivce()
end
end
end
1 change: 1 addition & 0 deletions telemetry_api/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ source .env
env_vars=(
"ENVIRONMENT"
"ALIGNED_CONFIG_FILE"
"OPERATOR_FETCHER_WAIT_TIME_MS"
)

for var in "${env_vars[@]}"; do
Expand Down

0 comments on commit 365616b

Please sign in to comment.