fix cache_indexer bug

This commit is contained in:
Kevin Lynx 2013-07-20 19:38:16 +08:00
parent 2658040f3a
commit 060804ae31

View File

@ -17,7 +17,7 @@
start_standalone/1, start_standalone/1,
start_standalone/2, start_standalone/2,
stop/0]). stop/0]).
-record(state, {work_on = [], done = [], workers = []}). -record(state, {scheduled = false, work_on = [], done = [], workers = []}).
-define(WORKDIR, "sync/"). -define(WORKDIR, "sync/").
-define(DBPOOL, index_builder_pool). -define(DBPOOL, index_builder_pool).
-define(SYNC_TODAY_INTERVAL, 5*60*1000). -define(SYNC_TODAY_INTERVAL, 5*60*1000).
@ -77,10 +77,10 @@ handle_info(sync_today, State) ->
% download yesterday file, to avoid time difference between server and local % download yesterday file, to avoid time difference between server and local
{Yesterday, _} = time_util:seconds_to_local_time(NowSecs - 24*60*60), {Yesterday, _} = time_util:seconds_to_local_time(NowSecs - 24*60*60),
index_download:download(self(), Yesterday), index_download:download(self(), Yesterday),
{noreply, State}; {noreply, State#state{scheduled = false}};
handle_info({sync_torrent_index, ok, FileName, Content}, State) -> handle_info({sync_torrent_index, ok, FileName, Content}, State) ->
#state{workers = Workers, work_on = WorkOn} = State, #state{scheduled = SFlag, workers = Workers, work_on = WorkOn} = State,
FullName = ?WORKDIR ++ FileName, FullName = ?WORKDIR ++ FileName,
{NewWorkOn, NewWorkers} = case lists:member(FullName, WorkOn) of {NewWorkOn, NewWorkers} = case lists:member(FullName, WorkOn) of
true -> true ->
@ -92,13 +92,14 @@ handle_info({sync_torrent_index, ok, FileName, Content}, State) ->
Pid = start_worker(FullName), Pid = start_worker(FullName),
{[FullName|WorkOn], [Pid|Workers]} {[FullName|WorkOn], [Pid|Workers]}
end, end,
schedule_update_today(), if not SFlag -> schedule_update_today(); true -> ok end,
{noreply, State#state{work_on = NewWorkOn, workers = NewWorkers}}; {noreply, State#state{scheduled = true, work_on = NewWorkOn, workers = NewWorkers}};
handle_info({sync_torrent_index, failed, FileName}, State) -> handle_info({sync_torrent_index, failed, FileName}, State) ->
?W(?FMT("today index file ~s download failed", [FileName])), ?W(?FMT("today index file ~s download failed", [FileName])),
schedule_update_today(), #state{scheduled = SFlag} = State,
{noreply, State}; if not SFlag -> schedule_update_today(); true -> ok end,
{noreply, State#state{scheduled = true}};
handle_info({worker_done, Pid, FileName}, State) -> handle_info({worker_done, Pid, FileName}, State) ->
?I(?FMT("worker ~s done", [FileName])), ?I(?FMT("worker ~s done", [FileName])),