1%% ``Licensed under the Apache License, Version 2.0 (the "License");
2%% you may not use this file except in compliance with the License.
3%% You may obtain a copy of 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,
9%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10%% See the License for the specific language governing permissions and
11%% limitations under the License.
12%%
13%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
14%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
15%% AB. All Rights Reserved.''
16%%
17%%     $Id: mnesia_late_loader.erl,v 1.1 2008/12/17 09:53:38 mikpe Exp $
18%%
19-module(mnesia_late_loader).
20
21-export([
22	 async_late_disc_load/3,
23	 maybe_async_late_disc_load/3,
24	 init/1,
25	 start/0
26	]).
27
28%% sys callback functions
29-export([
30	 system_continue/3,
31	 system_terminate/4,
32	 system_code_change/4
33	]).
34
35-define(SERVER_NAME, ?MODULE).
36
37-record(state, {supervisor}).
38
39async_late_disc_load(Node, Tabs, Reason) ->
40    Msg = {async_late_disc_load, Tabs, Reason},
41    catch ({?SERVER_NAME, Node} ! {self(), Msg}).
42
43maybe_async_late_disc_load(Node, Tabs, Reason) ->
44    Msg = {maybe_async_late_disc_load, Tabs, Reason},
45    catch ({?SERVER_NAME, Node} ! {self(), Msg}).
46
47start() ->
48    mnesia_monitor:start_proc(?SERVER_NAME, ?MODULE, init, [self()]).
49
50init(Parent) ->
51    %% Trap exit omitted intentionally
52    register(?SERVER_NAME, self()),
53    link(whereis(mnesia_controller)),  %% We may not hang
54    mnesia_controller:merge_schema(),
55    unlink(whereis(mnesia_controller)),
56    mnesia_lib:set(mnesia_status, running),
57    proc_lib:init_ack(Parent, {ok, self()}),
58    loop(#state{supervisor = Parent}).
59
60loop(State) ->
61    receive
62	{_From, {async_late_disc_load, Tabs, Reason}} ->
63	    mnesia_controller:schedule_late_disc_load(Tabs, Reason),
64	    loop(State);
65
66	{_From, {maybe_async_late_disc_load, Tabs, Reason}} ->
67	    GoodTabs =
68		[T || T <- Tabs,
69		      lists:member(node(),
70				   mnesia_recover:get_master_nodes(T))],
71	    mnesia_controller:schedule_late_disc_load(GoodTabs, Reason),
72	    loop(State);
73
74	{system, From, Msg} ->
75	    mnesia_lib:dbg_out("~p got {system, ~p, ~p}~n",
76			       [?SERVER_NAME, From, Msg]),
77	    Parent = State#state.supervisor,
78	    sys:handle_system_msg(Msg, From, Parent, ?MODULE, [], State);
79
80	Msg ->
81	    mnesia_lib:error("~p got unexpected message: ~p~n",
82			     [?SERVER_NAME, Msg]),
83	    loop(State)
84    end.
85
86%%%%%%%%%%%%%%%%%%%%%%%%%%%
87%% System upgrade
88
89system_continue(_Parent, _Debug, State) ->
90    loop(State).
91
92system_terminate(Reason, _Parent, _Debug, _State) ->
93    exit(Reason).
94
95system_code_change(State, _Module, _OldVsn, _Extra) ->
96    {ok, State}.
97