Skip to content

Commit

Permalink
暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed May 13, 2024
1 parent 2fb44d5 commit 7e2a497
Show file tree
Hide file tree
Showing 10 changed files with 1,000 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"/libs/",
"/3rd",
"/.vscode",
"/meta"
"/meta",
"script/ltask/lualib"
],
"checkThirdParty": false
},
Expand Down
9 changes: 1 addition & 8 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
if not package.loaded["ltask"] then
require "ltask"
return
end

local fs = require 'bee.filesystem'
local util = require 'utility'
local version = require 'version'
Expand Down Expand Up @@ -82,6 +77,4 @@ xpcall(dofile, log.debug, (ROOT / 'debugger.lua'):string())

require 'cli'

local _, service = xpcall(require, log.error, 'service')

service.start()
xpcall(require, log.error, 'ltask2')
4 changes: 0 additions & 4 deletions script/ltask/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions script/ltask/service/main.lua

This file was deleted.

24 changes: 18 additions & 6 deletions script/ltask/init.lua → script/ltask2/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local boot = require "ltask.bootstrap"

local function searchpath(name)
return assert(package.searchpath(name, "script/ltask/?.lua"))
return assert(package.searchpath(name, (ROOT / 'script/ltask2/lualib/?.lua'):string()))
end

local function readall(path)
Expand All @@ -17,9 +17,20 @@ local root_config = {
name = "timer",
unique = true,
},
{
name = "logger",
unique = true,
args = {
LOGPATH = LOGPATH,
}
},
{
name = "main",
args = { arg },
args = {
ROOT = ROOT:string(),
LOGPATH = LOGPATH,
METAPATH = METAPATH,
},
},
},
service_source = readall(servicepath),
Expand All @@ -28,23 +39,24 @@ local root_config = {
local name = ...
package.path = [[${lua_path}]]
package.cpath = [[${lua_cpath}]]
local filename, err = package.searchpath(name, "${service_path}")
local filename, err = package.searchpath(name, package.path)
if not filename then
return nil, err
return nil, err
end
return loadfile(filename)
]=]):gsub("%$%{([^}]*)%}", {
lua_path = package.path,
lua_cpath = package.cpath,
service_path = "script/ltask/service/?.lua",
service_path = (ROOT / "script/ltask2/service/?.lua"):string(),
}),
}

boot.init_socket()
local bootstrap = dofile(searchpath "bootstrap")
local bootstrap = require 'ltask2.lualib.bootstrap'
local ctx = bootstrap.start {
core = {},
root = root_config,
root_initfunc = root_config.initfunc,
}

bootstrap.wait(ctx)
42 changes: 42 additions & 0 deletions script/ltask2/lualib/bootstrap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local boot = require "ltask.bootstrap"

local SERVICE_ROOT <const> = 1
local MESSSAGE_SYSTEM <const> = 0

local function bootstrap_root(initfunc, config)
local sid = assert(boot.new_service("root", config.service_source, config.service_chunkname, SERVICE_ROOT))
assert(sid == SERVICE_ROOT)
boot.init_root(SERVICE_ROOT)
-- send init message to root service
local init_msg, sz = boot.pack("init", {
initfunc = initfunc,
name = "root",
args = {config}
})
-- self bootstrap
boot.post_message {
from = SERVICE_ROOT,
to = SERVICE_ROOT,
session = 1, -- 1 for root init
type = MESSSAGE_SYSTEM,
message = init_msg,
size = sz,
}
end

local function start(config)
boot.init(config.core)
boot.init_timer()
bootstrap_root(config.root_initfunc, config.root)
return boot.run(config.mainthread)
end

local function wait(ctx)
boot.wait(ctx)
boot.deinit()
end

return {
start = start,
wait = wait,
}
37 changes: 37 additions & 0 deletions script/ltask2/lualib/logger.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local ltask = require "ltask"
local logpath = (...).LOGPATH

local S = {}

local logfile = io.open(logpath .. '/ltask.log', 'w+b')
if logfile then
logfile:setvbuf("no")
end

local function writelog()
while true do
local ti, _, msg, sz = ltask.poplog()
if ti == nil then
break
end
local tsec = ti // 100
local msec = ti % 100
local level, message = ltask.unpack_remove(msg, sz)
if logfile then
logfile:write(string.format("[%s.%02d][%-5s]%s\n", os.date("%Y-%m-%d %H:%M:%S", tsec), msec, level:upper(), message))
end
end
end

ltask.fork(function()
while true do
writelog()
ltask.sleep(100)
end
end)

function S.quit()
writelog()
end

return S
Loading

0 comments on commit 7e2a497

Please sign in to comment.