-
-
Save m1dnight/89744012b25ebcf7e7917a9a07965b7f to your computer and use it in GitHub Desktop.
Elixir GenServer Template
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 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