1- defmodule Naive.DynamicSymbolSupervisor do
1+ defmodule Naive.DynamicTraderSupervisor do
22 @ moduledoc """
33 Dynamic symbol supervisor, in charge of supervise runtime created symbols
44 """
55 use DynamicSupervisor
66
7+ require Logger
8+
79 alias Naive.Repo
810 alias Naive.Schema.Settings
9- alias Naive.SymbolSupervisor
11+ alias Naive.Strategy
12+ alias Naive.Trader
1013
1114 import Ecto.Query , only: [ from: 2 ]
1215
13- require Logger
14-
15- @ registry :naive_symbol_supervisors
16+ @ registry :naive_traders
1617
1718 def start_link ( _arg ) do
1819 DynamicSupervisor . start_link ( __MODULE__ , [ ] , name: __MODULE__ )
@@ -22,7 +23,7 @@ defmodule Naive.DynamicSymbolSupervisor do
2223 DynamicSupervisor . init ( strategy: :one_for_one )
2324 end
2425
25- def autostart_workers ( ) do
26+ def autostart_workers do
2627 Repo . all (
2728 from ( s in Settings ,
2829 where: s . status == "on" ,
@@ -34,39 +35,34 @@ defmodule Naive.DynamicSymbolSupervisor do
3435
3536 def start_worker ( symbol ) do
3637 Logger . info ( "Starting trading on #{ symbol } " )
37- update_status ( symbol , "on" )
38+ Strategy . update_status ( symbol , "on" )
3839 start_child ( symbol )
3940 end
4041
4142 def stop_worker ( symbol ) do
4243 Logger . info ( "Stopping trading on #{ symbol } " )
43- update_status ( symbol , "off" )
44+ Strategy . update_status ( symbol , "off" )
4445 stop_child ( symbol )
4546 end
4647
4748 def shutdown_worker ( symbol ) when is_binary ( symbol ) do
4849 Logger . info ( "Shutdown of trading on #{ symbol } initialized" )
4950
5051 { :ok , settings } =
51- update_status (
52+ Strategy . update_status (
5253 symbol ,
5354 "shutdown"
5455 )
5556
56- Naive.Leader . notify ( :settings_updated , settings )
57- { :ok , settings }
58- end
57+ Trader . notify ( :settings_updated , settings )
5958
60- defp update_status ( symbol , status ) when is_binary ( symbol ) and is_binary ( status ) do
61- Repo . get_by ( Settings , symbol: symbol )
62- |> Ecto.Changeset . change ( % { status: status } )
63- |> Repo . update ( )
59+ { :ok , settings }
6460 end
6561
6662 defp start_child ( args ) do
6763 DynamicSupervisor . start_child (
6864 __MODULE__ ,
69- { SymbolSupervisor , args }
65+ { Trader , args }
7066 )
7167 end
7268
0 commit comments