Skip to content

Commit

Permalink
录像文件命令优化
Browse files Browse the repository at this point in the history
  • Loading branch information
huahua132 committed Dec 4, 2024
1 parent ddfb01e commit 286e5f4
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 40 deletions.
20 changes: 10 additions & 10 deletions examples/record/module/B_m.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ end

function CMD.start()
skynet.fork(function()
test_service_api()
test_call()
http_call()
test_tcp()
test_redis()
test_utp()
test_math_rand()
test_ostime()
test_hotfix()
test_share_data()
pcall(test_service_api)
pcall(test_call)
pcall(http_call)
pcall(test_tcp)
pcall(test_redis)
pcall(test_utp)
pcall(test_math_rand)
pcall(test_ostime)
pcall(test_hotfix)
pcall(test_share_data)
end)
return true
end
Expand Down
16 changes: 9 additions & 7 deletions lualib/skynet-fly/hotfix/hotfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ local rawget = rawget
local tinsert = table.insert
local tsort = table.sort
local sgsub = string.gsub
local os = os

local g_recordpath = skynet.getenv("recordpath")

local g_loadedmap = {}
local M = {}

local g_seq = 1
local g_patch = 0

local g_tb = _G

Expand Down Expand Up @@ -66,9 +66,12 @@ local function get_patch_file_path(file_path, patch_dir)
return sgsub(file_path, pat, path, 1)
end

local function get_patch_dir()
local function get_patch_dir(up_time)
local base_info = module_info.get_base_info()
return file_util.path_join(g_recordpath, string.format("hotfix_%s/patch_%s/", base_info.module_name, g_patch))

return file_util.path_join(g_recordpath, string.format("hotfix_%s-%s-%s/patch_%s/",
base_info.module_name, base_info.version, os.date("%Y%m%d-%H%M%S", base_info.launch_time),
os.date("%Y%m%d-%H%M%S", up_time)))
end

--可热更模块加载
Expand All @@ -94,11 +97,10 @@ end
--热更
function M.hotfix(hotfixmods)
local base_info = module_info.get_base_info()
g_patch = g_patch + 1

local patch_dir
local cur_time = time_util.time()
if skynet.is_record_handle() then --说明是播放录像
patch_dir = get_patch_dir()
patch_dir = get_patch_dir(cur_time)
end

local hot_ret = {}
Expand Down Expand Up @@ -207,7 +209,7 @@ function M.hotfix(hotfixmods)
end

if skynet.is_write_record() and base_info.index == 1 then --第一个启动记录下就行
local patch_dir = get_patch_dir()
local patch_dir = get_patch_dir(cur_time)
local copy_file_obj = file_util.new_copy_file()
local dir_path_map = {}
for i, info in ipairs(sort_list) do
Expand Down
25 changes: 14 additions & 11 deletions lualib/skynet-fly/sharedata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local timer = require "skynet-fly.timer"
local module_info = require "skynet-fly.etc.module_info"
local log = require "skynet-fly.log"
local file_util = require "skynet-fly.utils.file_util"
local string_util = require "skynet-fly.utils.string_util"

local sinterface = service_watch_interface:new(".sharedata_service")

Expand All @@ -20,13 +21,13 @@ local type = type
local string = string
local os = os
local loadfile = loadfile
local tonumber = tonumber

local g_recordpath = skynet.getenv("recordpath")

local g_mode_map = {}
local g_data_map = {}
local g_version_map = {}
local g_patch_map = {} --热更的patch编号
local g_flush_time_obj = nil

local M = {
Expand Down Expand Up @@ -57,15 +58,12 @@ local function get_patch_file_path(file_path, patch_dir)
return string.gsub(file_path, pat, path, 1)
end

local function add_patch(file_path)
local function add_patch(file_path, up_time)
local function get_patch_dir()
if not g_patch_map[file_path] then
g_patch_map[file_path] = 0
end
g_patch_map[file_path] = g_patch_map[file_path] + 1
local patch = g_patch_map[file_path]
local base_info = module_info.get_base_info()
return file_util.path_join(g_recordpath, string.format("sharedata_%s/patch_%s/", base_info.module_name, patch))
return file_util.path_join(g_recordpath, string.format("sharedata_%s-%s-%s/patch_%s/",
base_info.module_name, base_info.version, os.date("%Y%m%d-%H%M%S", base_info.launch_time),
os.date("%Y%m%d-%H%M%S", up_time)))
end

if skynet.is_write_record() then --如果写录像
Expand Down Expand Up @@ -130,13 +128,16 @@ end})

