Skip to content

Commit 61ec546

Browse files
committed
Extended logging and process runner.
1 parent af29e35 commit 61ec546

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/main/java/Handler.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
import payloads.Request;
88
import payloads.Response;
99

10+
import java.io.File;
1011
import java.io.IOException;
1112
import java.net.HttpURLConnection;
1213
import java.time.Duration;
1314
import java.time.Instant;
15+
import java.util.ArrayList;
16+
import java.util.List;
1417
import java.util.Map;
1518

1619
public class Handler {
1720

18-
private static final String FOLDER_PATH = "/tmp/";
21+
private static final String TMP_PATH = "/tmp/";
1922
private static final String BUCKET_KEY = "bucket";
2023
private static final String PREFIX_KEY = "prefix";
2124

@@ -38,7 +41,7 @@ public Response handleRequest(Event event, Context context) {
3841

3942
downloadData(request, logger);
4043
downloadExecutable(request, logger);
41-
execute(request.getExecutable(), String.join(" ", request.getArgs()), logger);
44+
execute(createCommand(request), logger);
4245
uploadData(request, logger);
4346
} catch (Exception e) {
4447
return handleException(e, request);
@@ -53,19 +56,35 @@ private void downloadData(Request request, LambdaLogger logger) throws IOExcepti
5356
for (Map<String, Object> input : request.getInputs()) {
5457
String fileName = input.get("name").toString();
5558
String key = request.getOptions().get(PREFIX_KEY) + "/" + fileName;
56-
logger.log("Downloading " + request.getOptions().get(BUCKET_KEY) + "/" + key);
59+
logger.log("Downloading file: " + request.getOptions().get(BUCKET_KEY) + "/" + key);
5760
S3Object s3Object = S3Utils.getObject(request.getOptions().get(BUCKET_KEY), key);
58-
S3Utils.saveToFile(s3Object, FOLDER_PATH + fileName);
61+
S3Utils.saveToFile(s3Object, TMP_PATH + fileName);
5962
}
6063
}
6164

62-
private void execute(String executable, String args, LambdaLogger logger) throws Exception {
63-
logger.log("Executing " + executable);
65+
private void downloadExecutable(Request request, LambdaLogger logger) throws IOException {
66+
logger.log("Downloading executable file: " + request.getExecutable());
67+
String key = request.getOptions().get(PREFIX_KEY) + "/" + request.getExecutable();
68+
S3Object s3Object = S3Utils.getObject(request.getOptions().get(BUCKET_KEY), key);
69+
S3Utils.saveToFile(s3Object, TMP_PATH + request.getExecutable());
70+
}
71+
72+
private List<String> createCommand(Request request) {
73+
List<String> command = new ArrayList<>();
74+
command.add(TMP_PATH + request.getExecutable());
75+
command.addAll(request.getArgs());
76+
return command;
77+
}
78+
79+
private void execute(List<String> command, LambdaLogger logger) throws Exception {
80+
logger.log("Executing: " + String.join(" ", command));
6481

65-
Process chmod = Runtime.getRuntime().exec("chmod -R 755 " + FOLDER_PATH);
82+
Process chmod = Runtime.getRuntime().exec("chmod -R 777 " + TMP_PATH);
6683
chmod.waitFor();
67-
Process process = Runtime.getRuntime().exec(FOLDER_PATH + executable + " " + args);
68-
process.waitFor();
84+
85+
ProcessBuilder processBuilder = new ProcessBuilder(command);
86+
processBuilder.directory(new File(TMP_PATH));
87+
Process process = processBuilder.start();
6988

7089
String outputMsg = IOUtils.toString(process.getInputStream());
7190
String errorMsg = IOUtils.toString(process.getErrorStream());
@@ -77,9 +96,9 @@ private void execute(String executable, String args, LambdaLogger logger) throws
7796
private void uploadData(Request request, LambdaLogger logger) {
7897
for (Map<String, String> input : request.getOutputs()) {
7998
String fileName = input.get("name");
80-
String filePath = FOLDER_PATH + fileName;
99+
String filePath = TMP_PATH + fileName;
81100
String key = request.getOptions().get(PREFIX_KEY) + "/" + fileName;
82-
logger.log("Uploading " + request.getOptions().get(BUCKET_KEY) + "/" + key);
101+
logger.log("Uploading to " + request.getOptions().get(BUCKET_KEY) + "/" + key + " from " + filePath);
83102
S3Utils.putObject(request.getOptions().get(BUCKET_KEY), key, filePath);
84103
}
85104
}
@@ -99,12 +118,4 @@ private Response handleException(Exception e, Request request) {
99118
response.setBody("Execution of " + request.getExecutable() + " failed, cause: " + cause.getMessage());
100119
return response;
101120
}
102-
103-
private void downloadExecutable(Request request, LambdaLogger logger) throws IOException {
104-
logger.log("Downloading executable" + request.getExecutable());
105-
String key = request.getOptions().get(PREFIX_KEY) + "/" + request.getExecutable();
106-
S3Object s3Object = S3Utils.getObject(request.getOptions().get(BUCKET_KEY), key);
107-
S3Utils.saveToFile(s3Object, FOLDER_PATH + request.getExecutable());
108-
}
109-
110121
}

0 commit comments

Comments
 (0)