Permalink
Browse files

Engine dumping WIP

  • Loading branch information...
1 parent 02ca254 commit 0061bbae18a952c247dd1b94de593258323ac153 Yurii Rashkovskii committed Apr 24, 2012
@@ -2,7 +2,7 @@
[
{lager, ".*", {git, "https://github.com/basho/lager.git", {branch, "master"}}},
{esupervisor, ".*", {git, "https://github.com/spawngrid/esupervisor.git", {branch, "master"}}},
- {seresye, ".*", {git, "https://github.com/spawngrid/seresye.git", {branch, "alterline"}}},
+ {seresye, ".*", {git, "https://github.com/afiniate/seresye.git", {branch, "master"}}},
{getopt, ".*", {git, "https://github.com/jcomellas/getopt.git", {branch, "master"}}},
{dynamic_compile, ".*", {git, "https://github.com/spawngrid/dynamic_compile", {branch, "master"}}},
{erlydtl, ".*", {git, "https://github.com/evanmiller/erlydtl.git", {branch, "master"}}},
@@ -19,7 +19,8 @@
]},
{mod, { htoad_app, []}},
{env, [
- {trace, [{htoad_trace_ets, []}]},
+ {engine_dump, "data/engine"},
+ {trace, [{htoad_trace_ets, []}, {htoad_engine_dump, []}]},
{modules,
[
htoad_base,
@@ -0,0 +1,109 @@
+-module(htoad_engine_dump).
+
+-behaviour(gen_event).
+
+%% gen_event callbacks
+-export([init/1, handle_event/2, handle_call/2,
+ handle_info/2, terminate/2, code_change/3]).
+
+-define(SERVER, ?MODULE).
+
+-record(state, { filename }).
+
+%%%===================================================================
+%%% gen_event callbacks
+%%%===================================================================
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Whenever a new event handler is added to an event manager,
+%% this function is called to initialize the event handler.
+%%
+%% @spec init(Args) -> {ok, State}
+%% @end
+%%--------------------------------------------------------------------
+init([]) ->
+ {ok, EngineDump} = application:get_env(htoad, engine_dump),
+ Filename = EngineDump ++ "." ++ atom_to_list(node()),
+ {ok, #state{ filename = Filename }}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Whenever an event manager receives an event sent using
+%% gen_event:notify/2 or gen_event:sync_notify/2, this function is
+%% called for each installed event handler to handle the event.
+%%
+%% @spec handle_event(Event, State) ->
+%% {ok, State} |
+%% {swap_handler, Args1, State1, Mod2, Args2} |
+%% remove_handler
+%% @end
+%%--------------------------------------------------------------------
+handle_event({rule, Engine, _Fun, _Args}, #state{ filename = Filename } = State) ->
+ Bin = term_to_binary(seresye_engine:serialize(Engine)),
+ file:write_file(Filename, Bin),
+ {ok, State};
+handle_event(_, #state{} = State) ->
+ {ok, State}.
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Whenever an event manager receives a request sent using
+%% gen_event:call/3,4, this function is called for the specified
+%% event handler to handle the request.
+%%
+%% @spec handle_call(Request, State) ->
+%% {ok, Reply, State} |
+%% {swap_handler, Reply, Args1, State1, Mod2, Args2} |
+%% {remove_handler, Reply}
+%% @end
+%%--------------------------------------------------------------------
+handle_call(_Request, State) ->
+ Reply = ok,
+ {ok, Reply, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% This function is called for each installed event handler when
+%% an event manager receives any other message than an event or a
+%% synchronous request (or a system message).
+%%
+%% @spec handle_info(Info, State) ->
+%% {ok, State} |
+%% {swap_handler, Args1, State1, Mod2, Args2} |
+%% remove_handler
+%% @end
+%%--------------------------------------------------------------------
+handle_info(_Info, State) ->
+ {ok, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Whenever an event handler is deleted from an event manager, this
+%% function is called. It should be the opposite of Module:init/1 and
+%% do any necessary cleaning up.
+%%
+%% @spec terminate(Reason, State) -> void()
+%% @end
+%%--------------------------------------------------------------------
+terminate(_Reason, _State) ->
+ ok.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Convert process state when code is changed
+%%
+%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
+%% @end
+%%--------------------------------------------------------------------
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================
@@ -18,7 +18,16 @@ start_link(Args) ->
esupervisor:start_link({local, ?MODULE}, ?MODULE, [Args]).
start_seresye() ->
- {ok, Pid} = seresye:start(?ENGINE),
+ {ok, EngineDump} = application:get_env(htoad, engine_dump),
+ EngineFilename = EngineDump ++ "." ++ atom_to_list(node()),
+ case filelib:is_regular(EngineFilename) of
+ true ->
+ lager:debug("Restoring engine from ~s", [EngineFilename]),
+ {ok, B} = file:read_file(EngineFilename),
+ {ok, Pid} = seresye:start(?ENGINE, binary_to_term(B));
+ false ->
+ {ok, Pid} = seresye:start(?ENGINE)
+ end,
seresye:set_hooks(?ENGINE,[{before_rule, fun htoad_trace:rule/3}]),
{ok, Pid}.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,6 @@
+{application,getopt,
+ [{description,"Command-line options parser for Erlang"},
+ {vsn,"0.4.3"},
+ {modules,[getopt]},
+ {registered,[]},
+ {applications,[kernel,stdlib]}]}.
@@ -102,7 +102,7 @@ start_link(Name) when is_atom(Name) ->
start_link(ClientState) when not is_atom(ClientState) ->
gen_server:start_link(?MODULE, [ClientState], []).
-start_link(ClientState, Name) when is_atom(Name) ->
+start_link(Name, ClientState) when is_atom(Name) ->
gen_server:start_link({local, Name}, ?MODULE, [ClientState], []).
View
@@ -26,6 +26,7 @@
{overlay, [
{mkdir, "log/sasl"},
+ {mkdir, "data"},
{copy, "files/erl", "\{\{erts_vsn\}\}/bin/erl"},
{copy, "files/nodetool", "\{\{erts_vsn\}\}/bin/nodetool"},
{copy, "files/htoad", "bin/htoad"},

0 comments on commit 0061bba

Please sign in to comment.