summaryrefslogtreecommitdiffstats
path: root/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java
blob: 18c8eccf53b3679c39006bc53b49edc98fa26e34 (plain) (blame)
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
package org.openslx.taskmanager.api;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;

import org.apache.log4j.Logger;

public abstract class SystemCommandTask extends AbstractTask
{

	private static final Logger log = Logger.getLogger( SystemCommandTask.class );

	private String[] command = null;

	private Process process = null;

	protected int timeoutSeconds = 0;

	@Override
	protected final boolean execute()
	{
		try {
			return execInternal();
		} catch ( Exception e ) {
			log.warn( "Unexpected exception when executing " + getId() + ": " + e.toString() );
			processStdErrInternal( e.toString() );
			return processEnded( -3 );
		}
	}

	private final boolean execInternal()
	{
		command = initCommandLine();
		if ( command == null || command.length == 0 ) {
			return processEnded( -1 );
		}
		for (String a : command) {
			if (a == null) {
				log.warn( "An argument from initCommandLine is null: " + Arrays.toString( command ) );
				return processEnded( -5 );
			}
		}

		ProcessBuilder pb = new ProcessBuilder( command );
		pb.directory( new File( "/" ) );
		initEnvironment( pb.environment() );

		try {

			// Create process
			try {
				process = pb.start();
			} catch ( Exception e ) {
				log.warn( "Process of task " + getId() + " died.", e );
				processStdErrInternal( e.toString() );
				return processEnded( -2 );
			}
			final Process p = process;
			processStarted();
			p.getOutputStream();

			// Read its stdout
			Thread stdout = new Thread( new Runnable() {
				@Override
				public void run()
				{
					try ( BufferedReader reader = new BufferedReader( new InputStreamReader( p.getInputStream() ) ) ) {
						String line;
						while ( ( line = reader.readLine() ) != null ) {
							synchronized ( p ) {
								processStdOutInternal( line );
							}
						}
					} catch ( Exception e ) {
						e.printStackTrace();
					}
				}
			} );
			// Read its stderr
			Thread stderr = new Thread( new Runnable() {
				@Override
				public void run()
				{
					try ( BufferedReader reader = new BufferedReader( new InputStreamReader( p.getErrorStream() ) ) ) {
						String line;
						while ( ( line = reader.readLine() ) != null ) {
							synchronized ( p ) {
								processStdErrInternal( line );
							}
						}
					} catch ( Exception e ) {
						e.printStackTrace();
					}
				}
			} );

			stdout.start();
			stderr.start();

			// Wait for everything
			int retval = 124; // Default to 124, which is what the timeout util does
			if ( this.timeoutSeconds <= 0 ) {
				retval = process.waitFor();
			} else {
				int togo = timeoutSeconds * 10;
				while ( togo-- > 0 ) {
					try {
						retval = process.exitValue();
						break;
					} catch ( IllegalThreadStateException e1 ) {
						// Still running....
						try {
							Thread.sleep( 100 );
						} catch ( Exception e2 ) {
							// Bummer....
						}
					}
				}
			}
			try {
				stdout.join( 500 );
				stderr.join( 500 );
			} catch ( Throwable t ) {
			}
			try {
				process.getErrorStream().close();
			} catch ( Throwable t ) {
			}
			try {
				process.getOutputStream().close();
			} catch ( Throwable t ) {
			}
			try {
				process.getInputStream().close();
			} catch ( Throwable t ) {
			}

			synchronized ( p ) {
				return processEnded( retval );
			}

		} catch ( InterruptedException e ) {
			processEnded( -4 );
			Thread.currentThread().interrupt();
			return false;
		} finally {
			if ( process != null )
				process.destroy();
		}
	}

	/**
	 * Write data to the process's stdin.
	 * 
	 * @param data stuff to write
	 * @return success or failure mapped to a boolean in a really complicated way
	 */
	protected final boolean toStdIn( byte[] data )
	{
		try {
			process.getOutputStream().write( data );
		} catch ( IOException e ) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * Write text to the process's stdin.
	 * 
	 * @param text stuff to write
	 * @return success or failure mapped to a boolean in a really complicated way
	 */
	protected final boolean toStdIn( String text )
	{
		return toStdIn( text.getBytes( StandardCharsets.UTF_8 ) );
	}

	private final void processStdOutInternal( String line )
	{
		try {
			processStdOut( line );
		} catch ( Throwable t ) {
			log.warn( "processStdOut failed", t );
		}
	}

	private final void processStdErrInternal( String line )
	{
		try {
			processStdErr( line );
		} catch ( Throwable t ) {
			log.warn( "processStdErr failed", t );
		}
	}

	/**
	 * Called to get the command line. Each argument should be a separate array
	 * element. Returning null means the task should not run (as the arguments
	 * were probably faulty).
	 * 
	 * @return List of arguments. First element is the command itself.
	 */
	protected abstract String[] initCommandLine();

	/**
	 * Override this to modify the environment of the process to be started.
	 * @param environment
	 */
	protected void initEnvironment( Map<String, String> environment )
	{
	}

	/**
	 * Called when the process has been successfully started.
	 */
	protected void processStarted()
	{
	}

	/**
	 * Called when the process has finished running
	 * 
	 * @param exitCode the process' exit code
	 * @return
	 */
	protected abstract boolean processEnded( int exitCode );

	/**
	 * Called when a line has been read from the process' stdout.
	 * 
	 * @param line The line read from the process, without any newline characters
	 */
	protected abstract void processStdOut( String line );

	/**
	 * Called when a line has been read from the process' stderr.
	 * Trailing newline is removed.
	 * 
	 * @param line The line read from the process, without any newline characters
	 */
	protected abstract void processStdErr( String line );

}