Last active
February 15, 2019 10:20
-
-
Save marinho10/b4aae7f03d576ed2fa3f53786536b297 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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