diff options
author | Brian Cully <bjc@kublai.com> | 2008-07-28 12:41:06 -0400 |
---|---|---|
committer | Brian Cully <github.20.shmit@spamgourmet.com> | 2008-07-28 14:45:29 -0400 |
commit | 313eacd26062ce24452c569a68925fa4a317c9b4 (patch) | |
tree | 215ddb2167285718c97746318f2aa60f7f3bf9ec /src/mysqlerl_port.erl | |
parent | 0871101e0df1ae0f80fc2bdec88e1f0a320b6f01 (diff) | |
download | mysqlerl-313eacd26062ce24452c569a68925fa4a317c9b4.tar.gz mysqlerl-313eacd26062ce24452c569a68925fa4a317c9b4.zip |
Don't pass in DB connection params on CLI, but do it after process has spawned.
This is done when the port initializes, so it will work automagically
when the binary dies and can be restarted by the port supervisor.
Diffstat (limited to 'src/mysqlerl_port.erl')
-rw-r--r-- | src/mysqlerl_port.erl | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/src/mysqlerl_port.erl b/src/mysqlerl_port.erl index 3593119..74fe177 100644 --- a/src/mysqlerl_port.erl +++ b/src/mysqlerl_port.erl @@ -1,23 +1,35 @@ -module(mysqlerl_port). -author('bjc@kublai.com'). +-include("mysqlerl.hrl"). -include("mysqlerl_port.hrl"). -behavior(gen_server). --export([start_link/1]). +-export([start_link/7]). -export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2]). +-define(CONNECT_TIMEOUT, 30000). + -record(state, {ref}). -record(port_closed, {reason}). -start_link(Cmd) -> - gen_server:start_link(?MODULE, [Cmd], []). +start_link(Cmd, Host, Port, Database, User, Password, Options) -> + gen_server:start_link(?MODULE, + [Cmd, Host, Port, Database, User, Password, Options], + []). -init([Cmd]) -> +init([Cmd, Host, Port, Database, User, Password, Options]) -> process_flag(trap_exit, true), Ref = open_port({spawn, Cmd}, [{packet, 4}, binary]), + {data, ok} = send_port_cmd(Ref, #sql_connect{host = Host, + port = Port, + database = Database, + user = User, + password = Password, + options = Options}, + ?CONNECT_TIMEOUT), {ok, #state{ref = Ref}}. terminate(#port_closed{reason = Reason}, #state{ref = Ref}) -> @@ -34,27 +46,38 @@ code_change(_OldVsn, State, _Extra) -> handle_call(#req{request = {Request, Timeout}}, From, #state{ref = Ref} = State) -> - io:format("DEBUG: Sending request: ~p~n", [Request]), - port_command(Ref, term_to_binary(Request)), - receive - {Ref, {data, Res}} -> - {reply, binary_to_term(Res), State}; + case send_port_cmd(Ref, Request, Timeout) of + {data, Res} -> + {reply, Res, State}; {'EXIT', Ref, Reason} -> gen_server:reply(From, {error, connection_closed}), {stop, #port_closed{reason = Reason}, State}; + timeout -> + gen_server:reply(From, timeout), + {stop, timeout, State}; Other -> error_logger:warning_msg("Got unknown query response: ~p~n", [Other]), gen_server:reply(From, {error, connection_closed}), {stop, {unknownreply, Other}, State} - after Timeout -> - gen_server:reply(From, timeout), - {stop, timeout, State} end. + handle_cast(_Request, State) -> {noreply, State}. handle_info({'EXIT', Ref, Reason}, #state{ref = Ref} = State) -> io:format("DEBUG: Port ~p closed on ~p.~n", [Ref, State]), {stop, #port_closed{reason = Reason}, State}. + + +send_port_cmd(Ref, Request, Timeout) -> + io:format("DEBUG: Sending request: ~p~n", [Request]), + port_command(Ref, term_to_binary(Request)), + receive + {Ref, {data, Res}} -> + {data, binary_to_term(Res)}; + Other -> Other + after Timeout -> + timeout + end. |