@@ -33,6 +33,9 @@ defmodule Strom.GenMix do
3333 alias Strom.GenMix.Streams
3434 alias Strom.GenMix.Tasks
3535
36+ @ type t ( ) :: % __MODULE__ { }
37+
38+ @ spec start ( __MODULE__ . t ( ) ) :: __MODULE__ . t ( )
3639 def start ( % __MODULE__ { process_chunk: process_chunk , opts: opts , composite: composite } = gm )
3740 when is_list ( opts ) do
3841 gm = % {
@@ -69,20 +72,38 @@ defmodule Strom.GenMix do
6972 end
7073
7174 @ impl true
75+ @ spec init ( __MODULE__ . t ( ) ) :: { :ok , __MODULE__ . t ( ) }
7276 def init ( % __MODULE__ { } = gm ) do
7377 { :ok , % { gm | pid: self ( ) } }
7478 end
7579
80+ @ spec start_tasks ( pid ( ) , Strom . flow ( ) ) :: { pid ( ) , map ( ) }
81+ def start_tasks ( gm_pid , input_streams ) do
82+ GenServer . call ( gm_pid , { :start_tasks , input_streams } )
83+ end
84+
85+ @ spec run_tasks ( pid ( ) , atom ( ) ) :: :ok
86+ def run_tasks ( gm_pid , output_name ) do
87+ GenServer . call ( gm_pid , { :run_tasks , output_name } )
88+ end
89+
7690 @ spec call ( map ( ) , map ( ) ) :: map ( ) | no_return ( )
7791 def call ( flow , gm ) , do: Streams . call ( flow , gm )
7892
93+ @ spec state ( pid ( ) ) :: __MODULE__ . t ( )
7994 def state ( pid ) , do: GenServer . call ( pid , :state )
8095
8196 @ spec stop ( any ( ) ) :: any ( )
8297 def stop ( gm ) do
8398 GenServer . call ( gm . pid , :stop )
8499 end
85100
101+ @ spec transfer_tasks ( pid ( ) , map ( ) , atom ( ) ) :: :ok
102+ def transfer_tasks ( gm_pid , new_tasks , all_or_old ) when all_or_old in [ :all , :old ] do
103+ GenServer . cast ( gm_pid , { :transfer_tasks , new_tasks , all_or_old } )
104+ end
105+
106+ @ spec process_chunk ( atom ( ) , list ( ) , Strom . flow ( ) , any ( ) ) :: { Strom . flow ( ) , boolean ( ) , any ( ) }
86107 def process_chunk ( _input_stream_name , chunk , outputs , nil ) do
87108 outputs
88109 |> Enum . reduce ( { % { } , false , nil } , fn { output_name , output_stream_fun } , { acc , any? , nil } ->
@@ -97,7 +118,7 @@ defmodule Strom.GenMix do
97118 new_tasks = Tasks . start_tasks ( input_streams , gm )
98119 tasks = Map . merge ( gm . tasks , new_tasks )
99120
100- { :reply , { :ok , gm . pid , new_tasks } ,
121+ { :reply , { gm . pid , new_tasks } ,
101122 % { gm | tasks_started: true , tasks_run: false , tasks: tasks , input_streams: input_streams } }
102123 end
103124
@@ -120,16 +141,6 @@ defmodule Strom.GenMix do
120141 { :reply , :ok , % { gm | clients: clients } }
121142 end
122143
123- def handle_call (
124- { :replace_tasks , new_tasks } ,
125- _from ,
126- % __MODULE__ { tasks_started: true } = gm
127- ) do
128- old_tasks = Map . drop ( gm . tasks , Map . keys ( new_tasks ) )
129- run_new_tasks_and_halt_the_old_ones ( new_tasks , old_tasks , gm . accs )
130- { :reply , :ok , % { gm | tasks_run: true , tasks: new_tasks } }
131- end
132-
133144 def handle_call ( :stop , _from , % __MODULE__ { } = gm ) do
134145 gm = % { gm | stopping: true }
135146
@@ -151,12 +162,19 @@ defmodule Strom.GenMix do
151162 after_action ( % { gm | stopping: true } )
152163 end
153164
154- def handle_cast ( { :transfer_tasks , new_tasks } , gm ) do
165+ def handle_cast ( { :transfer_tasks , new_tasks , :all } , gm ) do
155166 run_new_tasks_and_halt_the_old_ones ( new_tasks , gm . tasks , gm . accs )
156167
157168 after_action ( % { gm | stopping: true } )
158169 end
159170
171+ def handle_cast ( { :transfer_tasks , new_tasks , :old } , gm ) do
172+ old_tasks = Map . drop ( gm . tasks , Map . keys ( new_tasks ) )
173+ run_new_tasks_and_halt_the_old_ones ( new_tasks , old_tasks , gm . accs )
174+
175+ after_action ( % { gm | tasks_run: true , tasks: new_tasks } )
176+ end
177+
160178 def handle_cast (
161179 { :put_data , { input_name , task_pid } , { new_data , new_acc } } ,
162180 % __MODULE__ { } = gm
0 commit comments