arjan / singleton

Global, supervised singleton processes for Elixir
MIT License
108 stars 19 forks source link

Is it possible to use with GenStage? #4

Closed quolpr closed 3 years ago

quolpr commented 4 years ago

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

ericdude4 commented 4 years ago

@quolpr Can you elaborate a bit more? You would like to run a GenStage producer process as a Singleton?

quolpr commented 4 years ago

@ericdude4 yep, you are correct

ericdude4 commented 4 years ago

@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!

quolpr commented 4 years ago

@ericdude4 good work! Unfortunately, I moved to :global, so I can't test your PR

ericdude4 commented 4 years ago

@quolpr What do you mean when you say you switched to :global?

quolpr commented 4 years ago

@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

ericdude4 commented 4 years ago

@quolpr cool approach! Thanks for sharing. That's basically exactly how Singleton works anyways