-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
MainClass.java
75 lines (60 loc) · 2.14 KB
/
MainClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package nu.marginalia.service;
import io.prometheus.client.hotspot.DefaultExports;
import io.reactivex.rxjava3.exceptions.UndeliverableException;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import nu.marginalia.service.id.ServiceId;
import nu.marginalia.client.exception.NetworkException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
/** Each main class of a service should extend this class.
* They must also invoke init() in their main method.
*/
public abstract class MainClass {
private static final Logger logger = LoggerFactory.getLogger(MainClass.class);
static {
// Load global config ASAP
ConfigLoader.loadConfig(
ConfigLoader.getConfigPath("system")
);
}
public MainClass() {
RxJavaPlugins.setErrorHandler(this::handleError);
}
protected void handleError(Throwable ex) {
if (ex instanceof UndeliverableException) {
ex = ex.getCause();
}
if (ex instanceof SocketTimeoutException) {
logger.warn("SocketTimeoutException");
}
else if (ex instanceof UnknownHostException) {
logger.warn("UnknownHostException");
}
else if (ex instanceof NetworkException) {
logger.warn("NetworkException", ex);
}
else {
logger.error("Uncaught exception", ex);
}
}
protected static void init(ServiceId id, String... args) {
System.setProperty("log4j2.isThreadContextMapInheritable", "true");
System.setProperty("isThreadContextMapInheritable", "true");
System.setProperty("service-name", id.name);
ConfigLoader.loadConfig(
ConfigLoader.getConfigPath(id.name)
);
initJdbc();
initPrometheus();
}
private static void initJdbc() {
// This looks weird, but it's just for running the static block
// in the driver class so that it registers itself
new org.mariadb.jdbc.Driver();
}
private static void initPrometheus() {
DefaultExports.initialize();
}
}