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

105
LINES

< > BotCompany Repo | #1032545 // Watch Dog for Ada SC [dev.]

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

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

!7

sbool smartBotToo = false;
sS url = "http://localhost:8080/1010504/raw/number-of-threads";
sS mainURL = "http://localhost";
sS smartBotURL = "http://localhost:4678/number-of-threads";
static double timeout = 60.0, delay = 30.0;
static int consecutiveFailsThreshold = /*2*/ 4; // times delay
static int consecutiveFailsThresholdWhenLoading = 20; // not used

static int consecutiveFails;
static volatile S status;
sbool playMode;

p {
  bot("New Eleu Watch Dog.");
  
  if (cic(args, "playMode")) set playMode;
  
  if (smartBotToo) thread "Smart Bot Loop" { smartBotLoop(); }
  
  printWithDateAndTimeInThisThread();
  repeat with sleep delay {
    bool ok = false, loading = false;
    pcall {
      long time = sysNow();
      
      S mainData = loadPageWithTimeout(mainURL, timeout/2);
      if (!startsWith(mainData, "<") || swic(mainData, "ERROR")) {
        print("Error! " + mainData);
      } else {
        S s = loadPageWithTimeout(url, timeout/2);
        if (startsWith(s, "LOADING")) loading = true; // TODO: loading is now code 500
        if (isInteger(s)) {
          ok = true;
          consecutiveFails = 0;
          print(status = "Server OK - " + n2(parseLong(s), "thread", "threads") + ", " + (sysNow()-time) + " ms");
        }
      }
    }
    
    if (!ok) {
      ++consecutiveFails;
      print(status = "Server fail #" + consecutiveFails + " of " + actualThreshold(loading) + (loading ? " (loading)" : ""));
      pcall { serverFailActivity(loading); }
    }
  }
}

static int actualThreshold(bool loading) {
  ret loading ? consecutiveFailsThresholdWhenLoading : consecutiveFailsThreshold;
}

svoid serverFailActivity(bool loading) {
  if (consecutiveFails == actualThreshold(loading)) {
    consecutiveFails = 0;
    if (playMode) ret with print("WOULD RESTART ELEU.");
    print("RESTARTING ELEU.");
    hardKillProgram(#1013896);
    S eleuOptions = loadTextFileTrim(javaxDataDir("eleu.options"));
    directNohupJavax(joinNemptiesWithSpace(eleuOptions, str(psI(#1013896))));
  }
}

answer {
  if "status" ret status;
}

svoid smartBotLoop {
  int consecutiveFails = 0;
  S status = "";
  printWithDateAndTimeInThisThread();
  repeat with sleep delay {
    bool ok = false, loading = false;
    pcall {
      long time = sysNow();
      S s = loadPageWithTimeout(smartBotURL, timeout);
      if (startsWith(s, "LOADING")) loading = true;
      if (isInteger(s)) {
        ok = true;
        consecutiveFails = 0;
        print(status = "Smart Bot OK - " + n2(parseLong(s), "thread", "threads") + ", " + (sysNow()-time) + " ms");
      }
    }
    
    if (!ok) {
      ++consecutiveFails;
      print(status = "Smart Bot fail #" + consecutiveFails + " of " + actualThreshold(loading) + (loading ? " (loading)" : ""));
      pcall {
        if (smartBotFailActivity(consecutiveFails, loading))
          consecutiveFails = 0;
      }
    }
  }
}

sbool smartBotFailActivity(int consecutiveFails, bool loading) {
  if (consecutiveFails >= actualThreshold(loading)) {
    print("RESTARTING SMART BOT.");
    hardKillProgram(#1010745);
    nohupJavaxHeadless(#1010745);
    true;
  }
  false;
}

Author comment

Began life as a copy of #1014075

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): bhatertpkbcr, mqqgnosmbjvj

No comments. add comment

-
Snippet ID: #1032545
Snippet name: Watch Dog for Ada SC [dev.]
Eternal ID of this version: #1032545/1
Text MD5: d03d2eee1951e2d6eb451683fb1bfca2
Transpilation MD5: a94e1518726877d774fac78f7e4561b1
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-22 15:34:45
Source code size: 3235 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 222 / 808
Referenced in: