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

83
LINES

< > BotCompany Repo | #1028187 // Discord Audio Bots Watch Dog

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

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

!7

sS iconID = #1101427;
static double interval = 60.0, timeout = 30.0; // change for fast computers
static int consecutiveFailsThreshold = 3; // TODO: change if loading
sS osBotName = "Discord Bots OS";
sS vmArgs = "--add-opens java.base/jdk.internal.loader=ALL-UNNAMED --add-opens java.base/jdk.internal.module=ALL-UNNAMED --add-opens java.base/jdk.internal.ref=ALL-UNNAMED --add-opens java.base/jdk.internal.reflect=ALL-UNNAMED --add-opens java.base/java.lang.module=ALL-UNNAMED --illegal-access=permit -Xmx1g";

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

sS osProgramID;

p {
  // TODO
  osProgramID = or(get(args, 0), #1025342);

  // TODO: put "Restart OS Now" in consoleMemoryView()'s popup menu

  S myName = "Watch dog for " + osBotName;
  bot(myName);
  trayIcon = installTrayIcon(iconID, myName,
    r showConsole,
    "Restart " + osBotName + " Now", r restartOSNow,
    "Show Watch Dog Window", r showConsole,
    "Hide Watch Dog Window", r hideConsole,
    "Exit Watch Dog", rThread cleanKill);
  consoleIcon(iconID);
  //printWithDateAndTimeInThisThread();
  doEvery(interval, r {
    bool ok = false;
    pcall {
      long time = sysNow();
      S s = sendOptWithTimeout(timeout, osBotName, "live check");
      if "i'm alive" {
        hideConsole();
        ok = true;
        consecutiveFails = 0;
        setStatus(osBotName + " OK");
      } else
        showConsole();
      consoleStatus(ok ? "OK" : "FAIL");
    }
    
    if (!ok) {
      ++consecutiveFails;
      printAndProgramLog(status = osBotName + " #" + consecutiveFails + " of " + consecutiveFailsThreshold);
      /*S stackTraces = sendOptWithTimeout(timeout, osBotName, "stack traces");
      if (nempty(stackTraces)) {
        stackTraces = unquote(stackTraces);
        print(stackTraces);
        saveTextFile(newFile(stefansOS_watchDogStackTracesDir(), "discord-audio-stack-trace-" +  ymd_minus_hms() + ".txt"), stackTraces);
      }*/
      pcall { osFailActivity(); }
    }
  });
  hideConsole();
  
  sleep();
}

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

svoid restartOSNow {
  printAndProgramLog("RESTARTING " + osBotName + ".");
  consecutiveFails = 0;
  hardKillProgram(osProgramID, level := 9);
  nohupJavax(osProgramID, vmArgs);
}

answer {
  if "status" ret status;
}

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

Author comment

Began life as a copy of #1019683

download  show line numbers  debug dex  old transpilations   

Travelled to 9 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028187
Snippet name: Discord Audio Bots Watch Dog
Eternal ID of this version: #1028187/8
Text MD5: edcc3b3f697686088cf5a72c3bb4a10b
Transpilation MD5: f4044e64768b9872ccef218dcc5a7ac4
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-11-10 12:44:40
Source code size: 2571 bytes / 83 lines
Pitched / IR pitched: No / No
Views / Downloads: 182 / 817
Version history: 7 change(s)
Referenced in: [show references]