agent_manager.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. --更新黑名单的书
  105. function CMD.updateBlackBooks(_,msg_body)
  106. local agent = getOneAgent()
  107. if agent~=nil then
  108. skynet.call(agent,"lua",nil,nil,"updateBlackBooks",msg_body)
  109. else
  110. skynet.error("把任务放到队列")
  111. end
  112. end
  113. --更新拉取配置
  114. function CMD.updatePullConig(_,msg_body)
  115. local agent = getOneAgent()
  116. if agent~=nil then
  117. skynet.call(agent,"lua",nil,nil,"updatePullConig",msg_body)
  118. else
  119. skynet.error("把任务放到队列")
  120. end
  121. end
  122. --同步主体
  123. function CMD.syncMain(_,msg_body)
  124. local agent = getOneAgent()
  125. if agent~=nil then
  126. skynet.call(agent,"lua",nil,nil,"syncMain",msg_body)
  127. else
  128. skynet.error("把任务放到队列")
  129. end
  130. end
  131. skynet.start(function()
  132. local protocol = "ws"
  133. -- for i= 1, 3 do --开启30个服务用来接收消息
  134. -- agents[i] = skynet.newservice("agent", "agent", protocol)
  135. -- end
  136. agents[1] = skynet.newservice("agent", "agent", protocol)
  137. local balance = 1
  138. local id = socket.listen("0.0.0.0",runconfig.wsProt )
  139. socket.start(id , function(id, addr)
  140. skynet.send(agents[balance], "lua", id,addr,"connect")
  141. balance = balance + 1
  142. if balance > #agents then
  143. balance = 1
  144. end
  145. end)
  146. skynet.dispatch("lua", dispatch)
  147. end)