Skip to content

Instantly share code, notes, and snippets.

@boriscy
Last active February 21, 2017 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boriscy/8ea91b693fee48d6c29a0892d7644291 to your computer and use it in GitHub Desktop.
Save boriscy/8ea91b693fee48d6c29a0892d7644291 to your computer and use it in GitHub Desktop.
# web/services/messaging_service.ex
defmodule Publit.MessagingService do
@moduledoc """
Service to send notifications to the user
"""
@message_api Application.get_env(:publit, :message_api)
def status_code(resp) do
{_s, msg} = resp
{:error, msg}
end
def send_messages(tokens, data, cb_ok, cb_error, cb_net_error \\ fn(v) -> end) do
Task.Supervisor.start_child(Publit.Messaging.Supervisor, fn() ->
resp = @message_api.send_messages(tokens, data)
case resp.status do
:ok -> cb_ok.(resp)
:error -> cb_error.(resp)
:network_error -> cb_net_error.(resp)
end
end)
end
def send_direct_messages(tokens, data) do
@message_api.send_messages(tokens, data)
end
end
# lib/publit.ex
defmodule Publit do
use Application
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec
# Define workers and child supervisors to be supervised
children = [
# Start the Ecto repository
supervisor(Publit.Repo, []),
# Start the endpoint when the application starts
supervisor(Publit.Endpoint, []),
supervisor(Task.Supervisor, [[name: Publit.Messaging.Supervisor]]),
# Start your own worker by calling: Publit.Worker.start_link(arg1, arg2, arg3)
# worker(Publit.Worker, [arg1, arg2, arg3])
worker(Publit.CartService, [])
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Publit.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
Publit.Endpoint.config_change(changed, removed)
:ok
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment