app_lua and sqlops returns error

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

app_lua and sqlops returns error

Sebastian Damm-2
Hi,

I'm developing some routing logic with Kamailio 5.0.1 and app_lua.
I've integrated my lua script and now I'm trying to use sqlops within
the script. I want to use the reconnect stuff of sqlops instead of
implementing it by myself.

This is what the Kamailio config looks like:
[..]
loadmodule "db_mysql.so"
loadmodule "sqlops.so"
loadmodule "app_lua.so"
[..]
modparam("sqlops", "sqlcon", "ca=>mysql://kamailio:foobar@127.0.0.1/kamailio")

modparam("app_lua", "reload", 1)
modparam("app_lua", "register", "sqlops")
modparam("app_lua", "load", "/etc/kamailio/playground.lua")

####### Routing Logic ########


# Main SIP request routing logic
request_route {
        route(everythinginlua);
}

onreply_route {
        route(everythinginlua);
}

route[everythinginlua] {
        if(!lua_run("handle_packet")) {
                xlog("L_ERR", "SCRIPT: failed to execute lua function!\n");
        }

        drop;
        exit;
}

The lua script looks like this:

-- Start of script
function handle_packet()
  sr.log("L_INFO", "Got packet method "..sr.pv.get("$rm"))
  local status = sr.sqlops.sql_query("ca", "SELECT * FROM table", "fooresult")
  sr.err("sql query returned status "..status)
end
-- End of script

Now when a packet comes in, I see the following in the log file:

Jun 14 11:19:59 busch /usr/sbin/kamailio[13254]: ERROR: app_lua
[app_lua_sr.c:104]: lua_sr_log(): Got packet method REGISTER
Jun 14 11:19:59 busch /usr/sbin/kamailio[13254]: ERROR: app_lua
[app_lua_api.c:713]: app_lua_run_ex(): error from Lua:
/etc/kamailio/playground.lua:4: attempt to call field 'sql_query' (a
nil value)
Jun 14 11:19:59 busch /usr/sbin/kamailio[13254]: ERROR: app_lua
[app_lua_api.c:726]: app_lua_run_ex(): error executing: handle_packet
(err: 2)
Jun 14 11:19:59 busch /usr/sbin/kamailio[13254]: ERROR: <script>:
SCRIPT: failed to execute lua function!

Looks to me as if the functions of sqlops don't get exported to my lua script.

According to the documentation, after specifying the "register"
modparam, I should be able to access those functions.

What am I missing?

Thanks in advance
Sebastian

_______________________________________________
Kamailio (SER) - Users Mailing List
[hidden email]
https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
Reply | Threaded
Open this post in threaded view
|

Re: app_lua and sqlops returns error

Daniel-Constantin Mierla-6
Hello,


On 14.06.17 11:25, Sebastian Damm wrote:

> Hi,
>
> I'm developing some routing logic with Kamailio 5.0.1 and app_lua.
> I've integrated my lua script and now I'm trying to use sqlops within
> the script. I want to use the reconnect stuff of sqlops instead of
> implementing it by myself.
>
> This is what the Kamailio config looks like:
> [..]
> loadmodule "db_mysql.so"
> loadmodule "sqlops.so"
> loadmodule "app_lua.so"
> [..]
> modparam("sqlops", "sqlcon", "ca=>mysql://kamailio:foobar@127.0.0.1/kamailio")
>
> modparam("app_lua", "reload", 1)
> modparam("app_lua", "register", "sqlops")
> modparam("app_lua", "load", "/etc/kamailio/playground.lua")
>
> ####### Routing Logic ########
>
>
> # Main SIP request routing logic
> request_route {
>         route(everythinginlua);
> }
>
> onreply_route {
>         route(everythinginlua);
> }
>
> route[everythinginlua] {
>         if(!lua_run("handle_packet")) {
>                 xlog("L_ERR", "SCRIPT: failed to execute lua function!\n");
>         }
>
>         drop;
>         exit;
> }
>
> The lua script looks like this:
>
> -- Start of script
> function handle_packet()
>   sr.log("L_INFO", "Got packet method "..sr.pv.get("$rm"))
>   local status = sr.sqlops.sql_query("ca", "SELECT * FROM table", "fooresult")
>   sr.err("sql query returned status "..status)
> end
> -- End of script
>
> Now when a packet comes in, I see the following in the log file:
>
> Jun 14 11:19:59 busch /usr/sbin/kamailio[13254]: ERROR: app_lua
> [app_lua_sr.c:104]: lua_sr_log(): Got packet method REGISTER
> Jun 14 11:19:59 busch /usr/sbin/kamailio[13254]: ERROR: app_lua
> [app_lua_api.c:713]: app_lua_run_ex(): error from Lua:
> /etc/kamailio/playground.lua:4: attempt to call field 'sql_query' (a
> nil value)
> Jun 14 11:19:59 busch /usr/sbin/kamailio[13254]: ERROR: app_lua
> [app_lua_api.c:726]: app_lua_run_ex(): error executing: handle_packet
> (err: 2)
> Jun 14 11:19:59 busch /usr/sbin/kamailio[13254]: ERROR: <script>:
> SCRIPT: failed to execute lua function!
>
> Looks to me as if the functions of sqlops don't get exported to my lua script.
>
> According to the documentation, after specifying the "register"
> modparam, I should be able to access those functions.
>
> What am I missing?
>
>
according to the code, the function is not exported as sql_query(), but
query():

  -
https://github.com/kamailio/kamailio/blob/master/src/modules/app_lua/app_lua_exp.c#L989

With the old export system, everything was done by the developer inside
the app_lua (interpreter module), each deciding the names of exports.
With kemi framework, the name is decided in the exporting module, not in
the interpreter module and the list of exported functions can be seen
via a rpc command:

  -
https://www.kamailio.org/docs/modules/devel/modules/app_lua.html#app_lua.r.api_list

Cheers,
Daniel

--
Daniel-Constantin Mierla
www.twitter.com/miconda -- www.linkedin.com/in/miconda
Kamailio Advanced Training - www.asipto.com
Kamailio World Conference - www.kamailioworld.com


_______________________________________________
Kamailio (SER) - Users Mailing List
[hidden email]
https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
Reply | Threaded
Open this post in threaded view
|

Re: app_lua and sqlops returns error

Sebastian Damm-2
Hi Daniel,

thanks for the reply. I just set up a test configuration for KEMI, and
it does work when loading an external lua script. However, it would be
nice if I could write my 5 lines of glue code (I have written a class
for all my functions) within my kamailio.cfg. Is this somehow
possible?

Also, when loading app_lua with KEMI and then looking into the
functions list via kamcmd, I don't see my functions exported from
sqlops. Are they only available when using the "old way"?

Best Regards,
Sebastian

_______________________________________________
Kamailio (SER) - Users Mailing List
[hidden email]
https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
Reply | Threaded
Open this post in threaded view
|

Re: app_lua and sqlops returns error

Daniel-Constantin Mierla-6
Hello,


On 19.06.17 17:41, Sebastian Damm wrote:
> Hi Daniel,
>
> thanks for the reply. I just set up a test configuration for KEMI, and
> it does work when loading an external lua script. However, it would be
> nice if I could write my 5 lines of glue code (I have written a class
> for all my functions) within my kamailio.cfg. Is this somehow
> possible?
I don't really get what you mean here, can you provide more
details/examples?

>
> Also, when loading app_lua with KEMI and then looking into the
> functions list via kamcmd, I don't see my functions exported from
> sqlops. Are they only available when using the "old way"?
>
The functions exported in the old way (not vi Kemi), are not listed in
the rpc command response. If you try with master branch, sqlops has its
functions also exported via Kemi and they will be listed via rpc.

Cheers,
Daniel

--
Daniel-Constantin Mierla
www.twitter.com/miconda -- www.linkedin.com/in/miconda
Kamailio Advanced Training - www.asipto.com
Kamailio World Conference - www.kamailioworld.com


_______________________________________________
Kamailio (SER) - Users Mailing List
[hidden email]
https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
Reply | Threaded
Open this post in threaded view
|

Re: app_lua and sqlops returns error

Sebastian Damm-2
Hi,

On Tue, Jun 20, 2017 at 1:15 PM, Daniel-Constantin Mierla
<[hidden email]> wrote:
>> thanks for the reply. I just set up a test configuration for KEMI, and
>> it does work when loading an external lua script. However, it would be
>> nice if I could write my 5 lines of glue code (I have written a class
>> for all my functions) within my kamailio.cfg. Is this somehow
>> possible?
> I don't really get what you mean here, can you provide more
> details/examples?

I start my kamailio.cfg with the usual lines:

listen=1.2.3.4:5060

loadmodule "lm.so"
loadmodule "app_lua.so"

modparam [..]

Now in the examples in the modparam of kemi and app_lua a lua file
gets loaded there. And after setting

cfgengine=lua

the kamailio.cfg ends. Everything else is done in this second file.

I was just wondering whether it would be possible to include the
content of the second file into the kamailio.cfg file, so that I have
only one configuration file. So after switching to lua as config
language, I would go on writing something like this directly into
kamailio.cfg:

foo = require "foo"

function ksr_request_route()
    [..]
end

Is this possible? Or does the design of kemi explicitly demands a
second file to be loaded?

> The functions exported in the old way (not vi Kemi), are not listed in
> the rpc command response. If you try with master branch, sqlops has its
> functions also exported via Kemi and they will be listed via rpc.

I know that I can't find the old way functions via rpc. But what I
found out later was that sqlops doesn't export to kemi in 5.0.2.

Are there already plans on abandoning the old way of including lua
files? Or will both ways be available for quite a while? If I'm
setting up a new system, I don't want something I have to completely
redesign in the near future.

Best Regards,
Sebastian

_______________________________________________
Kamailio (SER) - Users Mailing List
[hidden email]
https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users