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).

1  
!7
2  
3  
sbool smartBotToo = false;
4  
sS url = "http://localhost:8080/1010504/raw/number-of-threads";
5  
sS mainURL = "http://localhost";
6  
sS smartBotURL = "http://localhost:4678/number-of-threads";
7  
static double timeout = 60.0, delay = 30.0;
8  
static int consecutiveFailsThreshold = /*2*/ 4; // times delay
9  
static int consecutiveFailsThresholdWhenLoading = 20; // not used
10  
11  
static int consecutiveFails;
12  
static volatile S status;
13  
sbool playMode;
14  
15  
p {
16  
  bot("New Eleu Watch Dog.");
17  
  
18  
  if (cic(args, "playMode")) set playMode;
19  
  
20  
  if (smartBotToo) thread "Smart Bot Loop" { smartBotLoop(); }
21  
  
22  
  printWithDateAndTimeInThisThread();
23  
  repeat with sleep delay {
24  
    bool ok = false, loading = false;
25  
    pcall {
26  
      long time = sysNow();
27  
      
28  
      S mainData = loadPageWithTimeout(mainURL, timeout/2);
29  
      if (!startsWith(mainData, "<") || swic(mainData, "ERROR")) {
30  
        print("Error! " + mainData);
31  
      } else {
32  
        S s = loadPageWithTimeout(url, timeout/2);
33  
        if (startsWith(s, "LOADING")) loading = true; // TODO: loading is now code 500
34  
        if (isInteger(s)) {
35  
          ok = true;
36  
          consecutiveFails = 0;
37  
          print(status = "Server OK - " + n2(parseLong(s), "thread", "threads") + ", " + (sysNow()-time) + " ms");
38  
        }
39  
      }
40  
    }
41  
    
42  
    if (!ok) {
43  
      ++consecutiveFails;
44  
      print(status = "Server fail #" + consecutiveFails + " of " + actualThreshold(loading) + (loading ? " (loading)" : ""));
45  
      pcall { serverFailActivity(loading); }
46  
    }
47  
  }
48  
}
49  
50  
static int actualThreshold(bool loading) {
51  
  ret loading ? consecutiveFailsThresholdWhenLoading : consecutiveFailsThreshold;
52  
}
53  
54  
svoid serverFailActivity(bool loading) {
55  
  if (consecutiveFails == actualThreshold(loading)) {
56  
    consecutiveFails = 0;
57  
    if (playMode) ret with print("WOULD RESTART ELEU.");
58  
    print("RESTARTING ELEU.");
59  
    hardKillProgram(#1013896);
60  
    S eleuOptions = loadTextFileTrim(javaxDataDir("eleu.options"));
61  
    directNohupJavax(joinNemptiesWithSpace(eleuOptions, str(psI(#1013896))));
62  
  }
63  
}
64  
65  
answer {
66  
  if "status" ret status;
67  
}
68  
69  
svoid smartBotLoop {
70  
  int consecutiveFails = 0;
71  
  S status = "";
72  
  printWithDateAndTimeInThisThread();
73  
  repeat with sleep delay {
74  
    bool ok = false, loading = false;
75  
    pcall {
76  
      long time = sysNow();
77  
      S s = loadPageWithTimeout(smartBotURL, timeout);
78  
      if (startsWith(s, "LOADING")) loading = true;
79  
      if (isInteger(s)) {
80  
        ok = true;
81  
        consecutiveFails = 0;
82  
        print(status = "Smart Bot OK - " + n2(parseLong(s), "thread", "threads") + ", " + (sysNow()-time) + " ms");
83  
      }
84  
    }
85  
    
86  
    if (!ok) {
87  
      ++consecutiveFails;
88  
      print(status = "Smart Bot fail #" + consecutiveFails + " of " + actualThreshold(loading) + (loading ? " (loading)" : ""));
89  
      pcall {
90  
        if (smartBotFailActivity(consecutiveFails, loading))
91  
          consecutiveFails = 0;
92  
      }
93  
    }
94  
  }
95  
}
96  
97  
sbool smartBotFailActivity(int consecutiveFails, bool loading) {
98  
  if (consecutiveFails >= actualThreshold(loading)) {
99  
    print("RESTARTING SMART BOT.");
100  
    hardKillProgram(#1010745);
101  
    nohupJavaxHeadless(#1010745);
102  
    true;
103  
  }
104  
  false;
105  
}

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: 223 / 811
Referenced in: [show references]