question about postcommit hooks and patch

Bruce Lowekamp bruce.lowekamp at skype.net
Thu May 13 18:16:22 EDT 2010


I've been playing with using postcommit hooks in some code.  I
couldn't find an example, so looking at the source, I think the right
way to set one up is something like:

PHook = {struct, [ {<<"mod">>, <<?MODULE_STRING>>}, {<<"fun">>,
<<"notify_change">>}]},
RiakClient:set_bucket(<<?MY_KEY>>, [{postcommit, [PHook]}]),

Is there a better way?


Also, in debugging my hook, I found that wrapping the hook so I could
see exceptions made it much easier.  I made the following patch that I
think might be useful to others, as well:

diff -r e836ea266eca apps/riak_kv/src/riak_kv_put_fsm.erl
--- a/apps/riak_kv/src/riak_kv_put_fsm.erl	Thu May 13 17:28:01 2010 -0400
+++ b/apps/riak_kv/src/riak_kv_put_fsm.erl	Thu May 13 14:50:07 2010 -0700
@@ -314,7 +314,7 @@
 invoke_hook(precommit, Mod0, Fun0, undefined, RObj) ->
     Mod = binary_to_atom(Mod0, utf8),
     Fun = binary_to_atom(Fun0, utf8),
-    Mod:Fun(RObj);
+    wrap_hook(Mod, Fun, RObj);
 invoke_hook(precommit, undefined, undefined, JSName, RObj) ->
     case riak_kv_js_manager:blocking_dispatch({{jsfun, JSName}, RObj}) of
         {ok, <<"fail">>} ->
@@ -331,13 +331,22 @@
 invoke_hook(postcommit, Mod0, Fun0, undefined, Obj) ->
     Mod = binary_to_atom(Mod0, utf8),
     Fun = binary_to_atom(Fun0, utf8),
-    proc_lib:spawn(fun() -> Mod:Fun(Obj) end);
+    proc_lib:spawn(fun() -> wrap_hook(Mod,Fun,Obj) end);
 invoke_hook(postcommit, undefined, undefined, _JSName, _Obj) ->
     error_logger:warning_msg("Javascript post-commit hooks aren't
implemented");
 %% NOP to handle all other cases
 invoke_hook(_, _, _, _, RObj) ->
     RObj.

+wrap_hook(Mod, Fun, Obj)->
+    try Mod:Fun(Obj)
+    catch
+        EType:X ->
+            error_logger:error_msg("problem invoking hook ~p:~p ->
~p:~p~n~p~n",
+                                   [Mod, Fun, EType, X,
erlang:get_stacktrace()]),
+            fail
+    end.
+
 merge_robjs(RObjs0,AllowMult) ->
     RObjs1 = [X || X <- [riak_kv_util:obj_not_deleted(O) ||
                             O <- RObjs0], X /= undefined],



Bruce




More information about the riak-users mailing list