I want to connect to the database before my function start
Then you can run this function as a
supervisor(Repo, ) in your application.
Replace https://github.com/dersar00/friends/blob/master/lib/friends/application.ex with
defmodule Friends.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
children = [
worker(Task, [&Friends.save/0], restart: :transient)
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Friends.Supervisor]
Don’t forget to delete https://github.com/dersar00/friends/blob/master/lib/friends.ex#L10
My architecture is:
I build my project using a microservices architecture(rabbitmq), one of my worker need to be builded using elixir, when I get message from my queue, for example I want to save it to db, so how do it?
You would call your function like
Friends.save(data) from a handler which handles new messages from rabbitmq, just like a phoenix controller which handles new requests.
You wouldn’t need the
worker(Task, [&Friends.save/0], restart: :transient) from above.