You can use GenServer as well. If list_servers() and go_through_and_email() will be defined as helper functions you should be able to do something like this:
defmodule ExampleGenServer do
use GenServer
## Client API
def start_link(name) do
GenServer.start_link(__MODULE__, [], name: name)
end
def go_through(name) do
GenServer.cast(name, {:go_through, name})
end
## Server API / Callbacks
def init(initial_state) do
{:ok, initial_state}
end
def handle_cast({:go_through, name}, state) do
state = go_through_and_email(server)
{:noreply, state}
end
def handle_info({:some_info}, state) do
# stuff
{:noreply, state}
end
## Helper functions
defp list_servers() do
# stuff
end
defp go_through_and_email() do
# stuff
end
end
Then you could create new instance of GenServer in a loop:
list_servers()
|> Enum.map(fn(server) ->
ExampleGenserver.start_link(server)
end)
I've made it quickly during a break so I could miss / mess up something ;)
If you will need some additional details about GenServers you may refer to my Gist. It describes GenServer client / server API, messaging and arg passing ;)
And one more thing, remember about GenServer teardown! ;)
Task
functions? – DogbertTask.await
. The default is 5 seconds. A simple Task.async + await will spawn all the tasks at the same time though. You probably want to spawn in batches of e.g. 10 I guess (especially if the list is large)? – Dogbert