lua连接geraman并丢任务
auth yufei
2016-07-13
lua连接gearman
function F.unzip()
local cjson = require "cjson"
local datas = {}
local reqdata={}
local gearman = require "gearman"
local gm = gearman:new()
datas["url"] = args["url"]
datas["coid"] = args["coid"]
datas["work_id"] = args["work_id"]
datas["type"] = args["type"]
reqdata["code"] = 0
reqdata["data"] = ""
reqdata["msg"] = "ok"
reqdata["status"] = 1
local datas = cjson.encode(datas)
gm:set_timeout(1000) -- 1 sec
ngx.header.content_type = "text/plain"
local ok, err = gm:connect("gearman.yufei.com", 4730)
if not ok then
--return
reqdata["code"] = 1
reqdata["status"] = 0
reqdata["msg"] = "gearmand connection failed"
end
ok, err = gm:submit_job_high_bg("UnzipgByPy", datas)
if not ok then
--return
reqdata["code"] = 2
reqdata["status"] = 0
reqdata["msg"] = "gearmand push job failed"
else
ngx.say(ok)
end
local okey, err = gm:set_keepalive(0, 100)
--[[
if not okey then
--return
reqdata["code"] = 3
reqdata["msg"] = "gearmand keepalive failed"
end
--通过lua给swoole传数据
local swoole=require "luaswoole" --luaswoole.so
local client = swoole.new()
local err = client:connect("api.swoole.goodid.com",9090)
if err then
ngx.say(err)
return
end
local data = client:sendRecv(ok)
if data then
ngx.say(data)
end
client:close()
--]]
local ok, err = gm:close()
if not ok then
ngx.say("failed to close: ", err)
return
end
local error_log = "/data/logs/lua_unip.log"
local log_file = io.open(error_log, "a+")
if log_file then
local time = os.date("%a, %d %b %Y %X GMT")
log_file:write("\n",time,"\n")
log_file:write(ngx.var.request_uri,"\n")
log_file:write(datas,"\n")
log_file:write(cjson.encode(reqdata),"\n")
end
ngx.say(cjson.encode(reqdata))
ngx.exit(200)
end
gearman提交工作时有多种方式:
submit_job为普通的工作任务,client得到状态更新及通知任务已经完成的响应;
submit_job_bg为异步的工作任务,client不关心任务的完成情况;
submit_job_high为高优先级的工作任务;
submit_job_high_bg为高优先级的异步任务;
submit_job_low为低优先级的工作任务;
submit_job_low_bg为低优先级的异步任务。