Blame view

src/actions/HttpAction.lua 2.11 KB
3fe4471e   zhouhaihai   热更新 demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  
  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