4141-define (SERVER , ? MODULE ).
4242
4343-record (state , {tasks = #{}}).
44- -record (task , {state = not_started , pid , steps , done_steps }).
44+ -record (task , {state = not_started , pid , steps , done_steps , last_message }).
4545
4646% %%===================================================================
4747% %% API
@@ -99,14 +99,14 @@ handle_call({task_status, Type}, _From, #state{tasks = Tasks} = State) ->
9999 {reply , not_started , State };
100100 # task {state = not_started } ->
101101 {reply , not_started , State };
102- # task {state = failed , done_steps = Steps , pid = Error } ->
102+ # task {state = failed , done_steps = Steps , last_message = Error } ->
103103 {reply , {failed , Steps , Error }, State };
104- # task {state = aborted , done_steps = Steps } ->
105- {reply , {aborted , Steps }, State };
106- # task {state = working , done_steps = Steps } ->
107- {reply , {working , Steps }, State };
108- # task {state = completed , done_steps = Steps } ->
109- {reply , {completed , Steps }, State }
104+ # task {state = aborted , done_steps = Steps , last_message = Msg } ->
105+ {reply , {aborted , Steps , Msg }, State };
106+ # task {state = working , done_steps = Steps , last_message = Msg } ->
107+ {reply , {working , Steps , Msg }, State };
108+ # task {state = completed , done_steps = Steps , last_message = Msg } ->
109+ {reply , {completed , Steps , Msg }, State }
110110 end ;
111111handle_call ({abort_task , Type }, _From , # state {tasks = Tasks } = State ) ->
112112 case maps :get (Type , Tasks , none ) of
@@ -126,26 +126,26 @@ handle_call(_Request, _From, State = #state{}) ->
126126 {noreply , NewState :: # state {}} |
127127 {noreply , NewState :: # state {}, timeout () | hibernate } |
128128 {stop , Reason :: term (), NewState :: # state {}}).
129- handle_cast ({task_finished , Type , Pid }, # state {tasks = Tasks } = State ) ->
129+ handle_cast ({task_finished , Type , Pid , Msg }, # state {tasks = Tasks } = State ) ->
130130 case maps :get (Type , Tasks , none ) of
131131 # task {state = working , pid = Pid2 } = T when Pid == Pid2 ->
132- Tasks2 = maps :put (Type , T # task {state = completed , pid = none }, Tasks ),
132+ Tasks2 = maps :put (Type , T # task {state = completed , pid = none , last_message = Msg }, Tasks ),
133133 {noreply , State # state {tasks = Tasks2 }};
134134 _ ->
135135 {noreply , State }
136136 end ;
137- handle_cast ({task_progress , Type , Pid , Count }, # state {tasks = Tasks } = State ) ->
137+ handle_cast ({task_progress , Type , Pid , Count , Msg }, # state {tasks = Tasks } = State ) ->
138138 case maps :get (Type , Tasks , none ) of
139139 # task {state = working , pid = Pid2 , done_steps = Steps } = T when Pid == Pid2 ->
140- Tasks2 = maps :put (Type , T # task {done_steps = Steps + Count }, Tasks ),
140+ Tasks2 = maps :put (Type , T # task {done_steps = Steps + Count , last_message = Msg }, Tasks ),
141141 {noreply , State # state {tasks = Tasks2 }};
142142 _ ->
143143 {noreply , State }
144144 end ;
145145handle_cast ({task_error , Type , Pid , Error }, # state {tasks = Tasks } = State ) ->
146146 case maps :get (Type , Tasks , none ) of
147147 # task {state = working , pid = Pid2 } = T when Pid == Pid2 ->
148- Tasks2 = maps :put (Type , T # task {state = failed , pid = Error }, Tasks ),
148+ Tasks2 = maps :put (Type , T # task {state = failed , last_message = Error }, Tasks ),
149149 {noreply , State # state {tasks = Tasks2 }};
150150 _ ->
151151 {noreply , State }
@@ -186,20 +186,24 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
186186
187187work_loop (Task , JobState , JobFun , Rate , StartDate , CurrentProgress ) ->
188188 try JobFun (JobState ) of
189- {ok , _NewState , 0 } ->
190- gen_server :cast (? MODULE , {task_finished , Task , self ()});
191- {ok , NewState , Count } ->
192- gen_server :cast (? MODULE , {task_progress , Task , self (), Count }),
189+ {ok , _NewState , 0 , Msg } ->
190+ gen_server :cast (? MODULE , {task_finished , Task , self (), Msg });
191+ {ok , NewState , Count , Msg } ->
192+ gen_server :cast (? MODULE , {task_progress , Task , self (), Count , Msg }),
193193 NewProgress = CurrentProgress + Count ,
194- TimeSpent = erlang :monotonic_time (second ) - StartDate ,
195- SleepTime = max (0 , NewProgress / Rate * 60 - TimeSpent ),
194+ SleepTime = case Rate of
195+ infinity -> 0 ;
196+ _ ->
197+ TimeSpent = erlang :monotonic_time (second ) - StartDate ,
198+ max (0 , NewProgress / Rate * 60 - TimeSpent )
199+ end ,
196200 receive
197201 abort -> ok
198202 after round (SleepTime * 1000 ) ->
199203 work_loop (Task , NewState , JobFun , Rate , StartDate , NewProgress )
200204 end ;
201205 {error , Error } ->
202206 gen_server :cast (? MODULE , {task_error , Task , self (), Error })
203- catch _ : _ ->
204- gen_server :cast (? MODULE , {task_error , Task , self (), internal_error })
207+ catch T : E : S ->
208+ gen_server :cast (? MODULE , {task_error , Task , self (), { internal_error , T , E , S } })
205209 end .
0 commit comments