ODBC connection in http-bind

Hello,
I want to add feature to mod_http_bind which execute sql query when http-bind session will timeout.
I took run_sql_query function from mod_archive_odbc and add to ejabberd_http_bind.erl, then call it (simplest query):

handle_info({timeout, Timer, _}, _StateName,
    #state{id=SID, timer = Timer} = StateData) ->
    ?INFO_MSG("Session timeout. Closing the HTTP bind session: ~p", [SID]),
    run_sql_query(["SELECT VERSION()"]),
    {stop, normal, StateData};

But i've got error:

Supervisor: {local,
                                           'ejabberd_mod_http_bind_domain.com'}
     Context:    child_terminated
     Reason:     {{badrecord,state},
                  [{ejabberd_odbc,sql_query_internal,1},
                   {ejabberd_odbc,sql_query_t,1},
                   {ejabberd_http_bind,run_sql_query,1},
                   {ejabberd_http_bind,init,1},
                   {gen_fsm,init_it,6},
                   {proc_lib,init_p_do_apply,3}]}
     Offender:   [{pid,<0.568.0>},
                  {name,undefined},
                  {mfargs,{ejabberd_http_bind,start_link,undefined}},
                  {restart_type,temporary},
                  {shutdown,brutal_kill},
                  {child_type,worker}]

And I don't know what i do wrong... Maybe any connection initialization? I have configured ODBC Server in ejabberd.cfg and it works with mod_archive_odbc.
Please help.

I think that the function

I think that the function call needs to be inside a transaction, something like this:

handle_info({timeout, Timer, _}, _StateName,
    #state{id=SID, timer = Timer} = StateData) ->
    ?INFO_MSG("Session timeout. Closing the HTTP bind session: ~p", [SID]),
    %% This picks the first vhost defined in 'hosts' in ejabberd.cfg
    LServer = ?MYNAME,
    F = fun() ->
                run_sql_query(["SELECT VERSION()"])
        end,
    Res = run_sql_transaction(LServer, F).
    ?INFO_MSG("Result of my SQL query: ~p", [Res]),
    {stop, normal, StateData};

Thanks, it works, but I have

Thanks, it works, but I have idea, to put this functionality (db query) to different module.
So I want to execute query, from other module when the session timeouts.
Can I do this by any callback (in gen_fsm?) or other way ?

rysi3k wrote: Thanks, it

rysi3k wrote:

Thanks, it works, but I have idea, to put this functionality (db query) to different module.
So I want to execute query, from other module when the session timeouts.
Can I do this by any callback (in gen_fsm?) or other way ?

The only way I can think is that, in ejabberd_http_bind.erl you call
yourmod:run_sql_query(["SELECT VERSION()"])

And in yourmod.erl you define and export the function.

Syndicate content