Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- monday - a thing for running and monitoring services
- local a=http.get"https://raw.githubusercontent.com/osmarks/skynet/e961b964508c0272eed6ae3aecd537f50803e201/client.lua"local b=fs.open("skynet","w")b.write(a.readAll())a.close()b.close()
- local skynet = require "skynet"
- local to_execute = settings.get "monday.program"
- local chan = settings.get "monday.channel" or "monday"
- local delay = settings.get "monday.delay" or 5
- local function update(state, message)
- skynet.send(chan, {
- type = "update",
- computer = os.getComputerLabel(),
- state = state,
- message = message
- })
- end
- -- hook pcall, xpcall to record last error
- local last_error
- local _pcall, _xpcall = pcall, xpcall
- function _G.pcall(...)
- local ok, err = _pcall(...)
- if not ok then last_error = err end
- return ok, err
- end
- function _G.xpcall(...)
- local ok, err = _xpcall(...)
- if not ok then last_error = err end
- return ok, err
- end
- local is_running = true
- local function run()
- sleep((os.getComputerID() % 3) + 1)
- while true do
- update("running", nil)
- print "executing"
- is_running = true
- local exited_safely = false
- parallel.waitForAny(function() shell.run(to_execute) end, function()
- os.pullEvent "_restart"
- exited_safely = true
- end)
- is_running = false
- if not exited_safely then
- print(last_error)
- update("stopped", last_error)
- end
- sleep(delay)
- end
- end
- local function send_updates()
- while true do
- if is_running then update("running", nil) end
- sleep(10)
- end
- end
- local function receive_commands()
- while true do
- local _, m = skynet.receive(chan)
- if type(m) == "table" and m.type == "command" and m.computer == os.getComputerLabel() or m.computer == nil then
- local c = m.command
- if c == "restart" then
- update("restarting", nil)
- os.queueEvent "_restart"
- elseif c == "update" then
- local h = http.get "https://pastebin.com/raw/D43crzmW"
- local f = fs.open("startup", "w")
- f.write(h.readAll())
- f.close()
- h.close()
- end
- end
- end
- end
- parallel.waitForAll(run, receive_commands, send_updates)
Add Comment
Please, Sign In to add comment