I’m trying to do what was described here: GenStage - Determining Completion
I have a producer that generates a finite stream of data
I have a consumer that writes this data to a database
This consumer will be a subscriber to a lot of other producers that generate the same type of content, so I don’t want to hard code the subscription in the consumer’s init.
defmodule NHVerify.Application do
alias NHVerify.Producers.Hawaii
alias NHVerify.Consumers.LicenseeDBWriter
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
worker(Hawaii, [], name: :hawaii),
worker(LicenseeDBWriter, [:hawaii]),
]
opts = [strategy: :one_for_one, name: NHVerify.Supervisor]
IO.puts inspect "Starting"
Supervisor.start_link(children, opts)
end
end
defmodule NHVerify.Producers.Hawaii do
alias Experimental.GenStage
def start_link() do
GenStage.from_enumerable(get_licensees(), consumers: :permanent)
end
def get_licensees() do
# returns a stream
end
end
defmodule NHVerify.Consumers.LicenseeDBWriter do
alias Experimental.GenStage
use GenStage
def start_link(producer_name) do
GenStage.start_link(__MODULE__, producer_name)
end
def init(producer_name) do
{:consumer, :does_not_matter, subscribe_to: [:hawaii] }
end
def handle_events(licensees, _from, caller) do
save_licensees licensees
{:noreply, [], caller}
end
def handle_info({{_pid, _sub}, {:producer, :done}}, _) do
{:stop, :normal, 0}
end
end
my mix.exs has:
def application do
# elixir 1.4
[extra_applications: [:logger],
mod: {NHVerify.Application, []}]
end
When I run: mix run --no-halt
I get:
15:02:51.081 [info] Application nh_verify exited: NHVerify.Application.start(:normal, ) returned an error: shutdown: failed to start child: NHVerify.Consumers.LicenseeDBWriter
** (EXIT) no process: the process is not alive or there’s no process currently associated with the given name, possibly because its application isn’t started
** (Mix) Could not start application nh_verify: NHVerify.Application.start(:normal, ) returned an error: shutdown: failed to start child: NHVerify.Consumers.LicenseeDBWriter
** (EXIT) no process: the process is not alive or there’s no process currently associated with the given name, possibly because its application isn’t started
If I don’t try and subscribe the LicenseeDBWriter, then things spin up just fine. The problem appears to be with the way I’m trying to reference the producer’s name. I assume I need to create my producer first, let it spin up, and then the consumer. How do I do that?
Note: I realize that, from what I’ve shown, GenStage is overkill. There’s more to it than this, but I’ve stripped everything else away to try and clarify the problem.