I'm not sure what I'm doing wrong, but I created an elixir application if I ran it using
iex -S mix
the application loads the supervisor, the supervisor loads the gen_server, which connects to rabbitmq and continues to listen to the messages, but if I run the application with
mix app.start
or
mix run
the supervisor loads, the worker starts and connects to rabbitmq, but it immediately ends without any error, example code:
mix.exs
defmodule Sample.Mixfile do
use Mix.Project
def project do
[app: :sample,
version: "0.0.1",
elixir: "~> 1.0",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end
def application do
[applications: [:logger, :rabbit_common, :ssl, :erlcloud, :amqp],
mod: {Sample, []}]
end
defp deps do
[
{:erlcloud, git: "https://github.com/gleber/erlcloud" },
{:amqp, "~> 0.1.1"}
]
end
end
sample.ex file
defmodule Sample do
use Application
require Logger
def start(_type, _args) do
IO.puts("Starting App")
Sample.Supervisor.start_link
end
end
supervisor.ex
defmodule Sample.Supervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, :ok)
end
def init(:ok) do
children = [
worker(Sample.Worker, [[name: :sample_worker]])
]
supervise(children, strategy: :one_for_one)
end
end
and worker.ex
defmodule Sample.Worker do
use GenServer
use AMQP
@exchange "exchange_name"
@queue "queue_name"
@doc """
Starts the worker who consumes rabbitmq messages on exchange @exchange
"""
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, :ok, opts)
end
def stop do
GenServer.cast(__MODULE__, :stop)
end
def init(:ok) do
{:ok, connection} = Connection.open
{:ok, channel} = Channel.open(connection)
Queue.declare(channel, @queue, durable: true, arguments: [])
Exchange.direct(channel, @exchange, durable: true)
Queue.bind(channel, @queue, @exchange)
{:ok, _consumer_tag} = Basic.consume(channel, @queue)
{:ok, channel}
end
def handle_info({:basic_consume_ok, %{consumer_tag: consumer_tag}}, channel) do
IO.puts("#{__MODULE__} consumer connected")
{:noreply, channel}
end
def handle_info({:basic_cancel, %{consumer_tag: consumer_tag}}, channel) do
IO.puts("#{__MODULE__} consumer unexpectedly cancelled")
{:stop, :normal, channel}
end
def handle_info({:basic_cancel_ok, %{consumer_tag: consumer_tag}}, channel) do
IO.puts("#{__MODULE__} consumer Basic.cancel")
{:noreply, channel}
end
def handle_info({:basic_deliver, payload, %{delivery_tag: tag, redelivered: redelivered}}, channel) do
spawn fn -> consume(channel, tag, redelivered, payload) end
{:noreply, channel}
end
def handle_info(message, state) do
IO.puts("#{__MODULE__} handle_info called with #{message}")
{:noreply, state}
end
def handle_call(message, _from, state) do
IO.puts("#{__MODULE__} handle_call called with #{message}")
{:reply, :response, state}
end
def handle_cast(message, state) do
IO.puts("#{__MODULE__} handle_cast called with #{message}")
{:noreply, state}
end
defp consume(channel, tag, redelivered, payload) do
try do
IO.puts("Consuming #{payload}")
Basic.ack channel, tag
rescue
exception ->
Basic.reject channel, tag, requeue: not redelivered
IO.puts "Error received: #{exception}"
end
end
end