111 lines
4.3 KiB
Java
Raw Normal View History

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;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import metadata.etl.models.EtlJobName;
import metadata.etl.models.EtlJobStatus;
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;
import utils.JsonUtil;
import java.sql.SQLException;
/**
* Created by zechen on 9/25/15.
*/
public class EtlJobDao {
public static final String GET_ALL_SCHEDULED_JOBS = "SELECT * FROM wh_etl_job_schedule";
2015-11-19 14:39:21 -08:00
public static final String UPDATE_NEXT_RUN =
"REPLACE INTO wh_etl_job_schedule(wh_etl_job_name, next_run) VALUES (:whEtlJobName, :nextRun)";
2015-11-19 14:39:21 -08:00
public static final String INSERT_NEW_RUN = "INSERT INTO wh_etl_job_history(wh_etl_job_name, status, request_time) "
+ "VALUES (:whEtlJobName, :status, :requestTime)";
2015-11-19 14:39:21 -08:00
public static final String START_RUN =
"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 =
"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
public static final String UPDATE_JOB_PROCESS_ID_AND_HOSTNAME =
"UPDATE wh_etl_job_history SET process_id=?, host_name=? WHERE wh_etl_exec_id =?";
2015-11-19 14:39:21 -08: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
*/
public static void updateNextRun(String etlJobName, String cronExprStr, Date startTime)
2015-11-19 14:39:21 -08:00
throws Exception {
Time.CronExpression cronExpression = new Time.CronExpression(cronExprStr);
Date nextTime = cronExpression.getNextValidTimeAfter(startTime);
updateNextRun(etlJobName, nextTime);
2015-11-19 14:39:21 -08:00
}
public static void updateNextRun(String etlJobName, Date nextTime)
2015-11-19 14:39:21 -08:00
throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("nextRun", String.valueOf(nextTime.getTime() / 1000));
params.put("whEtlJobName", etlJobName);
2015-11-19 14:39:21 -08:00
JdbcUtil.wherehowsNamedJdbcTemplate.update(UPDATE_NEXT_RUN, params);
}
public static long insertNewRun(String etlJobName) {
2015-11-19 14:39:21 -08:00
Map<String, Object> params = new HashMap<>();
params.put("whEtlJobName", etlJobName);
2015-11-19 14:39:21 -08:00
params.put("status", EtlJobStatus.REQUESTED.toString());
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);
params.put("status", EtlJobStatus.STARTED.toString());
params.put("startTime", System.currentTimeMillis() / 1000);
params.put("message", message);
JdbcUtil.wherehowsNamedJdbcTemplate.update(START_RUN, params);
}
public static void endRun(long whEtlExecId, EtlJobStatus status, String message) {
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);
}
public static void updateJobProcessInfo(long whEtlExecId, int processId, String hostname)
throws DataAccessException {
JdbcUtil.wherehowsJdbcTemplate.update(UPDATE_JOB_PROCESS_ID_AND_HOSTNAME, processId, hostname, whEtlExecId);
}
2015-11-19 14:39:21 -08:00
}