| 
									
										
										
										
											2015-11-19 14:39:21 -08: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 models.daos;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-12 16:09:08 -07:00
										 |  |  | import java.util.Date;
 | 
					
						
							|  |  |  | import java.util.HashMap;
 | 
					
						
							|  |  |  | import java.util.List;
 | 
					
						
							|  |  |  | import java.util.Map;
 | 
					
						
							| 
									
										
										
										
											2017-08-16 21:24:38 -07:00
										 |  |  | import wherehows.common.jobs.JobStatus;
 | 
					
						
							| 
									
										
										
										
											2016-09-12 16:09:08 -07:00
										 |  |  | import org.springframework.dao.DataAccessException;
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | import org.springframework.jdbc.support.KeyHolder;
 | 
					
						
							|  |  |  | import play.libs.Time;
 | 
					
						
							|  |  |  | import utils.JdbcUtil;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Created by zechen on 9/25/15.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | public class EtlJobDao {
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public static final String GET_ALL_SCHEDULED_JOBS =
 | 
					
						
							|  |  |  |       "SELECT wh_etl_job_name, enabled, next_run FROM wh_etl_job_schedule";
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public static final String UPDATE_NEXT_RUN =
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |       "INSERT INTO wh_etl_job_schedule (wh_etl_job_name, enabled, next_run) VALUES (:whEtlJobName, :enabled, :nextRun) "
 | 
					
						
							|  |  |  |           + "ON DUPLICATE KEY UPDATE next_run = :nextRun";
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |   public static final String INSERT_NEW_RUN = "INSERT INTO wh_etl_job_history(wh_etl_job_name, status, request_time) "
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |       + "VALUES (:whEtlJobName, :status, :requestTime)";
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public static final String START_RUN =
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |       "UPDATE wh_etl_job_history set status = :status, message = :message, start_time = :startTime where wh_etl_exec_id = :whEtlExecId";
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public static final String END_RUN =
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |       "UPDATE wh_etl_job_history set status = :status, message = :message, end_time = :endTime where wh_etl_exec_id = :whEtlExecId";
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-12 16:09:08 -07:00
										 |  |  |   public static final String UPDATE_JOB_PROCESS_ID_AND_HOSTNAME =
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |       "UPDATE wh_etl_job_history SET process_id=?, host_name=? WHERE wh_etl_exec_id =?";
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Get all scheduled jobs from DB
 | 
					
						
							|  |  |  |    */
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |   public static List<Map<String, Object>> getAllScheduledJobs() throws Exception {
 | 
					
						
							|  |  |  |     return JdbcUtil.wherehowsJdbcTemplate.queryForList(GET_ALL_SCHEDULED_JOBS);
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Update the next run time for the etl job using Quartz cron expression
 | 
					
						
							|  |  |  |    */
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |   public static void updateNextRun(String etlJobName, String cronExprStr, Date startTime) throws Exception {
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |     Time.CronExpression cronExpression = new Time.CronExpression(cronExprStr);
 | 
					
						
							|  |  |  |     Date nextTime = cronExpression.getNextValidTimeAfter(startTime);
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |     updateNextRun(etlJobName, nextTime);
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |   public static void updateNextRun(String etlJobName, Date nextTime) throws Exception {
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |     Map<String, Object> params = new HashMap<>();
 | 
					
						
							|  |  |  |     params.put("nextRun", String.valueOf(nextTime.getTime() / 1000));
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |     params.put("enabled", true);
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |     params.put("whEtlJobName", etlJobName);
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |     JdbcUtil.wherehowsNamedJdbcTemplate.update(UPDATE_NEXT_RUN, params);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |   public static long insertNewRun(String etlJobName) {
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |     Map<String, Object> params = new HashMap<>();
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:40:05 -07:00
										 |  |  |     params.put("whEtlJobName", etlJobName);
 | 
					
						
							| 
									
										
										
										
											2017-08-16 21:24:38 -07:00
										 |  |  |     params.put("status", JobStatus.REQUESTED.toString());
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |     params.put("requestTime", System.currentTimeMillis() / 1000);
 | 
					
						
							|  |  |  |     KeyHolder keyHolder = JdbcUtil.insertRow(INSERT_NEW_RUN, params);
 | 
					
						
							|  |  |  |     return (Long) keyHolder.getKey();
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public static void startRun(long whEtlExecId, String message) {
 | 
					
						
							|  |  |  |     Map<String, Object> params = new HashMap<>();
 | 
					
						
							|  |  |  |     params.put("whEtlExecId", whEtlExecId);
 | 
					
						
							| 
									
										
										
										
											2017-08-16 21:24:38 -07:00
										 |  |  |     params.put("status", JobStatus.STARTED.toString());
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |     params.put("startTime", System.currentTimeMillis() / 1000);
 | 
					
						
							|  |  |  |     params.put("message", message);
 | 
					
						
							|  |  |  |     JdbcUtil.wherehowsNamedJdbcTemplate.update(START_RUN, params);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 21:24:38 -07:00
										 |  |  |   public static void endRun(long whEtlExecId, JobStatus status, String message) {
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |     Map<String, Object> params = new HashMap<>();
 | 
					
						
							|  |  |  |     params.put("whEtlExecId", whEtlExecId);
 | 
					
						
							|  |  |  |     params.put("status", status.toString());
 | 
					
						
							|  |  |  |     params.put("endTime", System.currentTimeMillis() / 1000);
 | 
					
						
							|  |  |  |     params.put("message", message);
 | 
					
						
							|  |  |  |     JdbcUtil.wherehowsNamedJdbcTemplate.update(END_RUN, params);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2016-09-12 16:09:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 21:13:56 -07:00
										 |  |  |   public static void updateJobProcessInfo(long whEtlExecId, int processId, String hostname) throws DataAccessException {
 | 
					
						
							| 
									
										
										
										
											2016-09-12 16:09:08 -07:00
										 |  |  |     JdbcUtil.wherehowsJdbcTemplate.update(UPDATE_JOB_PROCESS_ID_AND_HOSTNAME, processId, hostname, whEtlExecId);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | }
 |