From 3844c11f16a0910834ee6846f7c73e5883da3b46 Mon Sep 17 00:00:00 2001 From: bchamagne Date: Thu, 22 Aug 2024 11:36:15 +0200 Subject: [PATCH] update scheduler to request less often big intervals --- config/config.exs | 3 -- lib/archethic_fas/quotes/history/scheduler.ex | 39 ++++++++----------- 2 files changed, 16 insertions(+), 26 deletions(-) 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