Skip to content

Instantly share code, notes, and snippets.

@ihorkatkov
Created January 28, 2019 07:57
Show Gist options
  • Save ihorkatkov/ade619d92e788c658e5ece2d5e3452c1 to your computer and use it in GitHub Desktop.
Save ihorkatkov/ade619d92e788c658e5ece2d5e3452c1 to your computer and use it in GitHub Desktop.
Genserver example
defmodule Spread.Pulser.Worker do
@moduledoc """
This module manages the flow of opportunities and trades
"""
use GenServer
alias Spread.Pulser.Logic, as: PulserLogic
alias Spread.TradingPair.Manager
## Client API
def start_link(trading_pair) do
GenServer.start_link(__MODULE__, trading_pair, name: :"#{PulserWorker}-#{trading_pair.id}")
end
## Server Callbacks
def init(trading_pair) do
schedule(3000)
{:ok, trading_pair}
end
defp schedule(debounce \\ Application.get_env(:strategist_spread_elixir, :debounce)),
do:
Process.send_after(
self(),
:schedule,
debounce
)
def handle_info(:schedule, trading_pair) do
schedule()
PulserLogic.run(trading_pair) # This one could run more than 100 ms
{:noreply, trading_pair}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment