Skip to content

Commit 7e2a497

Browse files
committed
暂存
1 parent 2fb44d5 commit 7e2a497

File tree

10 files changed

+1000
-21
lines changed

10 files changed

+1000
-21
lines changed

.luarc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"/libs/",
4141
"/3rd",
4242
"/.vscode",
43-
"/meta"
43+
"/meta",
44+
"script/ltask/lualib"
4445
],
4546
"checkThirdParty": false
4647
},

main.lua

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
if not package.loaded["ltask"] then
2-
require "ltask"
3-
return
4-
end
5-
61
local fs = require 'bee.filesystem'
72
local util = require 'utility'
83
local version = require 'version'
@@ -82,6 +77,4 @@ xpcall(dofile, log.debug, (ROOT / 'debugger.lua'):string())
8277

8378
require 'cli'
8479

85-
local _, service = xpcall(require, log.error, 'service')
86-
87-
service.start()
80+
xpcall(require, log.error, 'ltask2')

script/ltask/.gitignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

script/ltask/service/main.lua

Lines changed: 0 additions & 2 deletions
This file was deleted.

script/ltask/init.lua renamed to script/ltask2/init.lua

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local boot = require "ltask.bootstrap"
22

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

77
local function readall(path)
@@ -17,9 +17,20 @@ local root_config = {
1717
name = "timer",
1818
unique = true,
1919
},
20+
{
21+
name = "logger",
22+
unique = true,
23+
args = {
24+
LOGPATH = LOGPATH,
25+
}
26+
},
2027
{
2128
name = "main",
22-
args = { arg },
29+
args = {
30+
ROOT = ROOT:string(),
31+
LOGPATH = LOGPATH,
32+
METAPATH = METAPATH,
33+
},
2334
},
2435
},
2536
service_source = readall(servicepath),
@@ -28,23 +39,24 @@ local root_config = {
2839
local name = ...
2940
package.path = [[${lua_path}]]
3041
package.cpath = [[${lua_cpath}]]
31-
local filename, err = package.searchpath(name, "${service_path}")
42+
local filename, err = package.searchpath(name, package.path)
3243
if not filename then
33-
return nil, err
44+
return nil, err
3445
end
3546
return loadfile(filename)
3647
]=]):gsub("%$%{([^}]*)%}", {
3748
lua_path = package.path,
3849
lua_cpath = package.cpath,
39-
service_path = "script/ltask/service/?.lua",
50+
service_path = (ROOT / "script/ltask2/service/?.lua"):string(),
4051
}),
4152
}
4253

4354
boot.init_socket()
44-
local bootstrap = dofile(searchpath "bootstrap")
55+
local bootstrap = require 'ltask2.lualib.bootstrap'
4556
local ctx = bootstrap.start {
4657
core = {},
4758
root = root_config,
4859
root_initfunc = root_config.initfunc,
4960
}
61+
5062
bootstrap.wait(ctx)

script/ltask2/lualib/bootstrap.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local boot = require "ltask.bootstrap"
2+
3+
local SERVICE_ROOT <const> = 1
4+
local MESSSAGE_SYSTEM <const> = 0
5+
6+
local function bootstrap_root(initfunc, config)
7+
local sid = assert(boot.new_service("root", config.service_source, config.service_chunkname, SERVICE_ROOT))
8+
assert(sid == SERVICE_ROOT)
9+
boot.init_root(SERVICE_ROOT)
10+
-- send init message to root service
11+
local init_msg, sz = boot.pack("init", {
12+
initfunc = initfunc,
13+
name = "root",
14+
args = {config}
15+
})
16+
-- self bootstrap
17+
boot.post_message {
18+
from = SERVICE_ROOT,
19+
to = SERVICE_ROOT,
20+
session = 1, -- 1 for root init
21+
type = MESSSAGE_SYSTEM,
22+
message = init_msg,
23+
size = sz,
24+
}
25+
end
26+
27+
local function start(config)
28+
boot.init(config.core)
29+
boot.init_timer()
30+
bootstrap_root(config.root_initfunc, config.root)
31+
return boot.run(config.mainthread)
32+
end
33+
34+
local function wait(ctx)
35+
boot.wait(ctx)
36+
boot.deinit()
37+
end
38+
39+
return {
40+
start = start,
41+
wait = wait,
42+
}

script/ltask2/lualib/logger.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
local ltask = require "ltask"
2+
local logpath = (...).LOGPATH
3+
4+
local S = {}
5+
6+
local logfile = io.open(logpath .. '/ltask.log', 'w+b')
7+
if logfile then
8+
logfile:setvbuf("no")
9+
end
10+
11+
local function writelog()
12+
while true do
13+
local ti, _, msg, sz = ltask.poplog()
14+
if ti == nil then
15+
break
16+
end
17+
local tsec = ti // 100
18+
local msec = ti % 100
19+
local level, message = ltask.unpack_remove(msg, sz)
20+
if logfile then
21+
logfile:write(string.format("[%s.%02d][%-5s]%s\n", os.date("%Y-%m-%d %H:%M:%S", tsec), msec, level:upper(), message))
22+
end
23+
end
24+
end
25+
26+
ltask.fork(function()
27+
while true do
28+
writelog()
29+
ltask.sleep(100)
30+
end
31+
end)
32+
33+
function S.quit()
34+
writelog()
35+
end
36+
37+
return S

0 commit comments

Comments
 (0)