Skip to content

Commit

Permalink
update scheduler to request less often big intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Aug 22, 2024
1 parent d2998fc commit 3844c11
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
3 changes: 0 additions & 3 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
39 changes: 16 additions & 23 deletions lib/archethic_fas/quotes/history/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3844c11

Please sign in to comment.