Closed quolpr closed 3 years ago
@quolpr Can you elaborate a bit more? You would like to run a GenStage producer process as a Singleton?
@ericdude4 yep, you are correct
@quolpr I don't see why not. The PR I have open will make this a bit easier since it modernizes this dependency a bit. https://github.com/arjan/singleton/pull/6. Let me know how it works!
@ericdude4 good work! Unfortunately, I moved to :global
, so I can't test your PR
@quolpr What do you mean when you say you switched to :global
?
@ericdude4
I use :global
directly, without any packages(as I understand this package is an abstraction over :global
).
The GenServer's start_link(or any other process) function is so:
def start_link(args, _opts \\ []) do
:global.trans(
{__MODULE__, __MODULE__},
fn ->
case GenStage.start_link(__MODULE__, args, name: {:global, __MODULE__}) do
{:ok, pid} ->
{:ok, pid}
{:error, {:already_started, pid}} ->
Process.link(pid)
{:ok, pid}
error ->
Logger.error("Failed to start producer!", inspect(error))
error
end
end,
Node.list(:connected),
5
)
end
And GenServers are starting under the supervisor at the application.ex
@quolpr cool approach! Thanks for sharing. That's basically exactly how Singleton works anyways
As I noticed, this library works with only GenServer. But is it possible to use it for GenStag? https://hexdocs.pm/gen_stage/GenStage.html