Skip to content

Instantly share code, notes, and snippets.

@codysoyland
Created August 23, 2011 16:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codysoyland/1165753 to your computer and use it in GitHub Desktop.
Save codysoyland/1165753 to your computer and use it in GitHub Desktop.
Riak postcommit ZeroMQ publisher
-module(riak_zmq).
-export([postcommit_publish/1, get_zmq_publisher/0, zmq_publisher/0, zmq_publisher/1]).
postcommit_publish(RObj) ->
Body = riak_object:get_value(RObj),
{ok, Pid} = get_zmq_publisher(),
Pid ! {send, Body}.
get_zmq_publisher() ->
case pg2:get_closest_pid([zmq]) of
{error, {no_such_group, _}} ->
pg2:create([zmq]),
get_zmq_publisher();
{error, {no_process, _}} ->
Pid = spawn(?MODULE, zmq_publisher, []),
pg2:join([zmq], Pid),
{ok, Pid};
Pid ->
{ok, Pid}
end.
zmq_publisher() ->
{ok, _Pid} = zmq:start_link(),
{ok, Socket} = zmq:socket(pub),
zmq:bind(Socket, "tcp://127.0.0.1:5550"),
zmq_publisher(Socket).
zmq_publisher(Socket) ->
receive
{send, Msg} -> zmq:send(Socket, Msg), zmq_publisher(Socket)
end.
@seancribbs
Copy link

This is a cool idea (and very little code), but one thought: you should probably start up the zmq publisher as a named process (gen_server, maybe) with a supervisor, rather than using pg2. It feels backwards to have the postcommit ensure that the process is running and publishing on the 0MQ socket.

@codysoyland
Copy link
Author

Is there a way I can start the named process automatically when Riak starts? I'm new to Erlang, so I wasn't sure the best way to do this. I took the pg2 idea from the Riak RabbitMQ postcommit hook (https://github.com/jbrisbin/riak-rabbitmq-commit-hooks). Any guidance here would be appreciated.

@seancribbs
Copy link

@codysoyland Yes, your postcommit hook can make sure the application (and thus its supervisor and worker processes) are started. Rejiggered your code here: https://github.com/seancribbs/riak_zmq

@codysoyland
Copy link
Author

Very cool. thanks. I was looking into how to turn it into an OTP app. Again, totally new to this (I've only dabbled with OTP).

@lalebarde
Copy link

Hi, could you explain please what this is for, for the newbbie I am. I am appealed by riak for availability on one hand, and by 0MQ for ease of use and scallability on the other hand. Is riak_zmq the glue between both ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment