How to add more than one worker in phoenix app?

I want to use MongoDB and ConCaache worker in my app, but when I tried the following

def start(_type, _args) do
    import Supervisor.Spec
    # List all child processes to be supervised
    children = [
      # Start the endpoint when the application starts
      supervisor(MyAppWeb.Endpoint, []),
      # Starts a worker by calling: MyAppWeb.Worker.start_link(arg)
      # {MyAppWeb.Worker, arg},
      worker(Mongo, [[database:
        Application.get_env(:mong, :db)[:name], name: :mongo, pool_size: 100]]),
      worker(ConCache, [name: :my_cache, ttl_check_interval: false])
    ]

I get the following error when running

[info] Application my_app exited: exited in: MyApp.Application.start(:normal, [])
    ** (EXIT) exited in: GenServer.call(:mongo, :topology, 5000)
        ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its
application isn't started
** (Mix) Could not start application my_app: exited in MyApp.Application.start(:normal, [])
    ** (EXIT) exited in: GenServer.call(:mongo, :topology, 5000)
        ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its
application isn't started

What Could I do?

This is an old syntax for supervision tree. Which version of Phoenix and Elixir do You use?

Elixir 1.10.2

Phoenix v1.4.14

There is nothing obviously incorrect with the code example you have shared. Would you be able to share a minimal repoduction of the project so that we can run and debug the problem? Thanks

An expanded view of that file might be good too. Are you trying to call Mongo after you define the children within the start function?

I have changed the code to this and I don’t know why but it worked perfectly

def start(_type, _args) do
    import Supervisor.Spec
    # List all child processes to be supervised
    children = [
      # Start the endpoint when the application starts
      supervisor(MyAppWeb.Endpoint, []),
      # Starts a worker by calling: MyAppWeb.Worker.start_link(arg)
      # {MyAppWeb.Worker, arg},
      worker(Mongo, [[database:
        Application.get_env(:mong, :db)[:name], name: :mongo, pool_size: 100]]),
     {ConCache, [name: :my_cache, ttl_check_interval: false]}
    ]

Now as this has been solved, you should perhaps move to the new supervision syntax. worker and supervisor macros are deprecated since 1.6 or so.

could you please refer to a reference?

From memory as I can’t use hexdocs on my mobile:

https://hexdocs.pm/elixir/Supervisor.html

1 Like