Hi,
I’m trying to practice and exercise my understanding of elixir and concurrency. It’s a toy project that I would like to do the following. I have a database with X rows, consisting of an id and a counter (10). A worker runs to take a row, reduces the counter by 1 and waits for 1 seconds before doing it again until the counter is 0.
def init(state) do
schedule()
{:ok, state}
end
def handle_info(:work, state) do
item_id = state.id
IO.inspect(state)
item = Repo.one(from(i in Task, where: i.id == ^item_id))
cond do
item.counter > 0 ->
Repo.transaction(fn ->
Repo.update!(change(item, counter: item.counter - 1))
end)
schedule()
{:noreply, item_id}
true ->
exit(item_id)
# {:stop, :normal, item_id}
end
end
defp schedule() do
Process.send_after(self(), :work, 1000)
end
I’m having a bit of a block on how best to implement a “Monitor” that will launch a number of workers (like say 100 or 500) to do the work. Once a worker is done and there’s work left to do, it should launch a new one, with the next available row id. I’m thinking this isn’t a job for supervisors.
I also didn’t get very far using spawn_monitor, perhaps not using it correctly? Any pointers on how to best approach this part of the problem would be appreciated.
I can start a number of processes, the problem is more the “starting new ones if there’s still work to do once one worker is done” is what’s giving me trouble. Should the workers pass messages back to the monitor, or what’s the correct approach for this?