Skip to content

Commit

Permalink
ltask init
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed May 12, 2024
1 parent 0df5883 commit c7c90e8
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@
[submodule "meta/3rd/luv"]
path = meta/3rd/luv
url = https://github.com/LuaCATS/luv.git
[submodule "3rd/ltask"]
path = 3rd/ltask
url = https://github.com/cloudwu/ltask
2 changes: 1 addition & 1 deletion 3rd/bee.lua
1 change: 1 addition & 0 deletions 3rd/ltask
Submodule ltask added at 79a3f1
5 changes: 5 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
if not package.loaded["ltask"] then
require "ltask.init"
return
end

local fs = require 'bee.filesystem'
local util = require 'utility'
local version = require 'version'
Expand Down
44 changes: 44 additions & 0 deletions make.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,49 @@ require "make.detect_platform"
lm:import "3rd/bee.lua/make.lua"
lm:import "make/code_format.lua"

lm:copy 'ltask-script' {
inputs = {
"3rd/ltask/lualib/bootstrap.lua",
"3rd/ltask/lualib/service.lua",
"3rd/ltask/service/timer.lua",
"3rd/ltask/service/root.lua",
},
outputs = {
"script/ltask/bootstrap.lua",
"script/ltask/service.lua",
"script/ltask/service/timer.lua",
"script/ltask/service/root.lua",
},
}

lm:source_set 'ltask' {
deps = "ltask-script",
rootdir = '3rd',
c = "c11",
includes = "bee.lua/3rd/lua",
sources = {
"ltask/src/*.c",
"!ltask/src/lua-seri.c",
},
defines = {
--"DEBUGLOG",
lm.mode=="debug" and "DEBUGTHREADNAME",
},
windows = {
defines = {
"_WIN32_WINNT=0x0601"
},
},
msvc = {
flags = {
"/experimental:c11atomics"
},
},
gcc = {
defines = "_XOPEN_SOURCE=600",
}
}

lm:source_set 'lpeglabel' {
rootdir = '3rd',
includes = "bee.lua/3rd/lua",
Expand All @@ -36,6 +79,7 @@ lm:executable "lua-language-server" {
"source_lua",
"source_bootstrap",
"lpeglabel",
"ltask",
includeCodeFormat and "code_format" or nil,
},
includes = {
Expand Down
5 changes: 4 additions & 1 deletion make/modules.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <bee/lua/module.h>

extern "C" int luaopen_lpeglabel (lua_State *L);
static ::bee::lua::callfunc _init(::bee::lua::register_module, "lpeglabel", luaopen_lpeglabel);
static ::bee::lua::callfunc _init_lpeg(::bee::lua::register_module, "lpeglabel", luaopen_lpeglabel);

extern "C" int luaopen_ltask_bootstrap (lua_State *L);
static ::bee::lua::callfunc _init_ltask(::bee::lua::register_module, "ltask.bootstrap", luaopen_ltask_bootstrap);

#ifdef CODE_FORMAT
extern "C" int luaopen_code_format(lua_State *L);
Expand Down
4 changes: 4 additions & 0 deletions script/ltask/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/bootstrap.lua
/service.lua
/service/timer.lua
/service/root.lua
54 changes: 54 additions & 0 deletions script/ltask/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
local boot = require "ltask.bootstrap"

local function searchpath(name)
return assert(package.searchpath(name, "script/ltask/?.lua"))
end

local function readall(path)
local f <close> = assert(io.open(path))
return f:read "a"
end

local servicepath = searchpath "service"

local root_config = {
bootstrap = {
{
name = "timer",
unique = true,
},
{
name = "logger",
unique = true,
},
{
name = "main",
args = { arg },
},
},
service_source = readall(servicepath),
service_chunkname = "@" .. servicepath,
initfunc = ([=[
local name = ...
package.path = [[${lua_path}]]
package.cpath = [[${lua_cpath}]]
local filename, err = package.searchpath(name, "${service_path}")
if not filename then
return nil, err
end
return loadfile(filename)
]=]):gsub("%$%{([^}]*)%}", {
lua_path = package.path,
lua_cpath = package.cpath,
service_path = "script/ltask/service/?.lua",
}),
}

boot.init_socket()
local bootstrap = dofile(searchpath "bootstrap")
local ctx = bootstrap.start {
core = {},
root = root_config,
root_initfunc = root_config.initfunc,
}
bootstrap.wait(ctx)
34 changes: 34 additions & 0 deletions script/ltask/service/logger.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local ltask = require "ltask"

local S = {}

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

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

function S.quit()
writelog()
end

return S
2 changes: 2 additions & 0 deletions script/ltask/service/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
arg = ...
dofile "main.lua"

0 comments on commit c7c90e8

Please sign in to comment.