lua.wetgenes.wiretasks
local wiretasks=require("wetgenes.wiretasks")
This is a small collection of useful tasks for running via wire. Each task has its own specific module dependencies, eg lua socket is needed for http fetching. This module is usually used in a task start code string passed to wire.tasks. So "require('wetgenes.wiretasks').http_code()" would be used to start the http task.
We also include some wrapper functions that make use of memos and require certain tasks to already be running.
lua.wetgenes.wiretasks.http_code
wire.tasks("http",4,"require('wetgenes.wiretasks').http_code()")
result = wire.memo({
fifo=wire.fifo("http"),
data={
url="http://google.com/",
},
on_result=function() end,
}):resolve()
The code we run in "http" tasks to handle http requests via memos.
This function requires the following libraries to be available.
require("socket.http")
require("ltn12")
Every memo sent is a http fetch request, the keys set in memo.data control the type of request.
data.url = "http://google.com/"
The url we will be talking to. This is all you need for a basic GET.
data.get = { a=1 , b=2 }
If this exists then we send a GET request with this table in the query string, eg append "?a=1&b=2" to the url.
data.json = { a=1 , b=2 }
If this exists then we encode as json and send as body of a POST also setting "Content-Type" to "application/json" in data.headers
data.post = { a=1 , b=2 }
If this exists then we encode as a query string and send as body of a POST also setting "Content-Type" to "application/x-www-form-urlencoded" in data.headers
data.body = "a=1&b=2"
Force a request body string.
data.method = "GET"
Force a request method.
data.headers = { ["Content-Type"]="application/json" , ... }
Force extra headers.
result.code
result.status
result.headers
result.body
The result of the http.request, may also raise an error for catastrophic failures, eg host not found.
Many things can go wrong so always check that result.fail is not set and the result.code is as expected ( eg 200 ) and only then is it safe to read the result.body
lua.wetgenes.wiretasks.sqlite_code
wire.tasks("sqlite",1,"require('wetgenes.wiretasks').sqlite_code()",{
sqlite_filename = "./test.sql" ,
sqlite_pragmas = "" ,
})
result = wire.memo({
fifo=wire.fifo("sqlite"),
data={
sql="SELECT name FROM sqlite_master WHERE type='table';",
},
on_result=function() end,
}):resolve()
The code we run in "sqlite" tasks to handle sqlite requests via memos.
This function requires the following libraries to be available.
require("lsqlite3")
As we are opening an sqlite database here it probably wont help you much to have more than one thread per database as they will just fight over file access. To open multiple sqlite databases create multiple tasks with different names.
Globals passed into the thread control the startup actions, primarily you should provide an sqlite_filename to open.
sqlite_filename="./test.sql"
The sqlite database to open when this thread starts.
Most memos are simple SQL queries, but some extra commands exist via data.action
data.action="close"
Shut down the sqlite database, this combined with waiting for the thread to halt allows sqlite to clean up before exiting the process.
When data.action is not set we are performing an SQL query.
data.sql="SELECT name FROM sqlite_master WHERE type='table';",
Will return result.rows filled with data.
data.compact=true
If this boolean is set then we will return compact rows, data.rows will contain rows that are arrays and data.names will be an array of column names. If not set then we return rows as [name]=value objects.
data.binds={ name=value , ... }
Bind these values to an sqlite statement.
data.blobs={ name=value , ... }
Bind these values to an sqlite statement as blobs. This must be used
for sending binary data.