├── .gitignore ├── README.md ├── rebar.config └── src ├── riak_zmq.app.src ├── riak_zmq.erl ├── riak_zmq_app.erl ├── riak_zmq_publisher.erl └── riak_zmq_sup.erl /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *#* 3 | ebin 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # riak_zmq 2 | 3 | This is a small OTP application, based on 4 | [this gist](https://gist.github.com/1165753) that provides a 5 | [Riak KV](https://github.com/basho/riak_kv) postcommit hook for 6 | publishing updates to [0MQ](http://zeromq.org). 7 | 8 | ## Status 9 | 10 | This is meant to be a proof-of-concept only. It is neither tested nor 11 | otherwise guaranteed to work. Thanks to 12 | [@codysoyland](https://gist.github.com/codysoyland) for the initial 13 | code. 14 | -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | %% Normally I would put this in, but erlzmq is not rebar-compatible. 2 | %% Might be better to rewrite the code to use erlzmq2. 3 | %% {deps, [{zmq, ".*", {git, "git://github.com/zeromq/erlzmq.git", {branch, "master"}}}]}. 4 | -------------------------------------------------------------------------------- /src/riak_zmq.app.src: -------------------------------------------------------------------------------- 1 | {application, riak_zmq, 2 | [ 3 | {description, "Riak postcommit hook that publishes to ZeroMQ. Based on https://gist.github.com/1165753"}, 4 | {vsn, "1"}, 5 | {registered, [riak_zmq_publisher]}, 6 | {applications, [ 7 | kernel, 8 | stdlib, 9 | riak_core, 10 | riak_kv, 11 | zmq 12 | ]}, 13 | {mod, { riak_zmq_app, []}}, 14 | {env, [ 15 | {zmq_uri, "tcp://127.0.0.1:5500"} 16 | ]} 17 | ]}. 18 | -------------------------------------------------------------------------------- /src/riak_zmq.erl: -------------------------------------------------------------------------------- 1 | -module(riak_zmq). 2 | -export([postcommit/1]). 3 | 4 | postcommit(RObj) -> 5 | riak_zmq_app:ensure_started(riak_zmq), 6 | Body = riak_object:get_value(RObj), 7 | riak_zmq_publisher:publish(Body). 8 | -------------------------------------------------------------------------------- /src/riak_zmq_app.erl: -------------------------------------------------------------------------------- 1 | -module(riak_zmq_app). 2 | 3 | -behaviour(application). 4 | 5 | %% Application callbacks 6 | -export([start/2, stop/1]). 7 | 8 | -export([ensure_started/1]). 9 | %% =================================================================== 10 | %% Application callbacks 11 | %% =================================================================== 12 | 13 | start(_StartType, _StartArgs) -> 14 | ensure_started(zmq), 15 | riak_zmq_sup:start_link(). 16 | 17 | stop(_State) -> 18 | ok. 19 | 20 | ensure_started(App) -> 21 | case application:start(App) of 22 | ok -> 23 | ok; 24 | {error, {already_started, App}} -> 25 | ok 26 | end. 27 | -------------------------------------------------------------------------------- /src/riak_zmq_publisher.erl: -------------------------------------------------------------------------------- 1 | -module(riak_zmq_publisher). 2 | -behaviour(gen_server). 3 | 4 | %% ------------------------------------------------------------------ 5 | %% API Function Exports 6 | %% ------------------------------------------------------------------ 7 | 8 | -export([start_link/0, publish/1]). 9 | 10 | %% ------------------------------------------------------------------ 11 | %% gen_server Function Exports 12 | %% ------------------------------------------------------------------ 13 | 14 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, 15 | terminate/2, code_change/3]). 16 | 17 | %% ------------------------------------------------------------------ 18 | %% API Function Definitions 19 | %% ------------------------------------------------------------------ 20 | 21 | start_link() -> 22 | gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). 23 | 24 | publish(Msg) -> 25 | gen_server:call({local, ?MODULE}, {publish, Msg}). 26 | %% ------------------------------------------------------------------ 27 | %% gen_server Function Definitions 28 | %% ------------------------------------------------------------------ 29 | 30 | init([]) -> 31 | Socket = zmq:socket(pub), 32 | zmq:bind(Socket, app_helper:get_env(riak_zmq, zmq_uri, "tcp://127.0.0.1:5500")), 33 | {ok, Socket}. 34 | 35 | handle_call({publish, Msg}, _From, Socket) -> 36 | zmq:send(Socket, Msg), 37 | {reply, ok, Socket}; 38 | handle_call(_Request, _From, State) -> 39 | {noreply, ok, State}. 40 | 41 | handle_cast(_Msg, State) -> 42 | {noreply, State}. 43 | 44 | handle_info(_Info, State) -> 45 | {noreply, State}. 46 | 47 | terminate(_Reason, _State) -> 48 | ok. 49 | 50 | code_change(_OldVsn, State, _Extra) -> 51 | {ok, State}. 52 | 53 | %% ------------------------------------------------------------------ 54 | %% Internal Function Definitions 55 | %% ------------------------------------------------------------------ 56 | 57 | -------------------------------------------------------------------------------- /src/riak_zmq_sup.erl: -------------------------------------------------------------------------------- 1 | -module(riak_zmq_sup). 2 | 3 | -behaviour(supervisor). 4 | 5 | %% API 6 | -export([start_link/0]). 7 | 8 | %% Supervisor callbacks 9 | -export([init/1]). 10 | 11 | %% Helper macro for declaring children of supervisor 12 | -define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}). 13 | 14 | %% =================================================================== 15 | %% API functions 16 | %% =================================================================== 17 | 18 | start_link() -> 19 | supervisor:start_link({local, ?MODULE}, ?MODULE, []). 20 | 21 | %% =================================================================== 22 | %% Supervisor callbacks 23 | %% =================================================================== 24 | 25 | init([]) -> 26 | {ok, { {one_for_one, 5, 10}, [ 27 | ?CHILD(zmq, worker), 28 | ?CHILD(riak_zmq_publisher, worker) 29 | ]}}. 30 | 31 | --------------------------------------------------------------------------------