Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

101
LINES

< > BotCompany Repo | #1019683 // Watch Dog for Stefan's OS [external part, see #1019696]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 5404K of libraries. Click here for Pure Java version (11411L/82K).

!7

sS iconID = /*#1101425*/#1101427;
static double delay = 10.0, timeout = 10.0; // change for fast computers
static int consecutiveFailsThreshold = 3; // TODO: change if loading
sS osBotName = "Stefan's OS.";

sbool v6;
static int consecutiveFails;
static volatile S status;
static TrayIcon trayIcon;

svoid activateOS {
  sendOpt("Stefan'S OS", "activate frames");
}

sS osProgramID;

p {
  // TODO
  osProgramID = or(get(args, 0), #1016005);
  S homeDir = get(args, 0);
  
  if (isAbsolutePath(homeDir)) _userHome = args[0];
  print("Home: " + _userHome);
  
  // TODO: put "Restart OS Now" in consoleMemoryView()'s popup menu

  bot(stefansOS_watchDogBotName());
  trayIcon = installTrayIcon(iconID, dropSuffix(".", stefansOS_watchDogBotName()),
    r showConsole,
    "Restart OS Now", r restartOSNow,
    "Show Watch Dog Window", r showConsole,
    "Hide Watch Dog Window", r hideConsole,
    "Exit Watch Dog", rThread cleanKill);
  consoleIcon(iconID);
  //printWithDateAndTimeInThisThread();
  doEvery(delay, r {
    bool ok = false;
    pcall {
      long time = sysNow();
      S s = sendOptWithTimeout(timeout, osBotName, "swing latency");
      if (isInteger(s)) {
        hideConsole();
        ok = true;
        consecutiveFails = 0;
        setStatus("Stefan's OS OK - Swing latency " + s + " ms, " + (sysNow()-time) + " ms");
        //v6 = isProgramRunning(#1016478);
        S progID = evalWithTimeoutOrNull(timeout, func -> S {
          send("Stefan's OS", "program id")
        });
        if (isSnippetID(progID)) osProgramID = or(progID, osProgramID);
      } else
        showConsole();
      consoleStatus(ok ? "OK" : "FAIL");
    }
    
    if (!ok) {
      ++consecutiveFails;
      printAndProgramLog(status = "Stefan's OS fail #" + consecutiveFails + " of " + consecutiveFailsThreshold);
      S stackTraces = sendOptWithTimeout(timeout, osBotName, "stack traces");
      if (nempty(stackTraces)) {
        stackTraces = unquote(stackTraces);
        print(stackTraces);
        saveTextFile(newFile(stefansOS_watchDogStackTracesDir(), "stack-trace-" +  ymd_minus_hms() + ".txt"), stackTraces);
      }
      pcall { osFailActivity(); }
    }
  });
  hideConsole();
  
  quickRestartOnSocketLoss();
  sleep();
}

svoid osFailActivity {
  if (consecutiveFails >= consecutiveFailsThreshold)
    restartOSNow();
}

svoid restartOSNow {
  printAndProgramLog("RESTARTING STEFAN'S OS " + (v6 ? "v6" : "v5") + ".");
  consecutiveFails = 0;
  hardKillStefansOS();
  nohupJavax(osProgramID);
}

answer {
  if "status" ret status;
}

// TODO
svoid quickRestartOnSocketLoss {
  /*int port = getVMPortForBot(osBotName);
  if (port != 0) talkTo(port);*/
}

svoid setStatus(S status) {
  printAndProgramLog(main.status = status);
  setTrayIconToolTip(trayIcon, status);
}

Author comment

Began life as a copy of #1012699

download  show line numbers  debug dex  old transpilations   

Travelled to 26 computer(s): bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, dbzfplsxganw, ekrmjmnbrukm, elmgxqgtpvxh, fzcpzdstiiyc, gwrvuhgaqvyk, hpgrupgrauku, irmadwmeruwu, ishqpsrjomds, jcllbfdqhrgy, jozkyjcghlvl, lqnftawlhpir, mqqgnosmbjvj, omdjrrnzbjjv, pcsjzfqafodr, pyentgdyhuwx, pzhvpgtvlbxg, snaazhdonpnp, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, wpzdwgqboxjy, xrpafgyirdlv, znvaruejrphg

No comments. add comment

Snippet ID: #1019683
Snippet name: Watch Dog for Stefan's OS [external part, see #1019696]
Eternal ID of this version: #1019683/38
Text MD5: ea58e9d6de587da921c012bddea60f05
Transpilation MD5: 03600803d0e3f4b3a09cb1862f55bcc5
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-14 17:26:41
Source code size: 2888 bytes / 101 lines
Pitched / IR pitched: No / No
Views / Downloads: 538 / 10744
Version history: 37 change(s)
Referenced in: #1016478 - Stefan's OS v6
#1019696 - Watch Dog [restarts OS when it hangs]
#1022729 - Stefan's OS v6 - copy for transpiler test
#1024922 - programPackager_standardExcludedSnippets
#1024932 - Stefan's OS v7 [LIVE]
#1028187 - Discord Audio Bots Watch Dog
#1031320 - Stefan's OS v7 [backup before allowing dynamic main classes]