dotfiles from arch
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
local thread = require 'bee.thread'
|
||||
|
||||
local taskPad = thread.channel('taskpad')
|
||||
local waiter = thread.channel('waiter')
|
||||
|
||||
---@class pub_brave
|
||||
local m = {}
|
||||
m.type = 'brave'
|
||||
m.ability = {}
|
||||
m.queue = {}
|
||||
|
||||
--- 注册成为勇者
|
||||
function m.register(id, privatePad)
|
||||
m.id = id
|
||||
|
||||
if #m.queue > 0 then
|
||||
for _, info in ipairs(m.queue) do
|
||||
waiter:push(m.id, info.name, info.params)
|
||||
end
|
||||
end
|
||||
m.queue = nil
|
||||
|
||||
m.start(privatePad)
|
||||
end
|
||||
|
||||
--- 注册能力
|
||||
function m.on(name, callback)
|
||||
m.ability[name] = callback
|
||||
end
|
||||
|
||||
--- 报告
|
||||
function m.push(name, params)
|
||||
if m.id then
|
||||
waiter:push(m.id, name, params)
|
||||
else
|
||||
m.queue[#m.queue+1] = {
|
||||
name = name,
|
||||
params = params,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
--- 开始找工作
|
||||
function m.start(privatePad)
|
||||
local reqPad = privatePad and thread.channel('req:' .. privatePad) or taskPad
|
||||
local resPad = privatePad and thread.channel('res:' .. privatePad) or waiter
|
||||
m.push('mem', collectgarbage 'count')
|
||||
while true do
|
||||
local name, id, params = reqPad:bpop()
|
||||
local ability = m.ability[name]
|
||||
-- TODO
|
||||
if not ability then
|
||||
resPad:push(m.id, id)
|
||||
log.error('Brave can not handle this work: ' .. name)
|
||||
goto CONTINUE
|
||||
end
|
||||
local ok, res = xpcall(ability, log.error, params)
|
||||
if ok then
|
||||
resPad:push(m.id, id, res)
|
||||
else
|
||||
resPad:push(m.id, id)
|
||||
end
|
||||
m.push('mem', collectgarbage 'count')
|
||||
::CONTINUE::
|
||||
end
|
||||
end
|
||||
|
||||
return m
|
||||
@@ -0,0 +1,4 @@
|
||||
local brave = require 'brave.brave'
|
||||
require 'brave.work'
|
||||
|
||||
return brave
|
||||
@@ -0,0 +1,55 @@
|
||||
local brave = require 'brave'
|
||||
local time = require 'bee.time'
|
||||
|
||||
local tablePack = table.pack
|
||||
local tostring = tostring
|
||||
local tableConcat = table.concat
|
||||
local debugTraceBack = debug.traceback
|
||||
local debugGetInfo = debug.getinfo
|
||||
local monotonic = time.monotonic
|
||||
|
||||
_ENV = nil
|
||||
|
||||
local function pushLog(level, ...)
|
||||
local t = tablePack(...)
|
||||
for i = 1, t.n do
|
||||
t[i] = tostring(t[i])
|
||||
end
|
||||
local str = tableConcat(t, '\t', 1, t.n)
|
||||
if level == 'error' then
|
||||
str = str .. '\n' .. debugTraceBack(nil, 3)
|
||||
end
|
||||
local info = debugGetInfo(3, 'Sl')
|
||||
brave.push('log', {
|
||||
level = level,
|
||||
msg = str,
|
||||
src = info.source,
|
||||
line = info.currentline,
|
||||
clock = monotonic(),
|
||||
})
|
||||
return str
|
||||
end
|
||||
|
||||
local m = {}
|
||||
|
||||
function m.info(...)
|
||||
pushLog('info', ...)
|
||||
end
|
||||
|
||||
function m.debug(...)
|
||||
pushLog('debug', ...)
|
||||
end
|
||||
|
||||
function m.trace(...)
|
||||
pushLog('trace', ...)
|
||||
end
|
||||
|
||||
function m.warn(...)
|
||||
pushLog('warn', ...)
|
||||
end
|
||||
|
||||
function m.error(...)
|
||||
pushLog('error', ...)
|
||||
end
|
||||
|
||||
return m
|
||||
@@ -0,0 +1,125 @@
|
||||
local brave = require 'brave.brave'
|
||||
|
||||
brave.on('loadProtoByStdio', function ()
|
||||
local jsonrpc = require 'jsonrpc'
|
||||
while true do
|
||||
local proto, err = jsonrpc.decode(io.read)
|
||||
--log.debug('loaded proto', proto.method)
|
||||
if not proto then
|
||||
brave.push('protoerror', err)
|
||||
return
|
||||
end
|
||||
brave.push('proto', proto)
|
||||
end
|
||||
end)
|
||||
|
||||
brave.on('loadProtoBySocket', function (param)
|
||||
local jsonrpc = require 'jsonrpc'
|
||||
local net = require 'service.net'
|
||||
local buf = ''
|
||||
|
||||
---@async
|
||||
local parser = coroutine.create(function ()
|
||||
while true do
|
||||
---@async
|
||||
local proto, err = jsonrpc.decode(function (len)
|
||||
while true do
|
||||
if #buf >= len then
|
||||
local res = buf:sub(1, len)
|
||||
buf = buf:sub(len + 1)
|
||||
return res
|
||||
end
|
||||
coroutine.yield()
|
||||
end
|
||||
end)
|
||||
--log.debug('loaded proto', proto.method)
|
||||
if not proto then
|
||||
brave.push('protoerror', err)
|
||||
return
|
||||
end
|
||||
brave.push('proto', proto)
|
||||
end
|
||||
end)
|
||||
|
||||
local lsclient = net.connect('tcp', '127.0.0.1', param.port)
|
||||
local lsmaster = net.connect('unix', param.unixPath)
|
||||
|
||||
assert(lsclient)
|
||||
assert(lsmaster)
|
||||
|
||||
function lsclient:on_data(data)
|
||||
buf = buf .. data
|
||||
coroutine.resume(parser)
|
||||
end
|
||||
|
||||
function lsclient:on_error(...)
|
||||
log.error(...)
|
||||
end
|
||||
|
||||
function lsmaster:on_data(data)
|
||||
lsclient:write(data)
|
||||
--net.update()
|
||||
end
|
||||
|
||||
function lsmaster:on_error(...)
|
||||
log.error(...)
|
||||
end
|
||||
|
||||
while true do
|
||||
net.update(10)
|
||||
end
|
||||
end)
|
||||
|
||||
brave.on('timer', function (time)
|
||||
local thread = require 'bee.thread'
|
||||
while true do
|
||||
thread.sleep(math.floor(time * 1000))
|
||||
brave.push('wakeup')
|
||||
end
|
||||
end)
|
||||
|
||||
brave.on('loadFile', function (path)
|
||||
local util = require 'utility'
|
||||
return util.loadFile(path)
|
||||
end)
|
||||
|
||||
brave.on('removeCaches', function (path)
|
||||
local fs = require 'bee.filesystem'
|
||||
local fsu = require 'fs-utility'
|
||||
for dir in fs.pairs(fs.path(path)) do
|
||||
local lockFile = dir / '.lock'
|
||||
local f = io.open(lockFile:string(), 'wb')
|
||||
if f then
|
||||
f:close()
|
||||
fsu.fileRemove(dir)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
---@class brave.param.compile
|
||||
---@field uri uri
|
||||
---@field text string
|
||||
---@field mode string
|
||||
---@field version string
|
||||
---@field options brave.param.compile.options
|
||||
|
||||
---@class brave.param.compile.options
|
||||
---@field special table<string, string>
|
||||
---@field unicodeName boolean
|
||||
---@field nonstandardSymbol table<string, true>
|
||||
|
||||
---@param param brave.param.compile
|
||||
brave.on('compile', function (param)
|
||||
local parser = require 'parser'
|
||||
local clock = os.clock()
|
||||
local state, err = parser.compile(param.text
|
||||
, param.mode
|
||||
, param.version
|
||||
, param.options
|
||||
)
|
||||
log.debug('Async compile', param.uri, 'takes:', os.clock() - clock)
|
||||
return {
|
||||
state = state,
|
||||
err = err,
|
||||
}
|
||||
end)
|
||||
Reference in New Issue
Block a user