Skip to content

Instantly share code, notes, and snippets.

@gogogarrett
Last active June 11, 2016 22:50
Show Gist options
  • Save gogogarrett/7b26b3fccbed42271eb5dce58e96341d to your computer and use it in GitHub Desktop.
Save gogogarrett/7b26b3fccbed42271eb5dce58e96341d to your computer and use it in GitHub Desktop.
Stand alone otp elixir application
defmodule PlayArcadeGame do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
worker(PlayArcadeGame.Workflow, []),
]
opts = [strategy: :one_for_one, name: PlayArcadeGame.Supervisor]
Supervisor.start_link(children, opts)
end
end
defmodule PlayArcadeGame.Workflow do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, :ok, name: :PlayArcadeGameWorkflow)
end
def call(student, game) do
GenServer.call(:PlayArcadeGameWorkflow, {:play_game, student, game})
end
def init(:ok) do
{:ok, %{}}
end
def handle_call({:play_game, student, game}, _from, state) do
case call_workflow(student, game) do
{:ok, msg} ->
{:reply, {:ok, msg}, state}
{:error, reason} ->
{:reply, {:error, reason}, state}
end
end
defp call_workflow(student, game) do
# granted these services / steps would need to also share the same ecto schema / models and connect to the same database
with {:ok, _} <- Service.EnsureEnoughAcorns.call(student.acorns, game.cost),
{:ok, _} <- deduct_acorns(student, game.cost),
{:ok, _} <- Service.CreateStudentEvent.call(%{id: 1}),
do: {:ok, "workflow successful"}
end
end
@mootpointer
Copy link

What benefit is the Genserver giving you here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment