Packages
tarearbol
0.9.0
1.12.0
1.11.2
1.11.1
1.11.0
1.10.4
1.10.3
1.10.2
1.10.0
1.9.102
1.9.101
1.9.100
1.9.99
1.9.11
1.9.10
1.9.9
1.9.8
1.9.7
1.9.6
1.9.5
1.9.4
1.9.3
1.9.2
1.9.1
1.9.0
1.8.2
1.8.1
1.8.0
1.7.0
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.0
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.0
1.2.1
1.2.0
1.1.2
1.1.1
1.1.0
1.0.4
1.0.3
1.0.2
1.0.0
0.99.11
0.99.10
0.99.9
0.99.8
0.99.7
0.99.6
0.99.4
0.99.3
0.99.2
0.99.1
0.99.0
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.0
0.9.6
0.9.5
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.2
0.2.1
0.2.0
0.1.0
The supervised tree of tasks, simplifying the process of handling: - recurrent tasks - retried tasks - long tasks - etc
Current section
Files
Jump to
Current section
Files
lib/tarearbol/application.ex
defmodule Tarearbol.Application do
@moduledoc false
use Application
@spec start(Application.app(), Application.restart_type()) ::
{:error, any()} | {:ok, pid()} | {:ok, pid(), any()}
def start(_type, _args) do
children = [
{Task.Supervisor, [name: Tarearbol.Application]},
{Tarearbol.Cron, []}
]
opts = [strategy: :one_for_one, name: Tarearbol.Supervisor]
Supervisor.start_link(children, opts)
end
##############################################################################
@spec children :: [pid()]
def children, do: Task.Supervisor.children(Tarearbol.Application)
def jobs do
children()
|> Enum.map(&Process.info(&1, :dictionary))
|> Enum.map(fn {:dictionary, dict} -> dict[:job] end)
|> Enum.reject(&is_nil/1)
end
@spec kill :: [:ok | {:error, :not_found | :dead}]
def kill(), do: for(child <- children(), do: kill(child))
@spec kill(pid()) :: :ok | {:error, :not_found | :dead}
def kill(child) when is_pid(child) do
children()
|> Enum.member?(child)
|> do_kill(child)
end
@spec task!((() -> any()) | {module(), atom(), list()}) :: Task.t()
def task!(job) when is_function(job, 0),
do: Task.Supervisor.async_nolink(Tarearbol.Application, job)
def task!({mod, fun, params}) do
Task.Supervisor.async_nolink(Tarearbol.Application, mod, fun, params)
end
##############################################################################
@spec do_kill(boolean(), pid()) :: :ok | {:error, :not_found | :dead}
defp do_kill(true, child),
do: Task.Supervisor.terminate_child(Tarearbol.Application, child)
defp do_kill(false, child),
do: with(:ok <- do_kill(true, child), do: {:error, :dead})
end