昨天没有输出Fun中的io的原因是因为在任务函数中没有调用Fun方法,相当于只传了Fun函数但是没有进行调用,因此没有执行Fun函数,所以控制台中没有进行io的输出,今天在add_job中调用了Fun方法并执行,所以输出了相应的io。
同时今天增加了完成工作的字段,能够记录以及完成的工作数量,这样相较于昨天单纯的输出0来说,能够统计工人已经完成工作数,基本符合了书上第2个小问的要求
-module(gen_server_test).
-export([start_link/0, add_job/2, work_wanted/0, job_done/1,statistics/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([test_job_centre/0]).
-define(SERVER, ?MODULE).
%%创建一个记录,记录中包含了一个工作队列,与下一个工作任务编号
-record(state,
{
jobs = queue:new(),
next_job_number = 1,
workers = gb_sets:new(),
completed_jobs = gb_sets:new()
}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
add_job(Fun,WorkerPid) ->
gen_server:call(?SERVER, {add_job, Fun,WorkerPid}).
work_wanted() ->
gen_server:call(?SERVER, work_wanted).
job_done(JobNumber) ->
gen_server:call(?SERVER, {job_done, JobNumber}).
statistics() ->
gen_server:call(?SERVER, statistics).
%%---------------------------------------------------------------------------
init([]) ->
{ok, #state{}}.
handle_call({add_job, Fun,WorkerPid}, _From, State) ->
%%向队列中添加元素,队列的任务为执行Fun中的操作
NewJobs = queue:in({State#state.next_job_number, Fun}, State#state.jobs),
%%执行任务函数
Fun(),
NewWorkers = gb_sets:add(WorkerPid, State#state.workers),
% 建立与工人的连接
link(WorkerPid),
{reply, State#state.next_job_number,
State#state{jobs = NewJobs,
next_job_number = State#state.next_job_number + 1,
workers = NewWorkers}};
handle_call(work_wanted, _From, State) ->
IsEmpty = queue:is_empty(State#state.jobs),
case IsEmpty of
true ->
{reply, no, State};
false ->
{{value, {JobNumber, Fun}}, NewJobs} = queue:out(State#state.jobs),
{reply, {JobNumber, Fun}, State#state{jobs = NewJobs}}
end;
handle_call({job_done, JobNumber}, _From, State) ->
NewCompletedJobs = gb_sets:add(JobNumber, State#state.completed_jobs),
{reply, ok, State#state{completed_jobs = NewCompletedJobs}};
handle_call(statistics, _From, State) ->
{reply, #{waiting => queue:len(State#state.jobs),
in_progress => gb_sets:size(State#state.workers),
completed => gb_sets:size(State#state.completed_jobs)}, State};
handle_call(Request, _From, State) ->
{reply, {error, {unknown_request, Request}}, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({exit, WorkerPid, _Reason}, State) ->
%% 如果工人进程崩溃,则从workers集合中移除,并将任务重新添加到队列
NewWorkers = gb_sets:delete(WorkerPid, State#state.workers),
%% 查找并重新添加任务
{ok, Fun} = find_task_for_worker(WorkerPid, State#state.jobs),
NewJobs = queue:in({State#state.next_job_number - 1, Fun}, State#state.jobs),
{noreply, State#state{workers = NewWorkers, jobs = NewJobs}};
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
find_task_for_worker(WorkerPid, Jobs) ->
case gb_sets:is_member(WorkerPid, gb_sets:from_list(Jobs)) of
true ->
%% 找到对应的任务
{value, {_, Fun}} = queue:out(Jobs),
{ok, Fun};
false ->
{error, not_found}
end.
test_job_centre() ->
%%创建两个任务
Job1 = fun
() ->
io:format("Doing job 1~n")
end,
Job2 = fun
() ->
io:format("Doing job 2~n")
end,
%%将两个任务放入任务队列中
JobNumber1 = add_job(Job1,self()),
io:format("Job number 1 is ~p~n", [JobNumber1]),
JobNumber2 = add_job(Job2,self()),
io:format("Job number 2 is ~p~n", [JobNumber2]),
%%工人领取任务队列任务
{JobNumber1, Job1} = work_wanted(),
io:format("Doing job ~p~n", [{JobNumber1,Job1}]),
{JobNumber2, Job2} = work_wanted(),
io:format("Doing job ~p~n", [{JobNumber2,Job2}]),
job_done(JobNumber1),
job_done(JobNumber2),
case work_wanted() of
no ->
io:format("No more jobs~n");
{JobNumber, Job} ->
io:format("Doing job ~p~n", [{JobNumber,Job}])
end,
exit(normal),
ok.
输出结果为: