hotfix.lua 3.6 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 assetcenter
  8. local watchdog
  9. local authz = {
  10. -- yytx = "lee@YY-Games.520"
  11. lee = "yytx-Games-9527"
  12. }
  13. local updateserver
  14. local function newserver()
  15. local newnum = {}
  16. table.insert(newnum,{server=skynet.localname(".span_roulette"),configname={"tabledata_proto","tablecrossgroup_proto","tableshop_proto","bonusturntable_proto"}})--奖金轮盘
  17. table.insert(newnum,{server=skynet.localname(".span_common_service"),model_name ='cross_elf_master',configname={"dsparameter_proto","dsfight_proto","dsmonster_proto","dsvar_proto",'dsaward_proto','dscrossgroup_proto'}})--跨服精灵大师
  18. return newnum
  19. end
  20. local function hotfix_true(hotfixlist,configname)
  21. if hotfixlist then
  22. if hotfixlist == "*" then
  23. return 1
  24. end
  25. for k, v in pairs(configname) do
  26. if hotfixlist[v] then
  27. logger.trace("_______________________热更配置:%s,更新服务",v)
  28. return 1
  29. end
  30. end
  31. end
  32. return
  33. end
  34. --对需要更新配置的服务进行更新
  35. local function updateconfig(hotfixlist)
  36. updateserver = updateserver or newserver()
  37. if next(updateserver) then
  38. for k, v in pairs(updateserver) do
  39. if v.configname and hotfix_true(hotfixlist,v.configname) then
  40. skynet.send(v.server, "lua", "hotfix",hotfixlist,v.model_name)
  41. end
  42. end
  43. else
  44. logger.trace("_______________________热更配置无服务需要")
  45. end
  46. end
  47. -- local z_trace = require "zlf_log"
  48. -- local z_trace = z_trace.trace
  49. -- example:
  50. -- http://192.168.1.57:9001/hotfix?user=lee&pwd=lee@123&config=*
  51. -- http://192.168.1.57:9001/hotfix?user=lee&pwd=lee@123&code=*
  52. -- http://192.168.1.57:9001/hotfix?user=lee&pwd=lee@123&code=*&config=*
  53. local function hotfix(args, ipaddr, header)
  54. logger.warn("处理来自主机 %s 的hotfix请求,:%s", ipaddr,args.config)
  55. -- 鉴权
  56. -- local user = args.user or ""
  57. -- local pwd = args.pwd or ""
  58. -- if authz[user] ~= pwd then
  59. -- -- return { "403 - Forbidden" }
  60. -- return cjson.encode({errno = 403, host = header.host, info = "账号或密码错误"})
  61. -- end
  62. logger.warn("更新配置文件:%s",args.config)
  63. -- 热更配置文件
  64. if args.config then
  65. local hotfixlist
  66. assetcenter = assetcenter or skynet.localname(".assetcenter")
  67. local error
  68. if args.config == "*" then
  69. error = skynet.call(assetcenter, "lua", "hotfix")
  70. hotfixlist = "*"
  71. elseif args.config then
  72. hotfixlist = {}
  73. local ok, entity = pcall(cjson.decode, args.config)
  74. -- z_trace.guild(" --- entity = ", entity)
  75. if not ok then
  76. return { code=400, msg="Bad request" }
  77. end
  78. for k, v in pairs(entity.config) 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({errno = 400, host = header.host, info = "缺少配置表参数"})
  85. end
  86. if error then
  87. return cjson.encode({errno = 400, host = header.host, info = "读取文件失败: ".. error})
  88. end
  89. updateconfig(hotfixlist)
  90. -- else
  91. -- return cjson.encode({errno = 400, host = header.host, info = "参数未nil : "})
  92. end
  93. -- 热更源代码
  94. if args.code then
  95. codecache.clear()
  96. end
  97. --通知 watchdog, 放弃 agent 池中的失效对象
  98. watchdog = watchdog or skynet.localname(".ws_watchdog")
  99. skynet.call(watchdog, "lua", "hotfix")
  100. logger.warn("hotfix (%s, %s)", args.user, ipaddr)
  101. return cjson.encode({errno = 200, host = header.host, info = "热更新 成功"})
  102. end
  103. return hotfix