exchange_rates_retrieve_every: 3_600_000_000 is being ignored.
Closed this issue · 8 comments
I've got this configuration on runtime.exs
config :ex_money,
exchange_rates_retrieve_every: 3_600_000_000,
api_module: Money.ExchangeRates.OpenExchangeRates,
exchange_rates_cache_module: Money.ExchangeRates.Cache.Ets,
open_exchange_rates_app_id: System.get_env("OPEN_EXCHANGE"),
json_library: Jason,
default_cldr_backend: Hermes.Cldr
And still the exchange_rates_retrieve_every is being ignored and in what's gone already of this month i've retrieve exchange rates 1500 times. Care to explain what am I doing wrong or why is this happening?
Thats definitely unexpected, 3_600_000_000
milliseconds is about 41 days so I would have expected one retrieval so far. Is that your expectation?
May I ask you to add three more configuration lines?
log_failure: :debug,
log_info: :debug,
log_success: :debug,
With that configuration you should see a message that says something like:
23:44:48.375 [debug] Exchange Rates will be retrieved now and then every 3600000 seconds.
when you start up the retriever.
If so then at least we know the configuration is being respected. If not, then it's something else which I will investigate in parallel.
I changed it to 86400 seconds (a day) and added what you said and I see:
[debug] Exchange Rates will be retrieved now and then every 86400 seconds
when running
iex -S mix phx.server
But I don't see that message when running "mix test" which I have with mix test.watch btw.
Is it possible that every time the tests run is re-loading again the exchange rates from scratch?
Is it possible that every time the tests run is re-loading again the exchange rates from scratch?
mix test
will initialise the configuration and if you are auto-starting the exchange rates retriever on application start (which is the default behaviour) then yes, you will see a retrieval sequence start each time tests are run.
Perhaps adding:
auto_start_exchange_rate_service: Mix.env() != :test,
to your configuration would mean that the retriever won't start automatically when in the :test
environment. Then when you need to test exchange rates retrieval you can either start the service yourself with Money.ExchangeRates.Retriever.start()
or just call the Open Exchange Rates retriever directly in a test with something like:
config = Money.ExchangeRates.OpenExchangeRates.init(Money.ExchangeRates.default_config())
config = Map.put(config, :log_levels, %{failure: nil, info: nil, success: nil})
assert Money.ExchangeRates.OpenExchangeRates.get_latest_rates(config)
Which is basically what I do in this test.
To be clear on the sequence of events:
- When the exchange rates retriever starts, it immediately requests retireval of exchange rates
- And then it schedules the next retrieval according to the configured retrieval interval in milliseconds.
It works that way on the basis that exchange rates should always be available as quickly as possible, and then retrieved according to the configured schedule.
So I'm guessing then my 1500 times requesting is basically every time mix test.watch has restarted O_o
Is there a way to prevent that from happening, for tests ?
Yes, thats the configuration change I suggested above:
auto_start_exchange_rate_service: Mix.env() != :test,
Aw, sorry I didn't notice you posted two messages and only read the bottom one. Now resting but will try tomorrow and come back with the results. Thank you very much.
Closing for now since I think we've resolved this issue. Please reopen if not.