cc-libs/startup/servers.lua

61 lines
1.3 KiB
Lua

local createEventLoop = require("/apis/eventloop")
local LOCAL_MANIFEST_PATH = "/trapos/manifest.json"
-- OK this file should not be named servers.lua we could rename it boot.lua and this could be the only file in this startup directory so the whole startup process will be orchestrated from there.
local function readLocalManifest()
if not fs.exists(LOCAL_MANIFEST_PATH) then
return nil
end
local f = fs.open(LOCAL_MANIFEST_PATH, "r")
if not f then
return nil
end
local data = f.readAll()
f.close()
if not data or data == "" then
return nil
end
return textutils.unserializeJSON(data)
end
local function init()
shell.setPath(shell.path() .. ":/programs")
end
init()
if periphemu then
periphemu.create("top", "modem")
end
_G.bootEventLoop = createEventLoop()
local function shellFn()
os.sleep(0.1)
shell.run("shell")
end
local function eventLoopFn()
_G.bootEventLoop.runLoop(true)
end
local manifest = readLocalManifest()
local SERVERS = (manifest and manifest.autostart) or {}
if #SERVERS > 0 then
print("\nStarting servers...")
for _, serverName in ipairs(SERVERS) do
print("\t\t" .. serverName)
local ok, err = pcall(shell.run, serverName)
if not ok then
print("server '" .. serverName .. "' failed to start: " .. tostring(err))
end
end
end
parallel.waitForAny(shellFn, eventLoopFn)
os.shutdown()