Skip to content
This repository
tree: 74cbf2580d
Fetching contributors…

Cannot retrieve contributors at this time

file 81 lines (76 sloc) 3.197 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
%% Copyright (c) 2010 Jacob Vorreuter <jacob.vorreuter@gmail.com>
%%
%% Permission is hereby granted, free of charge, to any person
%% obtaining a copy of this software and associated documentation
%% files (the "Software"), to deal in the Software without
%% restriction, including without limitation the rights to use,
%% copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following
%% conditions:
%%
%% The above copyright notice and this permission notice shall be
%% included in all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
%% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
%% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
%% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
%% OTHER DEALINGS IN THE SOFTWARE.
-module(logplex_redis_writer).
-export([start_link/2, init/3, loop/3]).

-include("logplex.hrl").
-include("logplex_logging.hrl").

%% API functions
start_link(BufferPid, RedisOpts) ->
    ?INFO("at=start_link redis_opts=~p~n", [RedisOpts]),
    proc_lib:start_link(?MODULE, init, [self(), BufferPid, RedisOpts], 5000).

init(Parent, BufferPid, RedisOpts) ->
    ?INFO("at=init buffer_pid=~p parent=~p~n", [BufferPid, Parent]),
    logplex_queue:register(BufferPid, self()),
    case open_socket(RedisOpts) of
        {error, Err} ->
            timer:sleep(500),
            exit({error, Err});
        {ok, Socket} when is_port(Socket) ->
            proc_lib:init_ack(Parent, {ok, self()}),
            loop(BufferPid, Socket, RedisOpts)
    end.

loop(BufferPid, Socket, RedisOpts) ->
    %% verify that writer still has an open
    %% connection to redis server
    case gen_tcp:recv(Socket, 0, 0) of
        {ok, _} -> ok;
        {error, timeout} -> ok;
        {error, closed} ->
            ?INFO("event=recv result=closed", []),
            exit({error, closed})
    end,
    case catch logplex_queue:out(BufferPid, 100) of
        {'EXIT', {noproc, _}} ->
            exit(normal);
        timeout -> ok;
        {NumItems, Logs} ->
            case gen_tcp:send(Socket, Logs) of
                ok ->
                    logplex_stats:incr(message_processed, NumItems),
                    logplex_realtime:incr(message_processed, NumItems);
                {error, closed} ->
                    ?INFO("event=send result=closed", []),
                    timer:sleep(500),
                    exit(normal);
                Err ->
                    ?INFO("event=send result=~p", [Err]),
                    exit(normal)
            end
    end,
    receive stop -> exit(normal) after 0 -> ok end,
    ?MODULE:loop(BufferPid, Socket, RedisOpts).

open_socket(Opts) ->
    Ip = proplists:get_value(ip, Opts),
    Port = proplists:get_value(port, Opts),
    Pass = proplists:get_value(pass, Opts),
    redis:connect(Ip, Port, Pass).
Something went wrong with that request. Please try again.