events from a database or even another source like Apache’s Kafka. Process, sort, catalog, and store metrics as they become available. • Work Queue — We could produce works of unit to be completed by a series of consumers. • Event Processing — Similar to a data pipeline, we could receive, process, sort, and take action on events emitted in real time from our sources.
start_link(args) do GenStage.start_link(__MODULE__, args, name: __MODULE__) end @impl GenStage def init(_state) do {:producer, %{events: []}} end def handle_demand(demand, state) when is_integer(demand) and demand > 0 do events = Alice.make_donuts(demand) {:noreply, events, state} end end Workflow: Making
start_link(args) do GenStage.start_link(__MODULE__, args, name: __MODULE__) end def init(_) do {:consumer, :state, subscribe_to: [{Baking, min_demand: 2, max_demand: 5}]} end def handle_events(events, _from, state) do events |> Enum.map(&Clair.pack_donut/1) {:noreply, [], state} end end Workflow: Packing