thojanssens1

thojanssens1

Error: could not lookup MyApp.Repo because it was not started or it does not exist

Hello,

I want to start a module (MyApp.MyAgent) using Agent that will fetch some data from DB (and thus use MyApp.Repo) and keep it in memory, and I want the data to be available when the application starts. So I add it as a child to the superviser but the compiler complains that MyApp.Repo has not yet been started when aliasing it in MyApp.MyAgent:

def start(_type, _args) do
  # List all child processes to be supervised
  children = [
    # Start the Ecto repository
    MyApp.Repo,
    # Start the endpoint when the application starts
    MyAppWeb.Endpoint,
    # Starts a worker by calling: MyApp.Worker.start_link(arg)
    # {MyApp.Worker, arg},
    MyApp.MyAgent.start_link()
  ]
defmodule MyApp.MyAgent do
  import Ecto.Query, warn: false
  alias MyApp.Repo
  alias MyApp.SomeContext.SomeSchema

  def start_link do
    ids = Repo.all(from s in SomeSchema, select: s.id)

    Agent.start_link(fn -> ids end, name: __MODULE__)
  end

  def all do
    Agent.get(__MODULE__, & &1)
  end
end

Thank you for any help!

Marked As Solved

LostKobrakai

LostKobrakai

Calling start_link is effectively a “start this now” and it returns the pid of the started process, while childspecs are just data, which tells the supervisor how to start children, when it’s in the place to start a certain child at a later time, e.g. when the supervisor itself starts or when a child is restarted.

Also Liked

NobbZ

NobbZ

Do not call start link, but provide a childspec as in the already existing items.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement