Skip to content
This repository was archived by the owner on Nov 24, 2018. It is now read-only.

Commit e778096

Browse files
committed
Auto restart on error
Automatically restart on error
1 parent 9b4466e commit e778096

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package net.olympiccode.vhackos.bot.core.misc;
2+
3+
import net.olympiccode.vhackos.bot.core.BotService;
4+
import net.olympiccode.vhackos.bot.core.vHackOSBot;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.ScheduledExecutorService;
10+
import java.util.concurrent.ThreadFactory;
11+
import java.util.concurrent.TimeUnit;
12+
13+
public class MaintenanceService implements BotService {
14+
ScheduledExecutorService mainService;
15+
Logger LOG = LoggerFactory.getLogger("MainService");
16+
public MaintenanceService() {
17+
LOG.info("Creating MainService...");
18+
mainService = Executors.newScheduledThreadPool(1, new MainServiceFactory());
19+
}
20+
21+
@Override
22+
public ScheduledExecutorService getService() {
23+
return mainService;
24+
}
25+
26+
@Override
27+
public void setup() {
28+
LOG.info("Setting up MainSerice...");
29+
mainService.scheduleAtFixedRate(() -> runService(), 60000 * 5, 60000 * 5, TimeUnit.MILLISECONDS);
30+
}
31+
32+
int miscAttempts = 0;
33+
int netAttempts = 0;
34+
int upAttempts = 0;
35+
@Override
36+
public void runService() {
37+
if (vHackOSBot.miscService.miscService.isTerminated() || vHackOSBot.miscService.miscService.isTerminated() && miscAttempts < 4) {
38+
miscAttempts++;
39+
LOG.info("Restarting MiscSerivce has it stopped due to an error (Attempt " + miscAttempts + ")");
40+
vHackOSBot.miscService.setup();
41+
}
42+
if (vHackOSBot.networkingService.getService().isTerminated() || vHackOSBot.networkingService.getService().isTerminated() && netAttempts < 4) {
43+
netAttempts++;
44+
LOG.info("Restarting NetworkingSerivce has it stopped due to an error (Attempt " + netAttempts + ")");
45+
vHackOSBot.networkingService.setup();
46+
}
47+
if (vHackOSBot.updateService.getService().isTerminated() || vHackOSBot.updateService.getService().isTerminated() && upAttempts < 4) {
48+
upAttempts++;
49+
LOG.info("Restarting UpdateSerivce has it stopped due to an error (Attempt " + upAttempts + ")");
50+
vHackOSBot.updateService.setup();
51+
}
52+
}
53+
54+
public class MainServiceFactory implements ThreadFactory {
55+
public Thread newThread(Runnable r) {
56+
return new Thread(r, "vHackOSBot-MainService");
57+
}
58+
}
59+
}

