HttpAction.lua 2.11 KB

local skynet = require "skynet"
local sharedata = require "skynet.sharedata"
local codecache = require "skynet.codecache"  -- 清空缓存用

local _M = {}

-- 清空代码和csvdata 缓存  (无论什么热更新  都要先更新服务器 然后调用这个 api 再进行更新)
function _M.clearcache(query, body)
	skynet.error(string.format("clearcache time: %s", skynet.timex()))
	codecache.clear()
	return 'success'
end

--重新加载 csvdata  -- 需要先调用 clearcache
function _M.reload_csvdata(query, body)
	skynet.error(string.format("reload_csvdata time: %s", skynet.timex()))
	local status = skynet.call('CSVDATA', "lua", "reload")
	if status == "ok" then
		-- 所有需要 更新的服务 都更新一下数据
		pcall(skynet.call, 'WATCHDOG', "lua", "reloadCsvData")
		pcall(skynet.call, 'NGXD', "lua", "reloadCsvdb")
	end
	return 'success'
end

--post 需要更改的 globalCsv json
function _M.global_csv(query, body)
	if not body or body == "" then return end
	local change = json.decode(body) -- json 表
	if not next(change) then return end
	skynet.error(string.format("global_csv change time: %s, value: %s", skynet.timex(), body))
	-- 更新所有需要更新globalDefine 的地方
	pcall(skynet.call, 'WATCHDOG', "lua", "reloadCsvData", change)
	pcall(skynet.call, 'NGXD', "lua", "reloadCsvdb", change) -- 他也要改

	return 'success'
end


-- 热更新代码  -- 针对 agent 执行发送过来的代码 -- 代码要规范~ 

--[=[ eg:
1. *Action 重新加载 (action 每次用才require  直接清掉重新加载) (某些)
	body = "package.loaded['actions.*Action']=nil"

2. 修改 global 方法 和 变量 直接覆盖
	body = """
		function a()
			print(123)
		end
	"""

3. 修改 role 方法
	
	body = """
		local role = ...
		if role then
			role.getItemCount = function(self)
				return "hehe"
			end
		end
	"""
]=]

function _M.hotfix(query, body)
	if not body or body == "" then
		return
	end
	skynet.error(string.format("hotfix time: %s, code: %s", skynet.timex(), body))
	local ok = pcall(load, body)
	if not ok then return end
	pcall(skynet.call, 'WATCHDOG', "lua", "hotfix", body)
	return 'success'
end




return _M