Packages
honeydew
1.4.4
1.5.0
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.0
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc7
1.0.0-rc6
1.0.0-rc5
1.0.0-rc4
1.0.0-rc3
1.0.0-rc2
1.0.0-rc1
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Pluggable local/clusterable job queue focused on safety.
Current section
Files
Jump to
Current section
Files
lib/honeydew/job_runner.ex
defmodule Honeydew.JobRunner do
@moduledoc false
use GenServer
require Logger
require Honeydew
alias Honeydew.Job
alias Honeydew.Crash
alias Honeydew.Worker
@doc false
def start_link(opts) do
GenServer.start_link(__MODULE__, opts)
end
defmodule State do
@moduledoc false
defstruct [:worker, :job, :module, :worker_private]
end
#
# Internal API
#
def run_link(job, module, worker_private) do
GenServer.start_link(__MODULE__, [self(), job, module, worker_private])
end
@impl true
def init([worker, %Job{job_monitor: job_monitor} = job, module, worker_private]) do
Process.put(:job_monitor, job_monitor)
{:ok, %State{job: job,
module: module,
worker: worker,
worker_private: worker_private}, {:continue, :run}}
end
defp do_run(%State{job: %Job{task: task} = job,
module: module,
worker: worker,
worker_private: worker_private} = state) do
private_args =
case worker_private do
{:state, s} -> [s]
:no_state -> []
end
result =
try do
result =
case task do
f when is_function(f) -> apply(f, private_args)
f when is_atom(f) -> apply(module, f, private_args)
{f, a} -> apply(module, f, a ++ private_args)
end
{:ok, result}
rescue e ->
{:error, Crash.new(:exception, e, System.stacktrace())}
catch
:exit, reason ->
# catch exit signals and shut down in an orderly manner
{:error, Crash.new(:exit, reason)}
e ->
{:error, Crash.new(:throw, e, System.stacktrace())}
end
:ok = Worker.job_finished(worker, %{job | result: result})
state
end
@impl true
def handle_continue(:run, state) do
state = do_run(state)
{:stop, :normal, state}
end
@impl true
def terminate(:normal, _state), do: :ok
def terminate(:shutdown, _state), do: :ok
def terminate({:shutdown, _}, _state), do: :ok
def terminate(reason, _state) do
Logger.info "[Honeydew] JobRunner #{inspect self()} stopped because #{inspect reason}"
end
end