PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 2ms app.codeStats 0ms

/src/reloader.erl

http://github.com/basho/mochiweb
Erlang | 179 lines | 96 code | 26 blank | 57 comment | 3 complexity | 2ac99b87a181d9de5d70062d9a5d4aeb MD5 | raw file
Possible License(s): MIT
  1. %% @author Matthew Dempsky <matthew@mochimedia.com>
  2. %% @copyright 2007 Mochi Media, Inc.
  3. %%
  4. %% Permission is hereby granted, free of charge, to any person obtaining a
  5. %% copy of this software and associated documentation files (the "Software"),
  6. %% to deal in the Software without restriction, including without limitation
  7. %% the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. %% and/or sell copies of the Software, and to permit persons to whom the
  9. %% Software is furnished to do so, subject to the following conditions:
  10. %%
  11. %% The above copyright notice and this permission notice shall be included in
  12. %% all copies or substantial portions of the Software.
  13. %%
  14. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. %% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. %% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. %% DEALINGS IN THE SOFTWARE.
  21. %% @doc Erlang module for automatically reloading modified modules
  22. %% during development.
  23. -module(reloader).
  24. -author("Matthew Dempsky <matthew@mochimedia.com>").
  25. -include_lib("kernel/include/file.hrl").
  26. -behaviour(gen_server).
  27. -export([start/0, start_link/0]).
  28. -export([stop/0]).
  29. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  30. -export([all_changed/0]).
  31. -export([is_changed/1]).
  32. -export([reload_modules/1]).
  33. -record(state, {last, tref}).
  34. %% External API
  35. %% @spec start() -> ServerRet
  36. %% @doc Start the reloader.
  37. start() ->
  38. gen_server:start({local, ?MODULE}, ?MODULE, [], []).
  39. %% @spec start_link() -> ServerRet
  40. %% @doc Start the reloader.
  41. start_link() ->
  42. gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  43. %% @spec stop() -> ok
  44. %% @doc Stop the reloader.
  45. stop() ->
  46. gen_server:call(?MODULE, stop).
  47. %% gen_server callbacks
  48. %% @spec init([]) -> {ok, State}
  49. %% @doc gen_server init, opens the server in an initial state.
  50. init([]) ->
  51. {ok, TRef} = timer:send_interval(timer:seconds(1), doit),
  52. {ok, #state{last = stamp(), tref = TRef}}.
  53. %% @spec handle_call(Args, From, State) -> tuple()
  54. %% @doc gen_server callback.
  55. handle_call(stop, _From, State) ->
  56. {stop, shutdown, stopped, State};
  57. handle_call(_Req, _From, State) ->
  58. {reply, {error, badrequest}, State}.
  59. %% @spec handle_cast(Cast, State) -> tuple()
  60. %% @doc gen_server callback.
  61. handle_cast(_Req, State) ->
  62. {noreply, State}.
  63. %% @spec handle_info(Info, State) -> tuple()
  64. %% @doc gen_server callback.
  65. handle_info(doit, State) ->
  66. Now = stamp(),
  67. _ = doit(State#state.last, Now),
  68. {noreply, State#state{last = Now}};
  69. handle_info(_Info, State) ->
  70. {noreply, State}.
  71. %% @spec terminate(Reason, State) -> ok
  72. %% @doc gen_server termination callback.
  73. terminate(_Reason, State) ->
  74. {ok, cancel} = timer:cancel(State#state.tref),
  75. ok.
  76. %% @spec code_change(_OldVsn, State, _Extra) -> State
  77. %% @doc gen_server code_change callback (trivial).
  78. code_change(_Vsn, State, _Extra) ->
  79. {ok, State}.
  80. %% @spec reload_modules([atom()]) -> [{module, atom()} | {error, term()}]
  81. %% @doc code:purge/1 and code:load_file/1 the given list of modules in order,
  82. %% return the results of code:load_file/1.
  83. reload_modules(Modules) ->
  84. [begin code:purge(M), code:load_file(M) end || M <- Modules].
  85. %% @spec all_changed() -> [atom()]
  86. %% @doc Return a list of beam modules that have changed.
  87. all_changed() ->
  88. [M || {M, Fn} <- code:all_loaded(), is_list(Fn), is_changed(M)].
  89. %% @spec is_changed(atom()) -> boolean()
  90. %% @doc true if the loaded module is a beam with a vsn attribute
  91. %% and does not match the on-disk beam file, returns false otherwise.
  92. is_changed(M) ->
  93. try
  94. module_vsn(M:module_info()) =/= module_vsn(code:get_object_code(M))
  95. catch _:_ ->
  96. false
  97. end.
  98. %% Internal API
  99. module_vsn({M, Beam, _Fn}) ->
  100. {ok, {M, Vsn}} = beam_lib:version(Beam),
  101. Vsn;
  102. module_vsn(L) when is_list(L) ->
  103. {_, Attrs} = lists:keyfind(attributes, 1, L),
  104. {_, Vsn} = lists:keyfind(vsn, 1, Attrs),
  105. Vsn.
  106. doit(From, To) ->
  107. [case file:read_file_info(Filename) of
  108. {ok, #file_info{mtime = Mtime}} when Mtime >= From, Mtime < To ->
  109. reload(Module);
  110. {ok, _} ->
  111. unmodified;
  112. {error, enoent} ->
  113. %% The Erlang compiler deletes existing .beam files if
  114. %% recompiling fails. Maybe it's worth spitting out a
  115. %% warning here, but I'd want to limit it to just once.
  116. gone;
  117. {error, Reason} ->
  118. io:format("Error reading ~s's file info: ~p~n",
  119. [Filename, Reason]),
  120. error
  121. end || {Module, Filename} <- code:all_loaded(), is_list(Filename)].
  122. reload(Module) ->
  123. io:format("Reloading ~p ...", [Module]),
  124. code:purge(Module),
  125. case code:load_file(Module) of
  126. {module, Module} ->
  127. io:format(" ok.~n"),
  128. case erlang:function_exported(Module, test, 0) of
  129. true ->
  130. io:format(" - Calling ~p:test() ...", [Module]),
  131. case catch Module:test() of
  132. ok ->
  133. io:format(" ok.~n"),
  134. reload;
  135. Reason ->
  136. io:format(" fail: ~p.~n", [Reason]),
  137. reload_but_test_failed
  138. end;
  139. false ->
  140. reload
  141. end;
  142. {error, Reason} ->
  143. io:format(" fail: ~p.~n", [Reason]),
  144. error
  145. end.
  146. stamp() ->
  147. erlang:localtime().
  148. %%
  149. %% Tests
  150. %%
  151. -ifdef(TEST).
  152. -include_lib("eunit/include/eunit.hrl").
  153. -endif.