Packages
commanded
1.4.7
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/commanded.reset.ex
defmodule Mix.Tasks.Commanded.Reset do
@moduledoc """
Reset an event handler.
## Usage
mix commanded.reset --app <app> --handler <handler_name>
## Examples
mix commanded.reset --app MyApp --handler MyHandler
mix commanded.reset -a MyApp -h MyHandler
## Command line options
* `-a`, `--app` - the Commanded application
* `-h`, `--handler` - the name of the event handler to reset
* `-q`, `--quiet` - do not log output
"""
use Mix.Task
alias Commanded.Event.Handler
alias Commanded.Registration
@shortdoc "Reset an event handler to its start_from"
@switches [
app: :string,
handler: :string,
quiet: :boolean
]
@aliases [
a: :app,
h: :handler,
q: :quiet
]
@doc false
def run(args) do
{parsed, _args} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
app = Keyword.get(parsed, :app)
handler = Keyword.get(parsed, :handler)
unless app && handler do
Mix.raise("""
Reset requires an application and handler, use "mix commanded.reset --app <app> --handler <handler>"
""")
end
app = String.to_atom("Elixir." <> app)
reset(app, handler, parsed)
end
defp reset(app, handler_name, opts) do
case app.start_link() do
{:ok, _pid} ->
:ok
{:error, {:already_started, _pid}} ->
:ok
{:error, error} ->
Mix.raise("Failed to start " <> inspect(app) <> " due to: " <> inspect(error))
end
quiet = Keyword.get(opts, :quiet, false)
registry_name = Handler.name(app, handler_name)
case Registration.whereis_name(app, registry_name) do
:undefined ->
unless quiet, do: Mix.shell().info("No process found for #{inspect(handler_name)}")
pid ->
unless quiet, do: Mix.shell().info("Resetting #{inspect(handler_name)}")
send(pid, :reset)
end
end
end