diff --git a/telemetry_api/.env.dev b/telemetry_api/.env.dev index b2a7e87be..1a33ddc8e 100644 --- a/telemetry_api/.env.dev +++ b/telemetry_api/.env.dev @@ -1,2 +1,3 @@ ALIGNED_CONFIG_FILE="../contracts/script/output/devnet/alignedlayer_deployment_output.json" +OPERATOR_FETCHER_WAIT_TIME_MS=5000 ENVIRONMENT=devnet diff --git a/telemetry_api/ecto_setup_db.sh b/telemetry_api/ecto_setup_db.sh index 4a225ed24..aebd647f0 100755 --- a/telemetry_api/ecto_setup_db.sh +++ b/telemetry_api/ecto_setup_db.sh @@ -6,6 +6,7 @@ source .env env_vars=( "ENVIRONMENT" "ALIGNED_CONFIG_FILE" + "OPERATOR_FETCHER_WAIT_TIME_MS" ) for var in "${env_vars[@]}"; do diff --git a/telemetry_api/lib/telemetry_api/application.ex b/telemetry_api/lib/telemetry_api/application.ex index 6d62b0dea..d55bb3cf0 100644 --- a/telemetry_api/lib/telemetry_api/application.ex +++ b/telemetry_api/lib/telemetry_api/application.ex @@ -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 diff --git a/telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex b/telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex new file mode 100644 index 000000000..e198e548a --- /dev/null +++ b/telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex @@ -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 diff --git a/telemetry_api/start.sh b/telemetry_api/start.sh index b20a0bfae..41148952d 100755 --- a/telemetry_api/start.sh +++ b/telemetry_api/start.sh @@ -6,6 +6,7 @@ source .env env_vars=( "ENVIRONMENT" "ALIGNED_CONFIG_FILE" + "OPERATOR_FETCHER_WAIT_TIME_MS" ) for var in "${env_vars[@]}"; do