Last active
December 1, 2017 15:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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