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
@gogogarrett
Copy link
Author

This approach would allow the entire "workflow" to be contained in a single elixir app, and included in any other elixir app (phoenix or not). The problem I see here is that these services (which may or may not be shared between other workflow / otp applications) will need to mutate data on the same ecto schema/models that the phoenix app provides (currently).

Perhaps there could be a shared data model layer that interfaces with the database that both the workflows/otp apps and the phoenix app used, but I am just weary that any shared model gem, would just grow quickly to become a bad thing.

Please - open to any suggestions or ideas.

@gogogarrett
Copy link
Author

This approach shows how this could be included directly inside a phoenix application - but am weary that this may lead to a path of a monolith application.

@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