src/main/java/net/olympiccode/vhackos/bot/core/misc/MiscService.java

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public ScheduledExecutorService getService() {
2929

3030
public void setup() {
3131
LOG.info("Setting up MiscSerice...");
32+
if (miscService.isTerminated() || miscService.isShutdown()) {
33+
miscService = Executors.newScheduledThreadPool(1, new MiscServiceFactory());
34+
}
3235
miscService.scheduleAtFixedRate(() -> runLongService(), 0, 60000 * 60, TimeUnit.MILLISECONDS);
3336
miscService.scheduleAtFixedRate(() -> runService(), 0, 60000 * 5, TimeUnit.MILLISECONDS);
3437
}

src/main/java/net/olympiccode/vhackos/bot/core/networking/NetworkingService.java

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public ScheduledExecutorService getService() {
4848

4949
public void setup() {
5050
LOG.info("Setting up NetworkingSerice...");
51+
if (networkingService.isTerminated() || networkingService.isShutdown()) {
52+
networkingService = Executors.newScheduledThreadPool(1, new NetworkingServiceFactory());
53+
}
5154
networkingService.scheduleAtFixedRate(() -> runService(), 0, 60000, TimeUnit.MILLISECONDS);
5255
}
5356

src/main/java/net/olympiccode/vhackos/bot/core/updating/UpdateService.java

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public ScheduledExecutorService getService() {
3535

3636
public void setup() {
3737
LOG.info("Setting up UpdateSerice...");
38+
if (updateService.isTerminated() || updateService.isShutdown()) {
39+
updateService = Executors.newScheduledThreadPool(1, new UpdateServiceFactory());
40+
}
3841
updateService.scheduleAtFixedRate(()->runService(), 0, 30000, TimeUnit.MILLISECONDS);
3942
}
4043

src/main/java/net/olympiccode/vhackos/bot/core/vHackOSBot.java

+23-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.olympiccode.vhackos.bot.core.config.AdvancedConfigValues;
1212
import net.olympiccode.vhackos.bot.core.config.ConfigFile;
1313
import net.olympiccode.vhackos.bot.core.config.ConfigValues;
14+
import net.olympiccode.vhackos.bot.core.misc.MaintenanceService;
1415
import net.olympiccode.vhackos.bot.core.misc.MiscConfigValues;
1516
import net.olympiccode.vhackos.bot.core.misc.MiscService;
1617
import net.olympiccode.vhackos.bot.core.networking.NetworkingConfigValues;
@@ -19,6 +20,7 @@
1920
import net.olympiccode.vhackos.bot.core.updating.UpdateService;
2021
import okhttp3.Request;
2122
import okhttp3.Response;
23+
import org.json.JSONException;
2224
import org.json.JSONObject;
2325
import org.slf4j.Logger;
2426
import org.slf4j.LoggerFactory;
@@ -37,10 +39,10 @@ public class vHackOSBot {
3739
static Logger LOG = LoggerFactory.getLogger("vHackOSBot");
3840
ConfigFile config = new ConfigFile();
3941
AdvancedConfigFile advConfig = new AdvancedConfigFile();
40-
BotService updateService = new UpdateService();
41-
MiscService miscService = new MiscService();
42-
BotService networkingService = new NetworkingService();
43-
42+
public static BotService updateService = new UpdateService();
43+
public static MiscService miscService = new MiscService();
44+
public static BotService networkingService = new NetworkingService();
45+
public static BotService maintenanceService = new MaintenanceService();
4446
public static void main(String[] args) {
4547
try {
4648
new vHackOSBot().run();
@@ -49,7 +51,7 @@ public static void main(String[] args) {
4951
} catch (InterruptedException e) {
5052
LOG.error("There was a problem initializing the vHackOSBot.");
5153
} catch (RuntimeException e) {
52-
if (e.getMessage().contains("vhack account has been banned")) {
54+
if (e.getMessage() != null && e.getMessage().contains("vhack account has been banned")) {
5355
LOG.error("Your vhack account has been banned.");
5456
System.exit(0);
5557
} else {
@@ -101,6 +103,7 @@ public void run() throws LoginException, InterruptedException {
101103
} else {
102104
api = new vHackOSAPIBuilder().setUsername(ConfigValues.username).setPassword(ConfigValues.password).buildBlocking();
103105
}
106+
checkForUpdates();
104107
advConfig.getConfigJson().addProperty("login.accesstoken", ((vHackOSAPIImpl) api).getAccessToken());
105108
advConfig.getConfigJson().addProperty("login.uid", ((vHackOSAPIImpl) api).getUid());
106109
advConfig.save();
@@ -115,7 +118,8 @@ public void run() throws LoginException, InterruptedException {
115118
if (UpdateConfigValues.enabled) updateService.setup();
116119
if (MiscConfigValues.enabled) miscService.setup();
117120
if (NetworkingConfigValues.enabled) networkingService.setup();
118-
121+
maintenanceService.setup();
122+
networkingService.getService().shutdownNow();
119123
} catch (Exception e) {
120124
Sentry.capture(e);
121125
e.printStackTrace();
@@ -149,7 +153,8 @@ public void run() throws LoginException, InterruptedException {
149153
case "services":
150154
System.out.println("NetworkingService: " + getStatus(networkingService.getService()) + "\n" +
151155
"UpdateService: " + getStatus(updateService.getService()) + "\n" +
152-
"MiscService: " + getStatus(miscService.getService()));
156+
"MiscService: " + getStatus(miscService.getService()) + "\n" +
157+
"MainService: " + getStatus(maintenanceService.getService()));
153158
break;
154159
case "apps":
155160
System.out.println("-------------------\n" + api.getAppManager().getApps().stream().map(app -> app.getType().getName() + ": " + (app.isInstalled() ? app.getLevel() : "Not installed")).collect(Collectors.joining("\n")) + "\n-------------------");
@@ -205,18 +210,24 @@ private String getProgressBar() {
205210
builder.append("] " + api.getStats().getLevelPorcentage() + "%");
206211
return builder.toString();
207212
}
208-
213+
double curVersion = 1.8;
209214
public void checkForUpdates() {
210-
//
211-
//
212-
Request request = (new Request.Builder()).url("https://api.github.com/repos/OlympicCode/vHackOSBot-Java/releases/latest").addHeader("user-agent", "Dalvik/1.6.0 (Linux; U; Android 4.4.4; SM-N935F Build/KTU84P)").addHeader("Accept-Encoding", "gzip").build();
215+
Request request = (new Request.Builder()).url("https://api.github.com/repos/OlympicCode/vHackOSBot-Java/releases/latest").addHeader("user-agent", "Dalvik/1.6.0 (Linux; U; Android 4.4.4; SM-N935F Build/KTU84P)").build();
213216
try {
214217
Response r = ((vHackOSAPIImpl) api).getRequester().getHttpClient().newCall(request).execute();
215218
if (r.isSuccessful()) {
216-
JSONObject json = new JSONObject(r.body());
219+
String s = r.body().string();
220+
JSONObject json = new JSONObject(s);
221+
double version = json.getDouble("tag_name");
222+
if (version != curVersion) {
223+
LOG.info("ATTENTION: An update is avaliable for vHackOSBot: " + version + " (Running " + curVersion + ")");
224+
}
225+
217226
}
218227
} catch (IOException e) {
219228
e.printStackTrace();
229+
} catch (JSONException e) {
230+
e.printStackTrace();
220231
}
221232
}
222233
}

0 commit comments

Comments
 (0)