osmarks

monday client

Nov 11th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | None | 0 0
  1. -- monday - a thing for running and monitoring services
  2.  
  3. 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()
  4.  
  5. local skynet = require "skynet"
  6.  
  7. local to_execute = settings.get "monday.program"
  8. local chan = settings.get "monday.channel" or "monday"
  9. local delay = settings.get "monday.delay" or 5
  10.  
  11. local function update(state, message)
  12.     skynet.send(chan, {
  13.         type = "update",
  14.         computer = os.getComputerLabel(),
  15.         state = state,
  16.         message = message
  17.     })
  18. end
  19.  
  20. -- hook pcall, xpcall to record last error
  21.  
  22. local last_error
  23.  
  24. local _pcall, _xpcall = pcall, xpcall
  25.  
  26. function _G.pcall(...)
  27.     local ok, err = _pcall(...)
  28.     if not ok then last_error = err end
  29.     return ok, err
  30. end
  31.  
  32. function _G.xpcall(...)
  33.     local ok, err = _xpcall(...)
  34.     if not ok then last_error = err end
  35.     return ok, err
  36. end
  37.  
  38. local is_running = true
  39.  
  40. local function run()
  41.     sleep((os.getComputerID() % 3) + 1)
  42.     while true do
  43.         update("running", nil)
  44.         print "executing"
  45.         is_running = true
  46.         local exited_safely = false
  47.         parallel.waitForAny(function() shell.run(to_execute) end, function()
  48.             os.pullEvent "_restart"
  49.             exited_safely = true
  50.         end)
  51.         is_running = false
  52.         if not exited_safely then
  53.             print(last_error)
  54.             update("stopped", last_error)
  55.         end
  56.         sleep(delay)
  57.     end
  58. end
  59.  
  60. local function send_updates()
  61.     while true do
  62.         if is_running then update("running", nil) end
  63.         sleep(10)
  64.     end
  65. end
  66.  
  67. local function receive_commands()
  68.     while true do
  69.         local _, m = skynet.receive(chan)
  70.         if type(m) == "table" and m.type == "command" and m.computer == os.getComputerLabel() or m.computer == nil then
  71.             local c = m.command
  72.             if c == "restart" then
  73.                 update("restarting", nil)
  74.                 os.queueEvent "_restart"
  75.             elseif c == "update" then
  76.                 local h = http.get "https://pastebin.com/raw/D43crzmW"
  77.                 local f = fs.open("startup", "w")
  78.                 f.write(h.readAll())
  79.                 f.close()
  80.                 h.close()
  81.             end
  82.         end
  83.     end
  84. end
  85.  
  86. parallel.waitForAll(run, receive_commands, send_updates)
Add Comment
Please, Sign In to add comment