FelisOrion

FelisOrion

How to run more then one same worker (Cachex) inside Supervision

Hi, I’m trying to add inside my application supervision tree two of Cachex (https://hexdocs.pm/cachex/Cachex.html) worker but getting error:

** bad child specification, more than one child specification has the id: Cachex. **

code :
children = [

worker(Cachex, [:requests, [] ]),
worker(Cachex, [:ntf, [] ])
]

As error text suggesting using to use maps as child specifications, i changed code to look like this:

children = [
Supervisor.child_spec({Cachex, [:requests, []]}, id: :my_cache_1),
Supervisor.child_spec({Cachex, [:ntf, []]}, id: :my_cache_2)
]

but now getting ** (EXIT) :invalid_name **
I wonder if there is any way to make it work or if some one more familiar with this library and can help me.

Thanks!

Marked As Solved

dom

dom

This:

{Cachex, [:requests, []]}

means the supervisor will call Cachex.start_link/1 like this:

Cachex.start_link([:requests, []])

rather than Cachex.start_link/2, which is probably what you expected:

Cachex.start_link(:requests, [])

So passing {Cachex, :requests} instead should work, because it’ll call Cachex.start_link(:requests).

If you want to pass options later on you’ll have to set the start function in the child spec yourself to make sure it calls start_link/2 and not start_link/1.

For context, recent Elixir guidelines are to always use start_link/1, so it’s normally not a problem. Cachex probably predates that.

Also Liked

FelisOrion

FelisOrion

Just in case some one need to see code for implementation:

def start(_type, _args) do
  import Supervisor.Spec
  import Cachex.Spec

  children = [
      %{id: Cache1, start: {Cachex, :start_link, [:requests, []]}},
      %{id: Cache2, start: {Cachex, :start_link, [:messages, []]}}
    ]
  opts = [strategy: :one_for_one]
  Supervisor.start_link(children, opts)
end
McTheels

McTheels

Going to leave this here in case the above responses do not work for later versions of the dependencies as well as elixir.

You can also set the following in your application.ex

children = [
      # Start the Telemetry supervisor
      # Start the Ecto repository
      Supervisor.child_spec({Cachex, :my_cache}, id: :my_cache),
      Supervisor.child_spec({Cachex, :temp_cache}, id: :temp_cache),
      ...
     ]
kamaroly

kamaroly

This worked for me! Thank you.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement