Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active December 1, 2017 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/7639513d83cf642499c109ba2cda0805 to your computer and use it in GitHub Desktop.
Save myobie/7639513d83cf642499c109ba2cda0805 to your computer and use it in GitHub Desktop.
This is an example of using GenStage to have a Hound producer/consumer, but the same thing can be done with just a GenServer
defmodule ExampleChecker do
use Hound.Helpers
use GenStage
def start_link(arg \\ []),
do: GenStage.start_link(__MODULE__, arg, [])
def init(_) do
try do
session_id = Hound.start_session()
navigate_to "http://example.com/" # go somewhere to make chrome fully awake
{:producer_consumer, %{session_id: session_id}}
rescue
error ->
{:error, error}
end
end
def handle_cast(:shutdown, state), do: {:stop, :exception, state}
def handle_events(events, _from, state) do
{:noreply, Enum.map(events, &safe_check/1), state}
end
def terminate(reason, state) do
Hound.end_session(self())
super(reason, state)
end
def safe_check(args) do
try do
check(args)
rescue
error ->
GenStage.cast(self(), :shutdown)
{:error, {:exception, error}, email}
end
end
def check(_args) do
# result = do_something(args)
# {:ok, result}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment