Skip to content

Instantly share code, notes, and snippets.

@m1dnight
Forked from ntrepid8/gen_server_tpl.ex
Last active March 2, 2018 09:53
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 m1dnight/89744012b25ebcf7e7917a9a07965b7f to your computer and use it in GitHub Desktop.
Save m1dnight/89744012b25ebcf7e7917a9a07965b7f to your computer and use it in GitHub Desktop.
Elixir GenServer Template
defmodule Server do
@moduledoc """
A GenServer template for a "singleton" process.
"""
use GenServer
require Logger
def start_link(opts \\ []) do
Logger.debug "Started with opts: #{inspect opts}"
GenServer.start_link(__MODULE__, opts, [name: __MODULE__])
end
def init(opts) do
Logger.debug "Init with opts: #{inspect opts}"
{:ok, opts}
end
#######
# API #
#######
def send_something(m) do
GenServer.cast(__MODULE__, m)
end
#############
# Callbacks #
#############
def handle_call(m, from, state) do
Logger.debug "Call #{inspect m} from #{inspect from} with state #{inspect state}"
{:reply, :response, state}
end
def handle_cast(m, state) do
Logger.debug "Cast #{inspect m} with state #{inspect state}"
{:noreply, state}
end
def handle_info(m, state) do
Logger.debug "Info #{inspect m} with state #{inspect state}"
{:noreply, state}
end
###########
# Helpers #
###########
defp private(x) do
x
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment