gus
Update state of an Agent at a specific time using Process.send_after/4
Hi all,
I am having some trouble finding resources on how to use Process.send_after/4 to update an Agent. I’m exploring a use case where I update a bunch of Agents each hour, and have all the changes be applied at the same time. In my head, I thought it would be pretty easy to use Process.send_after/4 to do so, but after working on the implementation, I discovered that I’m not sure what message to send to the process. I realize that under the hood, Agent is just a simple GenServer, and after looking at the source code the update is implemented as so:
def update(agent, fun, timeout \\ 5000) when is_function(fun, 1) do
GenServer.call(agent, {:update, fun}, timeout)
end
With a server implementation of:
def handle_call({:update, fun}, _from, state) do
{:reply, :ok, run(fun, [state])}
end
But, unless I’m mistake, I think if I were to use Process.send_after(pid, {:update, fn _ -> new_value end}, ms_until_update), it wouldn’t be handled by the handle_call/3 function shown above, because in a normal GenServer it would instead be handled by a (non-existent) handle_info/2 function. So I guess I’m not sure how to go about doing this? Or even if this is the right way/thinking for updating the state of a bunch of Agents at the same time?
I realize the easy alternative is just implementing my own version of agent that supports this handle_info/2 call 
I’m looking forward to hearing all y’alls input!
Thanks!
Gus
Most Liked
cmo
Or turn your Agents into GenServers.
dimitarvp
Does it have to be an Agent in particular? A normal GenServer handles that just fine.







