Generated on 2009-08-09 21:22:54 with etap 0.3.4.
| Name | Total lines | Lines of code | Total coverage | Code coverage | ||||
| couch_task_status | ?? | ?? | ?? |
|
....1 % Licensed under the Apache License, Version 2.0 (the "License"); you may not ....2 % use this file except in compliance with the License. You may obtain a copy of ....3 % the License at ....4 % ....5 % http://www.apache.org/licenses/LICENSE-2.0 ....6 % ....7 % Unless required by applicable law or agreed to in writing, software ....8 % distributed under the License is distributed on an "AS IS" BASIS, WITHOUT ....9 % WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the ...10 % License for the specific language governing permissions and limitations under ...11 % the License. ...12 ...13 -module(couch_task_status). ...14 -behaviour(gen_server). ...15 ...16 % This module allows is used to track the status of long running tasks. ...17 % Long running tasks register (add_task/3) then update their status (update/1) ...18 % and the task and status is added to tasks list. When the tracked task dies ...19 % it will be automatically removed the tracking. To get the tasks list, use the ...20 % all/0 function ...21 ...22 -export([start_link/0, stop/0]). ...23 -export([all/0, add_task/3, update/1, update/2, set_update_frequency/1]). ...24 ...25 -export([init/1, terminate/2, code_change/3]). ...26 -export([handle_call/3, handle_cast/2, handle_info/2]). ...27 ...28 -import(couch_util, [to_binary/1]). ...29 ...30 -include("couch_db.hrl"). ...31 ...32 ...33 start_link() -> ...34 gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). ...35 ...36 ...37 stop() -> ...38 gen_server:cast(?MODULE, stop). ...39 ...40 ...41 all() -> ...42 gen_server:call(?MODULE, all). ...43 ...44 ...45 add_task(Type, TaskName, StatusText) -> ...46 put(task_status_update, {{0, 0, 0}, 0}), ...47 Msg = { ...48 add_task, ...49 to_binary(Type), ...50 to_binary(TaskName), ...51 to_binary(StatusText) ...52 }, ...53 gen_server:call(?MODULE, Msg). ...54 ...55 ...56 set_update_frequency(Msecs) -> ...57 put(task_status_update, {{0, 0, 0}, Msecs * 1000}). ...58 ...59 ...60 update(StatusText) -> ...61 update("~s", [StatusText]). ...62 ...63 update(Format, Data) -> ...64 {LastUpdateTime, Frequency} = get(task_status_update), ...65 case timer:now_diff(Now = now(), LastUpdateTime) >= Frequency of ...66 true -> ...67 put(task_status_update, {Now, Frequency}), ...68 Msg = ?l2b(io_lib:format(Format, Data)), ...69 gen_server:cast(?MODULE, {update_status, self(), Msg}); ...70 false -> ...71 ok ...72 end. ...73 ...74 ...75 init([]) -> ...76 % read configuration settings and register for configuration changes ...77 ets:new(?MODULE, [ordered_set, protected, named_table]), ...78 {ok, nil}. ...79 ...80 ...81 terminate(_Reason,_State) -> ...82 ok. ...83 ...84 ...85 handle_call({add_task, Type, TaskName, StatusText}, {From, _}, Server) -> ...86 case ets:lookup(?MODULE, From) of ...87 [] -> ...88 true = ets:insert(?MODULE, {From, {Type, TaskName, StatusText}}), ...89 erlang:monitor(process, From), ...90 {reply, ok, Server}; ...91 [_] -> ...92 {reply, {add_task_error, already_registered}, Server} ...93 end; ...94 handle_call(all, _, Server) -> ...95 All = [ ...96 [ ...97 {type, Type}, ...98 {task, Task}, ...99 {status, Status}, ..100 {pid, ?l2b(pid_to_list(Pid))} ..101 ] ..102 || ..103 {Pid, {Type, Task, Status}} <- ets:tab2list(?MODULE) ..104 ], ..105 {reply, All, Server}. ..106 ..107 ..108 handle_cast({update_status, Pid, StatusText}, Server) -> ..109 [{Pid, {Type, TaskName, _StatusText}}] = ets:lookup(?MODULE, Pid), ..110 ?LOG_DEBUG("New task status for ~s: ~s",[TaskName, StatusText]), ..111 true = ets:insert(?MODULE, {Pid, {Type, TaskName, StatusText}}), ..112 {noreply, Server}; ..113 handle_cast(stop, State) -> ..114 {stop, normal, State}. ..115 ..116 handle_info({'DOWN', _MonitorRef, _Type, Pid, _Info}, Server) -> ..117 %% should we also erlang:demonitor(_MonitorRef), ? ..118 ets:delete(?MODULE, Pid), ..119 {noreply, Server}. ..120 ..121 ..122 code_change(_OldVsn, State, _Extra) -> ..123 {ok, State}. ..124
Generated using etap 0.3.4.