Blame view

src/services/csvdatad.lua 2.2 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
3
4
5
6
7
8
9
10
  local sharedata = require "skynet.sharedata"
  local skynet = require "skynet"
  local lfs = require "lfs"
  local redisproxy = require "shared.redisproxy"
  require "shared.init"
  require "utils.init"
  require "csvdata.init"
  require "skynet.manager"
  require "RedisKeys"
  
8dce6908   zhouahaihai   配表由两个位置读取
11
  -- local csvdb = {}
314bc5df   zhengshouren   提交服务器初始代码
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  
  local function formatFileName(filename)
  	filename = string.trim(filename)
  	local basename = filename:match("([^/]+)%.lua$")
  	if not basename then return end
  	local loadname = filename:match("^src/([^.]+)%.lua$")
  	loadname = loadname:gsub('/', '.')
  	return basename, loadname
  end
  
  local function travCsv(rootPath, pathes)
  	pathes = pathes or {}
  	local modified = false
  	local ok, files, iter = pcall(lfs.dir, rootPath)
  	if not ok then return modified end
  	for entry in files, iter do
  		-- 过滤 . 开始的字符串包括 . .. .git .开头的文件名
  		if string.byte(entry, 1) ~= 46 then
  			local pathfile = rootPath .. '/' .. entry
  			local attrs = lfs.attributes(pathfile)
  			if attrs.mode == 'directory' then
  				modified = travCsv(pathfile, pathes) or modified
  			else
  				local basename, loadname = formatFileName(pathfile)
8dce6908   zhouahaihai   配表由两个位置读取
36
37
38
39
40
41
  				if basename then
  					if tonum(pathes[loadname]) < attrs.modification then
  						modified = true
  						pathes[loadname] = attrs.modification
  					end
  					if basename == "init" or basename == "init_adv" then
314bc5df   zhengshouren   提交服务器初始代码
42
  						require(loadname)
314bc5df   zhengshouren   提交服务器初始代码
43
  					end
314bc5df   zhengshouren   提交服务器初始代码
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  				end
  			end
  		end
  	end
  	return modified
  end
  
  -- 每分钟检查是否有更改
  local file2timeMap = {}
  local function handle_timeout()
  	if travCsv("src/csvdata", file2timeMap) then
  		sharedata.update("csvdata", csvdb)
  	end
  	skynet.timeout(100*5, handle_timeout)
  end
  
3fe4471e   zhouhaihai   热更新 demo
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  -- 重新加载csvdb
  
  
  local CMD = {}
  function CMD.reload()
  	-- 重新加载 csvdata 
  	csvdb = {}
  	for k, v in pairs(package.loaded) do
  		if k:find("csvdata") then
  			package.loaded[k] = nil
  		end
  	end
  	require("csvdata.init")
  	require("csvdata.init_adv")
  
  	sharedata.update("csvdata", csvdb)
  	skynet.sleep(1) -- 睡一觉再返回
  	return 'ok'
  end
  
314bc5df   zhengshouren   提交服务器初始代码
80
81
82
83
  skynet.start(function ()
  	travCsv("src/csvdata", file2timeMap)
  	sharedata.new("csvdata", csvdb)
  	-- handle_timeout()
3fe4471e   zhouhaihai   热更新 demo
84
85
86
87
88
89
90
  
  	skynet.dispatch("lua", function(_, _, command, ...)
  		local f = CMD[command]
  		skynet.ret(skynet.pack(f(...)))
  	end)
  
  	skynet.register "CSVDATA"
314bc5df   zhengshouren   提交服务器初始代码
91
  end)