| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Copyright 2015 LinkedIn Corp. All rights reserved.
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * 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.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | package actors;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-25 20:39:37 -07:00
										 |  |  | import com.google.common.collect.ImmutableList;
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  | import java.io.File;
 | 
					
						
							|  |  |  | import java.io.FileWriter;
 | 
					
						
							|  |  |  | import java.io.IOException;
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  | import java.util.Arrays;
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  | import java.util.Properties;
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  | import wherehows.common.Constant;
 | 
					
						
							| 
									
										
										
										
											2017-08-16 21:24:38 -07:00
										 |  |  | import wherehows.common.jobs.Launcher;
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-25 20:39:37 -07:00
										 |  |  | import static org.apache.commons.lang3.StringUtils.*;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Utility class for generate cmd, write and delete config files.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							| 
									
										
										
										
											2016-05-30 08:59:05 +02:00
										 |  |  | class ConfigUtil {
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-30 08:59:05 +02:00
										 |  |  |   private static final String WH_APPLICATION_DEFAULT_DIRECTORY = "/var/tmp/wherehows";
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Generate the config file in the 'wherehows.app_folder' folder
 | 
					
						
							|  |  |  |    * The file name is {whEtlExecId}.config
 | 
					
						
							| 
									
										
										
										
											2016-05-30 08:59:05 +02:00
										 |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  |    * @param whEtlExecId
 | 
					
						
							|  |  |  |    * @param props
 | 
					
						
							|  |  |  |    * @return void
 | 
					
						
							|  |  |  |    */
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |   static void generateProperties(long whEtlExecId, Properties props, String outDir) throws IOException {
 | 
					
						
							|  |  |  |     File dir = new File(outDir);
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  |     if (!dir.exists()) {
 | 
					
						
							| 
									
										
										
										
											2017-04-21 15:50:35 -07:00
										 |  |  |       dir.mkdirs();
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2017-10-20 10:22:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     File configFile = new File(dir, whEtlExecId + ".properties");
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  |     FileWriter writer = new FileWriter(configFile);
 | 
					
						
							|  |  |  |     props.store(writer, "exec id : " + whEtlExecId + " job configurations");
 | 
					
						
							|  |  |  |     writer.close();
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |   static void deletePropertiesFile(long whEtlExecId, String outDir) {
 | 
					
						
							|  |  |  |     File configFile = new File(outDir, whEtlExecId + ".properties");
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  |     if (configFile.exists()) {
 | 
					
						
							|  |  |  |       configFile.delete();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 15:54:05 -08:00
										 |  |  |   static ProcessBuilder buildProcess(String javaCmd, String etlJobName, long whEtlExecId, String cmdParam,
 | 
					
						
							| 
									
										
										
										
											2017-03-25 20:39:37 -07:00
										 |  |  |       Properties etlJobProperties) {
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  |     String classPath = System.getProperty("java.class.path");
 | 
					
						
							| 
									
										
										
										
											2017-04-27 16:43:18 -07:00
										 |  |  |     String outDir = etlJobProperties.getProperty(Constant.WH_APP_FOLDER_KEY, WH_APPLICATION_DEFAULT_DIRECTORY);
 | 
					
						
							| 
									
										
										
										
											2017-10-20 10:22:30 -07:00
										 |  |  |     String configFile = outDir + "/" + whEtlExecId + ".properties";
 | 
					
						
							| 
									
										
										
										
											2016-05-30 08:59:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-25 20:39:37 -07:00
										 |  |  |     String[] cmdParams = isNotBlank(cmdParam) ? cmdParam.trim().split(" ") : new String[0];
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 16:43:18 -07:00
										 |  |  |     ProcessBuilder pb = new ProcessBuilder(new ImmutableList.Builder<String>().add(javaCmd)
 | 
					
						
							| 
									
										
										
										
											2017-03-25 20:39:37 -07:00
										 |  |  |         .addAll(Arrays.asList(cmdParams))
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |         .add("-cp")
 | 
					
						
							|  |  |  |         .add(classPath)
 | 
					
						
							| 
									
										
										
										
											2017-03-25 20:39:37 -07:00
										 |  |  |         .add("-Dconfig=" + configFile)
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |         .add("-DCONTEXT=" + etlJobName)
 | 
					
						
							| 
									
										
										
										
											2017-03-25 20:39:37 -07:00
										 |  |  |         .add("-Dlogback.configurationFile=etl_logback.xml")
 | 
					
						
							| 
									
										
										
										
											2017-04-27 16:43:18 -07:00
										 |  |  |         .add("-DLOG_DIR=" + outDir)
 | 
					
						
							| 
									
										
										
										
											2017-08-16 21:24:38 -07:00
										 |  |  |         .add(Launcher.class.getCanonicalName())
 | 
					
						
							| 
									
										
										
										
											2017-04-27 16:43:18 -07:00
										 |  |  |         .build());
 | 
					
						
							| 
									
										
										
										
											2017-11-28 14:30:18 -08:00
										 |  |  |     pb.redirectOutput(ProcessBuilder.Redirect.to(new File(outDir + "/stdout")));
 | 
					
						
							|  |  |  |     pb.redirectError(ProcessBuilder.Redirect.to(new File(outDir + "/stderr")));
 | 
					
						
							| 
									
										
										
										
											2017-04-27 16:43:18 -07:00
										 |  |  |     return pb;
 | 
					
						
							| 
									
										
										
										
											2016-05-03 15:17:38 -07:00
										 |  |  |   }
 | 
					
						
							|  |  |  | }
 |