/
WrapperService.java
470 lines (408 loc) · 17.6 KB
/
WrapperService.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.wrapper;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import org.jumpmind.symmetric.wrapper.Constants.Status;
import com.sun.jna.Platform;
public abstract class WrapperService {
private static final Logger logger = Logger.getLogger(WrapperService.class.getName());
protected WrapperConfig config;
protected boolean keepRunning = true;
protected Process child;
protected BufferedReader childReader;
private static WrapperService instance;
public static WrapperService getInstance() {
if (Platform.isWindows()) {
instance = new WindowsService();
} else {
instance = new UnixService();
}
return instance;
}
public void loadConfig(String applHomeDir, String configFile, String jarFile) throws IOException {
config = new WrapperConfig(applHomeDir, configFile, jarFile);
setWorkingDirectory(config.getWorkingDirectory().getAbsolutePath());
}
public void start() {
if (isRunning()) {
throw new WrapperException(Constants.RC_SERVER_ALREADY_RUNNING, 0, "Server is already running");
}
stopProcesses(true);
System.out.println("Waiting for server to start");
ArrayList<String> cmdLine = getWrapperCommand("exec");
Process process = null;
boolean success = false;
int rc = 0;
try {
ProcessBuilder pb = new ProcessBuilder(cmdLine);
pb.redirectErrorStream(true);
process = pb.start();
if (!(success = waitForPid(getProcessPid(process)))) {
rc = process.exitValue();
}
} catch (IOException e) {
rc = -1;
System.out.println(e.getMessage());
}
if (success) {
System.out.println("Started");
} else {
System.err.println(commandToString(cmdLine));
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.err.println(line);
}
reader.close();
} catch (Exception e) {
}
throw new WrapperException(Constants.RC_FAIL_EXECUTION, rc, "Failed second stage");
}
}
public void init() {
execJava(false);
}
public void console() {
if (isRunning()) {
throw new WrapperException(Constants.RC_SERVER_ALREADY_RUNNING, 0, "Server is already running");
}
execJava(true);
}
protected void execJava(boolean isConsole) {
try {
LogManager.getLogManager().reset();
WrapperLogHandler handler = new WrapperLogHandler(config.getLogFile(),
config.getLogFileMaxSize(), config.getLogFileMaxFiles());
handler.setFormatter(new WrapperLogFormatter());
Logger rootLogger = Logger.getLogger("");
rootLogger.setLevel(Level.parse(config.getLogFileLogLevel()));
rootLogger.addHandler(handler);
} catch (IOException e) {
throw new WrapperException(Constants.RC_FAIL_WRITE_LOG_FILE, 0, "Cannot open log file " + config.getLogFile(), e);
}
try {
int pid = getCurrentPid();
writePidToFile(pid, config.getWrapperPidFile());
logger.log(Level.INFO, "Started wrapper as PID " + pid);
ArrayList<String> cmd = config.getCommand(isConsole);
String cmdString = commandToString(cmd);
boolean usingHeapDump = cmdString.indexOf("-XX:+HeapDumpOnOutOfMemoryError") != -1;
logger.log(Level.INFO, "Working directory is " + System.getProperty("user.dir"));
long startTime = 0;
int startCount = 0;
boolean startProcess = true, restartDetected = false;
int serverPid = 0;
while (keepRunning) {
if (startProcess) {
logger.log(Level.INFO, "Executing " + cmdString);
if (startCount == 0) {
updateStatus(Status.START_PENDING);
}
startTime = System.currentTimeMillis();
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
try {
child = pb.start();
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to execute: " + e.getMessage());
updateStatus(Status.STOPPED);
throw new WrapperException(Constants.RC_FAIL_EXECUTION, -1, "Failed executing server", e);
}
serverPid = getProcessPid(child);
logger.log(Level.INFO, "Started server as PID " + serverPid);
writePidToFile(serverPid, config.getServerPidFile());
if (startCount == 0) {
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
updateStatus(Status.RUNNING);
}
startProcess = false;
startCount++;
} else {
try {
logger.log(Level.INFO, "Watching output of java process");
childReader = new BufferedReader(new InputStreamReader(child.getInputStream()));
String line = null;
while ((line = childReader.readLine()) != null) {
System.out.println(line);
logger.log(Level.INFO, line, "java");
if ((usingHeapDump && line.matches("Heap dump file created.*")) ||
(!usingHeapDump && line.matches("java.lang.OutOfMemoryError.*")) ||
line.matches(".*java.net.BindException.*") ||
line.matches(".*A fatal error has been detected.*")) {
logger.log(Level.SEVERE, "Stopping server because its output matches a failure condition");
child.destroy();
childReader.close();
stopProcess(serverPid, "server");
break;
}
if (line.equalsIgnoreCase("Restarting")) {
restartDetected = true;
}
}
logger.log(Level.INFO, "End of output from java process");
} catch (IOException e) {
logger.log(Level.SEVERE, "Error while reading from process");
}
if (restartDetected) {
logger.log(Level.INFO, "Restart detected");
restartDetected = false;
startProcess = true;
} else if (keepRunning) {
long tenminutesinms = 60 * 10 * 1000;
long twelveminutesinms = 60 * 12 * 1000;
long tensecondsinms = 10 * 1000;
long now = System.currentTimeMillis();
while(child.isAlive()) {
logger.log(Level.WARNING, "Server process has not stopped yet");
if(System.currentTimeMillis() - now > twelveminutesinms) {
logger.log(Level.SEVERE, "Server process never exited, exiting now");
child.destroyForcibly();
// If it has not exited by now, it probably never will.
break;
}
if(System.currentTimeMillis() - now > tenminutesinms) {
logger.log(Level.SEVERE, "Server process never exited, trying to force the exit of server process");
child.destroyForcibly();
}
try {
Thread.sleep(tensecondsinms);
} catch(InterruptedException e) { }
}
logger.log(Level.SEVERE, "Unexpected exit from server: " + child.exitValue());
long runTime = System.currentTimeMillis() - startTime;
if (System.currentTimeMillis() - startTime < 7000) {
logger.log(Level.SEVERE, "Stopping because server exited too quickly after only " + runTime + " milliseconds");
updateStatus(Status.STOPPED);
throw new WrapperException(Constants.RC_SERVER_EXITED, child.exitValue(), "Unexpected exit from server");
} else {
startProcess = true;
}
}
}
}
} catch (Throwable ex) {
// The default logging config doesn't show the stack trace here, so include it in the message.
try {
logger.log(Level.SEVERE, "Exception caught.\r\n" + getStackTrace(ex));
updateStatus(Status.STOPPED);
throw new WrapperException(Constants.RC_SERVER_EXITED, child.exitValue(), "Exception caught.");
} catch (Throwable ex2) {
ex2.printStackTrace();
}
}
}
public void stop() {
stopProcesses(false);
deletePidFile(config.getServerPidFile());
deletePidFile(config.getWrapperPidFile());
System.out.println("Stopped");
}
protected void stopProcesses(boolean isStopAbandoned) {
int serverPid = readPidFromFile(config.getServerPidFile());
int wrapperPid = readPidFromFile(config.getWrapperPidFile());
boolean isServerRunning = isPidRunning(serverPid);
boolean isWrapperRunning = isPidRunning(wrapperPid);
if (!isStopAbandoned) {
if (!isServerRunning && !isWrapperRunning) {
throw new WrapperException(Constants.RC_SERVER_NOT_RUNNING, 0, "Server is not running");
}
System.out.println("Waiting for server to stop");
}
if (isWrapperRunning) {
if (isStopAbandoned) {
System.out.println("Stopping abandoned wrapper PID " + wrapperPid);
}
isWrapperRunning = !stopProcess(wrapperPid, "wrapper");
}
if (isServerRunning) {
if (isStopAbandoned) {
System.out.println("Stopping abandoned server PID " + serverPid);
}
isServerRunning = !stopProcess(serverPid, "server");
}
if (isWrapperRunning || isServerRunning) {
throw new WrapperException(Constants.RC_FAIL_STOP_SERVER, 0, "Server did not stop");
}
}
protected boolean stopProcess(int pid, String name) {
killProcess(pid, false);
if (waitForPid(pid)) {
killProcess(pid, true);
if (waitForPid(pid)) {
System.out.println("ERROR: '" + name + "' did not stop");
return false;
}
}
return true;
}
protected void shutdown() {
if (keepRunning) {
keepRunning = false;
new Thread() {
@Override
public void run() {
logger.log(Level.INFO, "Stopping server");
child.destroy();
try {
childReader.close();
} catch (IOException e) {
}
logger.log(Level.INFO, "Stopping wrapper");
deletePidFile(config.getWrapperPidFile());
deletePidFile(config.getServerPidFile());
updateStatus(Status.STOPPED);
System.exit(0);
}
}.start();
}
}
public void restart() {
if (isRunning()) {
stop();
}
start();
}
public void relaunchAsPrivileged(String className) {
}
public void status() {
boolean isRunning = isRunning();
int wrapperPid = readPidFromFile(config.getWrapperPidFile());
int serverPid = readPidFromFile(config.getServerPidFile());
System.out.println("Installed: " + isInstalled());
System.out.println("Running: " + isRunning);
System.out.println("Wrapper PID: " + wrapperPid);
System.out.println("Wrapper Running: " + isPidRunning(wrapperPid));
System.out.println("Server PID: " + serverPid);
System.out.println("Server Running: " + isPidRunning(serverPid));
}
public boolean isRunning() {
return isPidRunning(readPidFromFile(config.getWrapperPidFile()))
&& isPidRunning(readPidFromFile(config.getServerPidFile()));
}
public int getWrapperPid() {
return readPidFromFile(config.getWrapperPidFile());
}
public int getServerPid() {
return readPidFromFile(config.getServerPidFile());
}
protected String commandToString(ArrayList<String> cmd) {
StringBuilder sb = new StringBuilder();
for (String c : cmd) {
sb.append(c).append(" ");
}
return sb.toString();
}
protected ArrayList<String> getWrapperCommand(String arg) {
ArrayList<String> cmd = new ArrayList<String>();
String quote = getWrapperCommandQuote();
cmd.add(quote + config.getJavaCommand() + quote);
cmd.add("-Djava.io.tmpdir="+quote+System.getProperty("java.io.tmpdir")+quote);
cmd.add("-jar");
cmd.add(quote + config.getWrapperJarPath() + quote);
cmd.add(arg);
cmd.add(quote + config.getConfigFile() + quote);
return cmd;
}
protected ArrayList<String> getPrivilegedCommand() {
ArrayList<String> cmd = new ArrayList<String>();
String quote = getWrapperCommandQuote();
cmd.add(quote + config.getJavaCommand() + quote);
return cmd;
}
protected String getWrapperCommandQuote() {
return "";
}
protected int readPidFromFile(String filename) {
int pid = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
pid = Integer.parseInt(reader.readLine());
reader.close();
} catch (FileNotFoundException e) {
} catch (Exception e) {
e.printStackTrace();
}
return pid;
}
protected void writePidToFile(int pid, String filename) {
try {
FileWriter writer = new FileWriter(filename, false);
writer.write(String.valueOf(pid));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void deletePidFile(String filename) {
new File(filename).delete();
}
protected boolean waitForPid(int pid) {
int seconds = 0;
while (seconds <= 5) {
System.out.print(".");
if (!isPidRunning(pid)) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
seconds++;
}
System.out.println("");
return isPidRunning(pid);
}
protected void updateStatus(Status status) {
}
private static String getStackTrace(Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
throwable.printStackTrace(pw);
return sw.getBuffer().toString();
}
class ShutdownHook extends Thread {
public void run() {
shutdown();
}
}
public abstract void install();
public abstract void uninstall();
public abstract boolean isInstalled();
public abstract boolean isPrivileged();
protected abstract boolean setWorkingDirectory(String dir);
protected abstract int getProcessPid(Process process);
protected abstract int getCurrentPid();
protected abstract boolean isPidRunning(int pid);
protected abstract void killProcess(int pid, boolean isTerminate);
}