Initialize Etso Repo at startup (no suitable "init" callback)

I have a use case where I want to load a bunch of JSON files into memory so that they can be easily accessed later on. Ideally the API to access them would utilize Ecto, since that library has such a great API.

Looking around, I have a pretty good idea on how to do this with a basic GenServer. On init I would load the data in, store it in :persistent_term, and then be able to easily access the data via basic GenServer client API.

However, when using Etso & Ecto, I can’t find a similar init functionality to hook into to set things up when the process is starting up. The clearest candidate is the Ecto.Repo.init/2 callback, but that does not work as it is called before the supervisor tree of the Repo actually “starts up”.

So where would be the best way to shim this funtionality into etso, or an Ecto.Repo generally? I’m about to give up and just do a GenServer approach.

Cheers!

Edit with more details:
Looking further into it, it seems that I want this load-data-task run after the etso supervised children start up, as they seem to be defined here. But when I start to explore this path via overriding the Ecto.Repo.start_link/1 callback, I get an error about how the function is already defined. If that did work, is the idiomatic way to add a Task to that supervision tree that does the data loading?

FWIW, here is the logs I get when trying to hook into start_link:

this clause for start_link/1 cannot match because a previous clause at line 2 always matche

where line 2 corresponding to the use Ecto.Repo statement in my repo.

The Utilisation section of the README on the Etso GitHub repo has a link to a project (Erlef Website) where Etso is being used to import markdown files on application start.

After having a quick look at that repo, it looks to me like you could copy & modify what is being done there to import your JSON files.

2 Likes