Created
December 20, 2018 22:58
-
-
Save RickyCook/e439739bf36a827a4490fc414d9a9164 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Notifier.Webhook.App do | |
use Application | |
require Logger | |
def start(_type, _args) do | |
Logger.debug("App.start") | |
import Supervisor.Spec | |
children = [ | |
supervisor(Notifier.Webhook.Worker, []), | |
] | |
opts = [ | |
strategy: :one_for_one, | |
name: Notifier.Webhook.Supervisor, | |
] | |
Supervisor.start_link(children, opts) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Notifier.Webhook.Worker do | |
require Logger | |
use GenServer | |
use AMQP | |
@exchange "amq.topic" | |
@queue "notifier.webhook" | |
## Client API | |
def start_link() do | |
Logger.debug("Worker.start_link") | |
GenServer.start_link(__MODULE__, :ok, []) | |
end | |
## Server Callbacks | |
def init(:ok) do | |
Logger.debug("Worker.init") | |
{:ok, conn} = Connection.open(Application.get_env(:notifier_webhook, :mqUrl)) | |
{:ok, chan} = Channel.open(conn) | |
setup_queue(chan) | |
:ok = Basic.qos(chan, prefetch_count: 10) | |
{:ok, _consumer_tag} = Basic.consume(chan, @queue) | |
{:ok, chan} | |
end | |
def handle_info({:basic_consume_ok, %{consumer_tag: consumer_tag}}, chan) do | |
{:noreply, chan} | |
end | |
def handle_info({:basic_cancel, %{consumer_tag: consumer_tag}}, chan) do | |
{:stop, :normal, chan} | |
end | |
def handle_info({:basic_cancel_ok, %{consumer_tag: consumer_tag}}, chan) do | |
{:noreply, chan} | |
end | |
def handle_info({:basic_deliver, payload, %{delivery_tag: tag}}, chan) do | |
spawn fn -> consume(chan, tag, payload) end | |
{:noreply, chan} | |
end | |
defp setup_queue(chan) do | |
{:ok, _} = Queue.declare(chan, @queue, durable: true) | |
:ok = Exchange.topic(chan, @exchange, durable: true) | |
:ok = Queue.bind(chan, @queue, @exchange) | |
end | |
defp consume(chan, tag, payload) do | |
IO.puts("GOT #{payload}") | |
:ok = Basic.ack(chan, tag) | |
rescue | |
exception -> | |
:ok = Basic.reject(chan, tag) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Notifier.Webhook.MixProject do | |
use Mix.Project | |
def project do | |
[ | |
app: :notifier_webhook, | |
version: "0.1.0", | |
elixir: "~> 1.7", | |
start_permanent: Mix.env() == :prod, | |
deps: deps() | |
] | |
end | |
# Run "mix help compile.app" to learn about applications. | |
def application do | |
[ | |
extra_applications: [:logger], | |
mod: {Notifier.Webhook.App, []} | |
] | |
end | |
# Run "mix help deps" to learn about dependencies. | |
defp deps do | |
[ | |
{ :amqp, "~> 1.0" }, | |
{ :distillery, "~> 2.0", runtime: false } | |
] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment