Long Running Operations (Autoservcie)
Before starting long running operations, an autoservice should be started using the
autoservice_start() function. An autoservice will ensure that the user hears a continuous stream of audio while your lua code works in the background. This autoservice will automatically be stopped before executing applications and dialplan functions and will be restarted afterwards. The autoservice can be stopped using autoservice_stop() and the autoservice_status() function will return
true if an autoservice is currently running.
Defining Extensions Dynamically
Since extensions are functions in pbx_lua, any function can be used, including closures. A function can be defined that returns extension functions and used to populate the extensions table.
Creating Custom Aliases for Built-in Constructs
If you don't like the
app table being named 'app' or if you think typing 'channel' to access the
channel table is too much work, you can rename them.
Lua has a built in "print" function that outputs things to stdout, but for Asterisk, we would rather have the output go in the verbose log. To do so, we could rewrite the
Splitting Configuration into Multiple Files
require method can be used to load lua modules located in LUA_PATH. The
dofile method can be used to include any file by path name.
Using External Modules
Lua modules can be loaded using the standard
require lua method. Some of the functionality provided by various lua modules is already included in Asterisk (e.g. func_odbc provides what LuaSQL provides). It is generally better to use code built-in to Asterisk over external lua modules. Specifically, the func_odbc module uses a connection pool to provide database resources, where as with LuaSQL each channel would have to make a new connection to the database on its own.
luac program can be used to compile your
extensions.lua file into lua bytecode. This will slightly increase performance as pbx_lua will no longer need to parse
extensions.lua on load. The
luac compiler will also detect and report any syntax errors. To use
luac, rename your
extensions.lua file and then run
luac as follows.
The pbx_lua module automatically knows the difference between a lua text file and a lua bytecode file.