-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProcess.java
More file actions
246 lines (220 loc) · 7.33 KB
/
Process.java
File metadata and controls
246 lines (220 loc) · 7.33 KB
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
/*
* Copyright (C) 2016 Julien Viet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.julienviet.childprocess;
import com.julienviet.childprocess.impl.ProcessImpl;
import com.zaxxer.nuprocess.NuProcessBuilder;
import io.vertx.codegen.annotations.CacheReturn;
import io.vertx.codegen.annotations.Fluent;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A process launched from this current process.
* <p>
* Call the static {@link #spawn(Vertx, String)} to spawn child processes from the current process.
* <p>
* Please see the user manual for more detailed usage information.
*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
@VertxGen
public interface Process {
/**
* @return the current process environment variables
*/
static Map<String, String> env() {
return new HashMap<>(System.getenv());
}
/**
* Create and start a child process from this process.
*
* @param vertx the vertx instance
* @param command the command to run
* @return the process
*/
static Process spawn(Vertx vertx, String command) {
return spawn(vertx, command, Collections.emptyList(), new ProcessOptions());
}
/**
* Create and start a child process from this process.
*
* @param vertx the vertx instance
* @param command the command to run
* @param args list of string arguments
* @return the process
*/
static Process spawn(Vertx vertx, String command, List<String> args) {
return spawn(vertx, command, args, new ProcessOptions());
}
/**
* Create and start a child process from this process.
*
* @param vertx the vertx instance
* @param command the command to run
* @param options the options to run the command
* @return the process
*/
static Process spawn(Vertx vertx, String command, ProcessOptions options) {
return spawn(vertx, command, Collections.emptyList(), options);
}
/**
* Create and start a child process from this process.
*
* @param vertx the vertx instance
* @param command the command to run
* @param args list of string arguments
* @param options the options to run the command
* @return the process
*/
static Process spawn(Vertx vertx, String command, List<String> args, ProcessOptions options) {
Process process = create(vertx, command, args, options);
process.start();
return process;
}
/**
* Create a child process (not running) from this process, call {@link #start()} to start the process.
*
* @param vertx the vertx instance
* @param command the command to run
* @return the created child process
*/
static Process create(Vertx vertx, String command) {
return create(vertx, command, Collections.emptyList(), new ProcessOptions());
}
/**
* Create a child process (not running) from this process, call {@link #start()} to start the process.
*
* @param vertx the vertx instance
* @param command the command to run
* @param args list of string arguments
* @return the created child process
*/
static Process create(Vertx vertx, String command, List<String> args) {
return create(vertx, command, args, new ProcessOptions());
}
/**
* Create a child process (not running) from this process, call {@link #start()} to start the process.
*
* @param vertx the vertx instance
* @param command the command to run
* @param options the options to run the command
* @return the created child process
*/
static Process create(Vertx vertx, String command, ProcessOptions options) {
return create(vertx, command, Collections.emptyList(), options);
}
/**
* Create a child process (not running) from this process, call {@link #start()} to start the process.
*
* @param vertx the vertx instance
* @param command the command to run
* @param args list of string arguments
* @param options the options to run the command
* @return the created child process
*/
static Process create(Vertx vertx, String command, List<String> args, ProcessOptions options) {
Map<String, String> env = new HashMap<>();
if (options.getEnv() != null) {
options.getEnv().entrySet().forEach(entry -> {
if (entry.getValue() != null) {
env.put(entry.getKey(), entry.getValue());
}
});
}
ArrayList<String> commands = new ArrayList<>();
commands.add(command);
commands.addAll(args);
NuProcessBuilder builder = new NuProcessBuilder(commands, env);
if (options.getCwd() != null) {
builder.setCwd(new File(options.getCwd()).toPath());
}
return new ProcessImpl(vertx.getOrCreateContext(), builder);
}
/**
* Start the process.
*/
void start();
/**
* Start the process.
*
* @param handler the handler to be called when the process has started
*/
void start(Handler<Process> handler);
/**
* Set the handler to be called when the process exits, the handler will be called with the
* process status code value.
*
* @param handler the handler
* @return a reference to this, so the API can be used fluently
*/
@Fluent
Process exitHandler(Handler<Integer> handler);
/**
* @return the process PID or null if the process is not running
*/
Integer pid();
/**
* @return the process stdin stream
*/
@CacheReturn
StreamOutput stdin();
/**
* @return the process stdout stream
*/
@CacheReturn
StreamInput stdout();
/**
* @return the process stderr stream
*/
@CacheReturn
StreamInput stderr();
/**
* Terminates the process in a graceful manner.
* <p>
* On a POSIX OS, it sends the {@code SIGTERM}.
*/
default void kill() {
kill(false);
}
/**
* Terminates the process.
* <p>
* If {@code force} is {@code false}, the process will be terminated gracefully (i.e. its shutdown logic will
* be allowed to execute), assuming the OS supports such behavior. Note that the process may not actually
* terminate, as its cleanup logic may fail or it may choose to ignore the termination request. If a guarantee
* of termination is required, call this method with force equal to true instead.
* <p>
* If {@code force} is {@code true}, the process is guaranteed to terminate, but whether it is terminated
* gracefully or not is OS-dependent. Note that it may take the OS a moment to terminate the process, so
* {@link #isRunning()} may return {@code true} for a brief period after calling this method.
* <p>
* On a POSIX OS, it sends the {@code SIGTERM} or {@code SIGKILL} signals.
*
* @param force if true is passed, the process will be forcibly killed
*/
void kill(boolean force);
/**
* Tests whether or not the process is still running or has exited.
*/
boolean isRunning();
}