agent_manager.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. local skynet = require "skynet"
  2. local sockethelper = require "http.sockethelper"
  3. local socket = require "skynet.socket"
  4. local runconfig = require "run_config"
  5. local tools = require "tools"
  6. local CMD = {}
  7. local agents = {}
  8. function traceback(err)
  9. skynet.error(tostring(err))
  10. skynet.error(debug.traceback())
  11. end
  12. local dispatch = function(session, address, cmd, ...)
  13. local fun = CMD[cmd]
  14. if not fun then
  15. skynet.ret()
  16. return
  17. end
  18. local ret = table.pack(xpcall(fun, traceback, address, ...))
  19. local isok = ret[1]
  20. if not isok then
  21. skynet.ret()
  22. return
  23. end
  24. skynet.retpack(table.unpack(ret,2))
  25. end
  26. function getOneAgent()
  27. -- for i = 1, #agents, 1 do
  28. -- local isActive = skynet.call(agents[i],"lua",nil,nil,"isActive")
  29. -- if isActive then
  30. -- local isRunTask = skynet.call(agents[i],"lua",nil,nil,"isRunStak")
  31. -- if(isRunTask==false) then
  32. -- return agents[i]
  33. -- end
  34. -- end
  35. -- end
  36. local isActive = skynet.call(agents[1],"lua",nil,nil,"isActive")
  37. if isActive then
  38. return agents[1]
  39. end
  40. return nil
  41. end
  42. --更新筛选配置
  43. function CMD.updateFilterConfig(_,msg_body)
  44. local agent = getOneAgent()
  45. if agent~=nil then
  46. skynet.call(agent,"lua",nil,nil,"updateFilterConfig",msg_body)
  47. else
  48. skynet.error("把任务放到队列")
  49. end
  50. end
  51. --更新主体配置
  52. function CMD.updateMainConfig(_,msg_body)
  53. local agent = getOneAgent()
  54. if agent~=nil then
  55. skynet.call(agent,"lua",nil,nil,"updateMainConfig",msg_body)
  56. else
  57. skynet.error("把任务放到队列")
  58. end
  59. end
  60. --更新App配置
  61. function CMD.updateAppConfig(_,msg_body)
  62. local agent = getOneAgent()
  63. if agent~=nil then
  64. skynet.call(agent,"lua",nil,nil,"updateAppConfig",msg_body)
  65. else
  66. skynet.error("把任务放到队列")
  67. end
  68. end
  69. --更新平台配置
  70. function CMD.updatePlatformConfig(_,msg_body)
  71. local agent = getOneAgent()
  72. if agent~=nil then
  73. skynet.call(agent,"lua",nil,nil,"updatePlatformConfig",msg_body)
  74. else
  75. skynet.error("把任务放到队列")
  76. end
  77. end
  78. --添加主体
  79. function CMD.addMain(_,msg_body)
  80. local agent = getOneAgent()
  81. if agent~=nil then
  82. skynet.call(agent,"lua",nil,nil,"addMain",msg_body)
  83. else
  84. skynet.error("把任务放到队列")
  85. end
  86. end
  87. --更新番茄key列表
  88. function CMD.updateFqKeyList(_,msg_body)
  89. local agent = getOneAgent()
  90. if agent~=nil then
  91. skynet.call(agent,"lua",nil,nil,"updateFqKeyList",msg_body)
  92. else
  93. skynet.error("把任务放到队列")
  94. end
  95. end
  96. function CMD.updateFqMfKeyList(_,msg_body)
  97. local agent = getOneAgent()
  98. if agent~=nil then
  99. skynet.call(agent,"lua",nil,nil,"updateFqMfKeyList",msg_body)
  100. else
  101. skynet.error("把任务放到队列")
  102. end
  103. end
  104. function CMD.updateYwKeyList(_,msg_body)
  105. local agent = getOneAgent()
  106. if agent~=nil then
  107. skynet.call(agent,"lua",nil,nil,"updateYwKeyList",msg_body)
  108. else
  109. skynet.error("把任务放到队列")
  110. end
  111. end
  112. --更新黑名单的书
  113. function CMD.updateBlackBooks(_,msg_body)
  114. local agent = getOneAgent()
  115. if agent~=nil then
  116. skynet.call(agent,"lua",nil,nil,"updateBlackBooks",msg_body)
  117. else
  118. skynet.error("把任务放到队列")
  119. end
  120. end
  121. --更新拉取配置
  122. function CMD.updatePullConig(_,msg_body)
  123. local agent = getOneAgent()
  124. if agent~=nil then
  125. skynet.call(agent,"lua",nil,nil,"updatePullConig",msg_body)
  126. else
  127. skynet.error("把任务放到队列")
  128. end
  129. end
  130. --更新拉取配置
  131. function CMD.updatePullConig2(_,msg_body)
  132. local agent = getOneAgent()
  133. if agent~=nil then
  134. skynet.call(agent,"lua",nil,nil,"updatePullConig2",msg_body)
  135. else
  136. skynet.error("把任务放到队列")
  137. end
  138. end
  139. --同步主体
  140. function CMD.syncMain(_,msg_body)
  141. local agent = getOneAgent()
  142. if agent~=nil then
  143. skynet.call(agent,"lua",nil,nil,"syncMain",msg_body)
  144. else
  145. skynet.error("把任务放到队列")
  146. end
  147. end
  148. skynet.start(function()
  149. local protocol = "ws"
  150. -- for i= 1, 3 do --开启30个服务用来接收消息
  151. -- agents[i] = skynet.newservice("agent", "agent", protocol)
  152. -- end
  153. agents[1] = skynet.newservice("agent", "agent", protocol)
  154. local balance = 1
  155. local id = socket.listen("0.0.0.0",runconfig.wsProt )
  156. socket.start(id , function(id, addr)
  157. skynet.send(agents[balance], "lua", id,addr,"connect")
  158. balance = balance + 1
  159. if balance > #agents then
  160. balance = 1
  161. end
  162. end)
  163. skynet.dispatch("lua", dispatch)
  164. end)