/src/ssim_correlator.erl

https://github.com/fygrave/ssim · Erlang · 190 lines · 75 code · 24 blank · 91 comment · 0 complexity · caf44a76652b6a48e0dc32a10a0d31fc MD5 · raw file

  1. %%%-------------------------------------------------------------------
  2. %%% @author fygrave <fygrave@o0o.nu>
  3. %%% @copyright (C) 2010,2011, fygrave
  4. %%% @doc
  5. %%%
  6. %%% @end
  7. %%% Created : 7 Aug 2010 by fygrave <fygrave@o0o.nu>
  8. %%%-------------------------------------------------------------------
  9. -module(ssim_correlator).
  10. -behaviour(gen_server).
  11. %% API
  12. -export([start_link/0,
  13. check_status/0,
  14. statistics/0
  15. ]).
  16. %% gen_server callbacks
  17. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  18. terminate/2, code_change/3]).
  19. -define(SERVER, ?MODULE).
  20. -define (MESSAGE_KEY, <<"message">>).
  21. -record(state, {riaksock,
  22. msgcount,
  23. bucket
  24. }).
  25. -include("deps/amqp_client/include/amqp_client.hrl").
  26. %%%===================================================================
  27. %%% API
  28. %%%===================================================================
  29. %%--------------------------------------------------------------------
  30. %% @doc
  31. %% Starts the server
  32. %%
  33. %% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
  34. %% @end
  35. %%--------------------------------------------------------------------
  36. start_link() ->
  37. gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
  38. check_status() ->
  39. gen_server:call(?MODULE, {check_status}).
  40. statistics() ->
  41. gen_server:call(?MODULE, {statistics}).
  42. %%%===================================================================
  43. %%% gen_server callbacks
  44. %%%===================================================================
  45. %%--------------------------------------------------------------------
  46. %% @private
  47. %% @doc
  48. %% Initializes the server
  49. %%
  50. %% @spec init(Args) -> {ok, State} |
  51. %% {ok, State, Timeout} |
  52. %% ignore |
  53. %% {stop, Reason}
  54. %% @end
  55. %%--------------------------------------------------------------------
  56. init([]) ->
  57. io:format("Starting SSIM Correlator~n"),
  58. {ok, Params} = ssim_util:get_env(riakc_params),
  59. {ok, SPid} = riakc_pb_socket:start_link(proplists:get_value(db_hostname, Params),
  60. proplists:get_value(db_port, Params)),
  61. Bucket = proplists:get_value(data_bucket, Params),
  62. io:format("Data bucket ~s ~n", [ Bucket ]),
  63. State = #state{riaksock = SPid, msgcount = 0, bucket = proplists:get_value(data_bucket, Params)},
  64. io:format("Initial state ~p~n", [ State]),
  65. Rez = ssim_mq:subscribe_queue(?MESSAGE_KEY),
  66. io:format("Subscribed ~p~n", [ Rez]),
  67. {ok, State}.
  68. %%--------------------------------------------------------------------
  69. %% @private
  70. %% @doc
  71. %% Handling call messages
  72. %%
  73. %% @spec handle_call(Request, From, State) ->
  74. %% {reply, Reply, State} |
  75. %% {reply, Reply, State, Timeout} |
  76. %% {noreply, State} |
  77. %% {noreply, State, Timeout} |
  78. %% {stop, Reason, Reply, State} |
  79. %% {stop, Reason, State}
  80. %% @end
  81. %%--------------------------------------------------------------------
  82. handle_call({statistics}, _From, State) ->
  83. {ok, State};
  84. handle_call({check_status}, _From, State) ->
  85. {ok, State};
  86. handle_call(_Request, _From, State) ->
  87. Reply = ok,
  88. {reply, Reply, State}.
  89. %%--------------------------------------------------------------------
  90. %% @private
  91. %% @doc
  92. %% Handling cast messages
  93. %%
  94. %% @spec handle_cast(Msg, State) -> {noreply, State} |
  95. %% {noreply, State, Timeout} |
  96. %% {stop, Reason, State}
  97. %% @end
  98. %%--------------------------------------------------------------------
  99. handle_cast({up, _Node, _Services}, State) ->
  100. io:format("Cast node up~n"),
  101. {noreply, State};
  102. handle_cast({#'basic.consume_ok'{}, Tag}, State) ->
  103. {noreply, State};
  104. handle_cast({down, _Node}, State)->
  105. io:format("Cast node down~n"),
  106. {noreply, State};
  107. handle_cast(_Msg, State) ->
  108. {noreply, State}.
  109. %%--------------------------------------------------------------------
  110. %% @private
  111. %% @doc
  112. %% Handling all non call/cast messages
  113. %%
  114. %% @spec handle_info(Info, State) -> {noreply, State} |
  115. %% {noreply, State, Timeout} |
  116. %% {stop, Reason, State}
  117. %% @end
  118. %%--------------------------------------------------------------------
  119. handle_info ({nodeup, _Node}, State) ->
  120. io:format("info: Node up~n"),
  121. {noreply, State};
  122. % this really should be moved to ssim_mq
  123. handle_info({#'basic.deliver'{}, #amqp_msg{payload = Body}},State) ->
  124. store_message(binary_to_term(Body), State#state.riaksock, State#state.bucket),
  125. NewState = #state{ riaksock = State#state.riaksock, msgcount = State#state.msgcount + 1},
  126. {noreply, NewState};
  127. handle_info ({nodedown, _Node}, State) ->
  128. io:format("info: Node down~n"),
  129. {noreply, State};
  130. handle_info({#'basic.consume_ok'{}, Tag}, State)->
  131. io:format("Basic consume ok ~p~n", [ Tag]),
  132. {noreply, State};
  133. handle_info(_Info, State) ->
  134. io:format("Info ~p~n",[_Info]),
  135. {noreply, State}.
  136. %%--------------------------------------------------------------------
  137. %% @private
  138. %% @doc
  139. %% This function is called by a gen_server when it is about to
  140. %% terminate. It should be the opposite of Module:init/1 and do any
  141. %% necessary cleaning up. When it returns, the gen_server terminates
  142. %% with Reason. The return value is ignored.
  143. %%
  144. %% @spec terminate(Reason, State) -> void()
  145. %% @end
  146. %%--------------------------------------------------------------------
  147. terminate(_Reason, _State) ->
  148. ok.
  149. %%--------------------------------------------------------------------
  150. %% @private
  151. %% @doc
  152. %% Convert process state when code is changed
  153. %%
  154. %% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
  155. %% @end
  156. %%--------------------------------------------------------------------
  157. code_change(_OldVsn, State, _Extra) ->
  158. {ok, State}.
  159. %%%===================================================================
  160. %%% Internal functions
  161. %%%===================================================================
  162. store_message(Message, RiakPid, Bucket) ->
  163. io:format("Receiving message ~p ..",[Message]),
  164. Object = riakc_obj:new(Bucket, ssim_util:get_datekey(),iolist_to_binary(mochijson2:encode(Message)), <<"application/json">>),
  165. riakc_pb_socket:put(RiakPid, Object),
  166. io:format("Message stored ~p~n", [Object]).