reload.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. local skynet = require "skynet"
  2. local codecache = require "skynet.codecache"
  3. require "skynet.manager"
  4. local logger = require "logger"
  5. local stringify = require "stringify"
  6. local cjson = require "cjson"
  7. local md5 = require "md5"
  8. local assetcenter
  9. local watchdog
  10. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  11. -- Generate request data
  12. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  13. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  14. local whitelist = {
  15. ["192.168.1.23"] = true,
  16. ["192.168.1.127"] = true,
  17. ["14.29.136.211"] = true,
  18. ["192.168.1.41"] = true,
  19. ["222.212.88.4"] = true,
  20. }
  21. local updateserver
  22. local function newserver()
  23. local newnum = {}
  24. table.insert(newnum,{server = skynet.localname(".activitytime"),configname = {"ActivityConfig_proto"}}) -- 活动
  25. return newnum
  26. end
  27. local function hotfix_true(hotfixlist, configname)
  28. if hotfixlist then
  29. if hotfixlist == "*" then return 1 end
  30. for k, v in pairs(configname) do
  31. if hotfixlist[v] then
  32. logger.trace("_______________________热更配置:%s,更新服务", v)
  33. return 1
  34. end
  35. end
  36. end
  37. return
  38. end
  39. -- 对需要更新配置的服务进行更新
  40. local function updateconfig(hotfixlist)
  41. updateserver = updateserver or newserver()
  42. if next(updateserver) then
  43. for k, v in pairs(updateserver) do
  44. if v.configname and hotfix_true(hotfixlist, v.configname) then
  45. skynet.send(v.server, "lua", "hotfix", hotfixlist)
  46. end
  47. end
  48. else
  49. logger.trace("_______________________热更配置无服务需要")
  50. end
  51. end
  52. --示例1: http://192.168.1.102:9002/reload?code=xxx&ntype=1
  53. --示例2: http://192.168.1.102:9002/reload?code=xxx&ntype=2&vjson=["award_proto","item_proto"]
  54. local function reload(args, ipaddr, header)
  55. logger.warn("处理来自主机 %s 的hotfix热更请求,:%s", ipaddr, args.vjson)
  56. if not whitelist[ipaddr] then
  57. -- return { "403 - Forbidden" }
  58. -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
  59. end
  60. -- 验证gm账号
  61. local code = string.sub(args.code, 1, 16)
  62. -- if code ~= content.sign then
  63. -- return cjson.encode({state = 403, msg = "账号或密码错误"})
  64. -- end
  65. local ntype = tonumber(args.ntype)
  66. -- 热更配置文件
  67. if ntype == 2 then
  68. logger.warn("更新配置文件:%s", args.vjson)
  69. local hotfixlist
  70. assetcenter = assetcenter or skynet.localname(".assetcenter")
  71. local error
  72. if args.vjson == "*" then
  73. error = skynet.call(assetcenter, "lua", "hotfix")
  74. hotfixlist = "*"
  75. elseif args.vjson then
  76. hotfixlist = {}
  77. local entity=cjson.decode(args.vjson)
  78. for k, v in pairs(entity) do
  79. logger.warn("更新配置文件:%s", v)
  80. error = skynet.call(assetcenter, "lua", "hotfix", v)
  81. hotfixlist[v] = 1
  82. end
  83. else
  84. return cjson.encode({state = 400, msg = "缺少配置表参数"})
  85. end
  86. if error then
  87. return cjson.encode({state = 400,msg = "读取文件失败:" .. error})
  88. end
  89. updateconfig(hotfixlist)
  90. -- 热更源代码
  91. elseif ntype == 1 then
  92. codecache.clear()
  93. else
  94. return cjson.encode({state =400, msg = "错误的ntype"})
  95. end
  96. -- 通知 watchdog, 放弃 agent 池中的失效对象
  97. watchdog = watchdog or skynet.localname(".ws_watchdog")
  98. skynet.call(watchdog, "lua", "hotfix")
  99. logger.warn("hotfix (%s, %s)", args.code, ipaddr)
  100. return cjson.encode({state = 0, msg = "热更新 成功"})
  101. end
  102. return reload