Blame view

src/services/csvdatad.lua 2.18 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
  -- 重新加载csvdb
  
  
  local CMD = {}
5e5d7680   zhouhaihai   热更新 优化
64
  function CMD.reload(code)
3fe4471e   zhouhaihai   热更新 demo
65
  	-- 重新加载 csvdata 
5e5d7680   zhouhaihai   热更新 优化
66
67
68
69
70
71
72
73
74
  	csvdb = csvdb or {}
  	
  	local ok, func = pcall(load, code)
  	if ok then
  		ok = pcall(func)
  	end
  	if not ok then
  		skynet.error("reload_csvdata error by code " .. code)
  		return 'error'
3fe4471e   zhouhaihai   热更新 demo
75
  	end
3fe4471e   zhouhaihai   热更新 demo
76
77
  
  	sharedata.update("csvdata", csvdb)
3fe4471e   zhouhaihai   热更新 demo
78
79
80
  	return 'ok'
  end
  
314bc5df   zhengshouren   提交服务器初始代码
81
82
83
84
  skynet.start(function ()
  	travCsv("src/csvdata", file2timeMap)
  	sharedata.new("csvdata", csvdb)
  	-- handle_timeout()
3fe4471e   zhouhaihai   热更新 demo
85
86
87
88
89
90
91
  
  	skynet.dispatch("lua", function(_, _, command, ...)
  		local f = CMD[command]
  		skynet.ret(skynet.pack(f(...)))
  	end)
  
  	skynet.register "CSVDATA"
314bc5df   zhengshouren   提交服务器初始代码
92
  end)