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

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_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
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement