srvConfig.lua 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. --[[
  2. Descripttion:配置管理
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-04 11:24:54
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-04 16:49:25
  8. --]]
  9. local skynet = require "skynet"
  10. local sharetable = require "skynet.sharetable"
  11. local lfsUtil = require "utils.lfsUtil"
  12. local baseService = require "baseService"
  13. local pathConfig = skynet.getenv("config_path")
  14. local MAP_FILE_MOD_TIME = {} -- 文件修改时间
  15. local command = {}
  16. local SLEEPTIME = 60 * 100
  17. local function l_collect_garbage()
  18. while true do
  19. skynet.sleep(SLEEPTIME)
  20. collectgarbage "collect"
  21. end
  22. end
  23. local function l_get_file_path_name(file)
  24. return pathConfig .. file
  25. end
  26. -- 获取配置文件更新时间
  27. local function l_get_files_time(fileList)
  28. local files = fileList or lfsUtil:get_path_files(pathConfig)
  29. local mapFileTime = {}
  30. for _, file in pairs(files) do
  31. mapFileTime[file] = lfsUtil:get_file_modification(l_get_file_path_name(file))
  32. end
  33. return mapFileTime
  34. end
  35. -- 更新配置文件
  36. local function l_get_update_file_list()
  37. local updateFileList = {}
  38. local mapFileTime = l_get_files_time()
  39. for file, time in pairs(mapFileTime) do
  40. if time ~= MAP_FILE_MOD_TIME[file] then
  41. sharetable.loadfile(l_get_file_path_name(file))
  42. table.insert(updateFileList, l_get_file_path_name(file))
  43. MAP_FILE_MOD_TIME[file] = time
  44. end
  45. end
  46. return updateFileList
  47. end
  48. local function l_init()
  49. local files = lfsUtil:get_path_files(pathConfig)
  50. for _, file in pairs(files) do
  51. sharetable.loadfile(l_get_file_path_name(file))
  52. end
  53. MAP_FILE_MOD_TIME = l_get_files_time(files)
  54. end
  55. -- 获取更新文件
  56. function command.get_update_files()
  57. local syscmd = string.format("cd %s", pathConfig)
  58. os.execute(syscmd)
  59. os.execute("git pull")
  60. local updateList = l_get_update_file_list()
  61. if #updateList <= 0 then
  62. return
  63. end
  64. return updateList
  65. end
  66. function command.onStart()
  67. l_init()
  68. skynet.fork(l_collect_garbage)
  69. end
  70. baseService.start(command, ".config", true)