local function watch_update(watchcli, file_path)
while watchcli:is_watch(file_path) do
local new_version = watchcli:await_update(file_path)
local str = watchcli:await_update(file_path)
local spstr = string_util.split(str, '-')
local new_version = tonumber(spstr[1])
local up_time = tonumber(spstr[2])
local old_version = g_version_map[file_path]
if new_version ~= old_version then
local mode = g_mode_map[file_path]
local mode_func = g_mode_funcs[mode]
g_data_map[file_path] = mode_func.update(file_path)
add_patch(file_path)
add_patch(file_path, up_time)
g_version_map[file_path] = new_version
end
end
Expand All @@ -145,7 +146,9 @@ end
setmetatable(g_version_map, {__index = function(t, k)
local cli = watch_syn.new_client(sinterface)
cli:watch(k)
local version = cli:await_get(k)
local str = cli:await_get(k)
local spstr = string_util.split(str, '-')
local version = tonumber(spstr[1])
t[k] = version

skynet.fork(watch_update, cli, k)
Expand Down
13 changes: 9 additions & 4 deletions lualib/skynet-fly/utils/skynet_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,22 @@ function M.is_hot_container_server()
end

local g_shutdown_func_map = {}

--注册关服处理函数
function M.reg_shutdown_func(func)
function M.reg_shutdown_func(func, sort_weight)
sort_weight = sort_weight or 0
assert(type(func) == 'function', "not function")
assert(type(sort_weight) == 'number', "not number")
local info = debug_getinfo(2,"S")
local key = info.short_src
g_shutdown_func_map[key] = func

g_shutdown_func_map[key] = {func = func, weight = sort_weight}
end

--执行关服处理函数
function M.execute_shutdown()
for src, func in table_util.sort_ipairs_byk(g_shutdown_func_map) do
local isok, err = x_pcall(func)
for src, info in table_util.sort_ipairs(g_shutdown_func_map, function(a, b) return a.weight > b.weight end) do
local isok, err = x_pcall(info.func)
if not isok then
log.error("execute_shutdown err ", err)
end
Expand Down
5 changes: 3 additions & 2 deletions service/contriner_mgr.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local skynet = require "skynet.manager"
local skynet_util = require "skynet-fly.utils.skynet_util"
local time_util = require "skynet-fly.utils.time_util"
local log = require "skynet-fly.log"
local json = require "cjson"
local queue = require "skynet.queue"()
Expand Down Expand Up @@ -73,9 +74,9 @@ local function launch_new_module(module_name,config)

g_version_map[module_name] = g_version_map[module_name] + 1
local version = g_version_map[module_name]
local cur_time = time_util.time()
local cur_date = os.date("%Y-%m-%d[%H:%M:%S]",cur_time)
for i = 1,launch_num do
local cur_time = os.time()
local cur_date = os.date("%Y-%m-%d[%H:%M:%S]",cur_time)
local server_id = skynet.newservice('hot_container',module_name,i,cur_date,cur_time,version,is_record_on)
local args = mod_args[i] or default_arg

Expand Down
7 changes: 4 additions & 3 deletions service/hot_container.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ assert(MODULE_NAME)
local new_loaded = _loaded

if IS_RECORD_ON == 1 then
skynet.start_record(ARGV)
skynet.start_record(ARGV, MODULE_NAME .. '-' .. INDEX .. '-' .. VERSION .. '-' .. os.date('%Y%m%d-%H%M%S', LAUNCH_TIME))
end

local CMD = {}
Expand Down Expand Up @@ -160,6 +160,7 @@ local function check_exit()
log.warn("warning " .. MODULE_NAME .. ' can`t exit')
end
g_check_timer:cancel()
skynet.close_record()
end
end
end
Expand Down Expand Up @@ -257,6 +258,6 @@ end)

if IS_RECORD_ON == 1 then
skynet_util.reg_shutdown_func(function()
skynet.recordoff()
end)
skynet.close_record()
end, -1) -- -1 最后执行
end
7 changes: 5 additions & 2 deletions service/sharedata_service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local sharedata = require "skynet.sharedata"
local sharetable = require "skynet.sharetable"
local skynet_util = require "skynet-fly.utils.skynet_util"
local file_util = require "skynet-fly.utils.file_util"
local time_util = require "skynet-fly.utils.time_util"
local watch_syn = require "skynet-fly.watch.watch_syn"

local log = require "skynet-fly.log"
Expand Down Expand Up @@ -47,6 +48,7 @@ local CMD = {}
function CMD.load(dir_list, mode)
assert(g_modes[mode], "not exists mode:" .. tostring(mode))
local m = g_modes[mode]
local cur_time = time_util.time()
for _,dir in pairs(dir_list) do
for file_name,file_path,file_info in file_util.diripairs(dir) do
file_path = file_util.convert_windows_to_linux_relative(file_path)
Expand All @@ -60,14 +62,15 @@ function CMD.load(dir_list, mode)
last_change_time = file_info.modification,
}

g_watch_server:register(file_path, g_file_changetime_map[file_path].version)
g_watch_server:register(file_path, g_file_changetime_map[file_path].version .. '-' .. cur_time)
end
end
end
end

function CMD.check_reload()
local reload_list = {}
local cur_time = time_util.time()
for file_path, info in pairs(g_file_changetime_map) do
local file_info, errinfo, errno = lfs.attributes(file_path)
if not file_info then
Expand All @@ -81,7 +84,7 @@ function CMD.check_reload()
info.version = info.version + 1
table.insert(reload_list, file_path)

g_watch_server:publish(file_path, info.version)
g_watch_server:publish(file_path, info.version .. '-' .. cur_time)
end
end
end
Expand Down

0 comments on commit 286e5f4

Please sign in to comment.