Skip to content

Instantly share code, notes, and snippets.

@marinho10
Last active February 15, 2019 10:20
Show Gist options
  • Save marinho10/b4aae7f03d576ed2fa3f53786536b297 to your computer and use it in GitHub Desktop.
Save marinho10/b4aae7f03d576ed2fa3f53786536b297 to your computer and use it in GitHub Desktop.
defmodule MyApp.TaskSupervisor do
use DynamicSupervisor
alias MyApp.Management
alias MyApp.WorkerStateStorage
def start_link(arg) do
DynamicSupervisor.start_link(__MODULE__, arg, name: __MODULE__)
end
def init(arg) do
DynamicSupervisor.init(arg)
end
def start_task_worker(task_id) do
spec = {MyApp.TaskWorker, %{task_id: task_id}}
DynamicSupervisor.start_child(__MODULE__, spec)
end
def start_all_task_workers() do
Enum.each(Management.list_all_tasks(), fn %{id: task_id} ->
start_task_worker(task_id)
end)
end
def terminate_task_worker(pid, task_id) do
WorkerStateStorage.delete(task_id)
Supervisor.terminate_child(__MODULE__, pid)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment