diff --git a/config/config.exs b/config/config.exs index 2a8cda0..0994846 100644 --- a/config/config.exs +++ b/config/config.exs @@ -27,7 +27,4 @@ config :archethic_fas, config :archethic_fas, ArchethicFAS.QuotesLatest.Scheduler, schedule_interval: :timer.minutes(5) -config :archethic_fas, ArchethicFAS.QuotesHistorical.Scheduler, - schedule_interval: :timer.minutes(1) - import_config("#{Mix.env()}.exs") diff --git a/lib/archethic_fas/quotes/history/scheduler.ex b/lib/archethic_fas/quotes/history/scheduler.ex index 4dde19c..b6b23d3 100644 --- a/lib/archethic_fas/quotes/history/scheduler.ex +++ b/lib/archethic_fas/quotes/history/scheduler.ex @@ -15,36 +15,29 @@ defmodule ArchethicFAS.QuotesHistorical.Scheduler do end def init(_opts) do - [next | remaining] = Interval.list() - send(self(), {:tick, next}) - {:ok, %{remaining: remaining}} - end + # to avoid API restriction we do one operation per minute + :timer.send_interval(:timer.minutes(1), self(), :tick) + :timer.send_interval(:timer.hours(1), self(), :tick_hour) + :timer.send_interval(:timer.hours(24), self(), :tick_day) - def handle_info({:tick, interval}, state = %{remaining: []}) do - [next | remaining] = Interval.list() + {:ok, %{remaining: Interval.list()}} + end - Process.send_after( - self(), - {:tick, next}, - Application.fetch_env!(:archethic_fas, __MODULE__) |> Keyword.fetch!(:schedule_interval) - ) + def handle_info(:tick, state = %{remaining: []}) do + {:noreply, state} + end + def handle_info(:tick, %{remaining: [interval | rest]}) do hydrate(interval) - - {:noreply, %{state | remaining: remaining}} + {:noreply, %{remaining: rest}} end - def handle_info({:tick, interval}, state = %{remaining: [next | remaining]}) do - Process.send_after( - self(), - {:tick, next}, - Application.fetch_env!(:archethic_fas, __MODULE__) - |> Keyword.fetch!(:schedule_interval) - ) - - hydrate(interval) + def handle_info(:tick_hour, %{remaining: remaining}) do + {:noreply, %{remaining: [:hourly, :daily | remaining]}} + end - {:noreply, %{state | remaining: remaining}} + def handle_info(:tick_day, %{remaining: remaining}) do + {:noreply, %{remaining: [:weekly, :biweekly, :monthly, :bimonthly, :yearly | remaining]}} end defp hydrate(interval) do