Search in sources :

Example 6 with TaskRunningStatistics

use of com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics in project elastic-job by dangdangdotcom.

the class StatisticRdbRepository method findTaskRunningStatistics.

/**
     * 获取运行中的任务统计数据集合.
     * 
     * @param from 统计开始时间
     * @return 运行中的任务统计数据集合
     */
public List<TaskRunningStatistics> findTaskRunningStatistics(final Date from) {
    List<TaskRunningStatistics> result = new LinkedList<>();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String sql = String.format("SELECT id, running_count, statistics_time, creation_time FROM %s WHERE statistics_time >= '%s' order by id ASC", TABLE_TASK_RUNNING_STATISTICS, formatter.format(from));
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery()) {
        while (resultSet.next()) {
            TaskRunningStatistics taskRunningStatistics = new TaskRunningStatistics(resultSet.getLong(1), resultSet.getInt(2), new Date(resultSet.getTimestamp(3).getTime()), new Date(resultSet.getTimestamp(4).getTime()));
            result.add(taskRunningStatistics);
        }
    } catch (final SQLException ex) {
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error("Fetch taskRunningStatistics from DB error:", ex);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SimpleDateFormat(java.text.SimpleDateFormat) LinkedList(java.util.LinkedList) TaskRunningStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics) Date(java.util.Date)

Example 7 with TaskRunningStatistics

use of com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics in project elastic-job by dangdangdotcom.

the class JobRunningStatisticJobTest method assertExecute.

@Test
public void assertExecute() throws SchedulerException {
    Optional<JobRunningStatistics> latestJobRunningStatistics = Optional.of(new JobRunningStatistics(0, StatisticTimeUtils.getStatisticTime(StatisticInterval.MINUTE, -3)));
    Optional<TaskRunningStatistics> latestTaskRunningStatistics = Optional.of(new TaskRunningStatistics(0, StatisticTimeUtils.getStatisticTime(StatisticInterval.MINUTE, -3)));
    when(repository.findLatestJobRunningStatistics()).thenReturn(latestJobRunningStatistics);
    when(repository.findLatestTaskRunningStatistics()).thenReturn(latestTaskRunningStatistics);
    when(repository.add(any(JobRunningStatistics.class))).thenReturn(true);
    when(repository.add(any(TaskRunningStatistics.class))).thenReturn(true);
    Map<String, Set<TaskContext>> jobMap = new HashMap<>(1);
    Set<TaskContext> jobSet = new HashSet<>(1);
    jobSet.add(TaskContext.from(TaskNode.builder().jobName("test_job").build().getTaskNodeValue()));
    jobMap.put("test_job", jobSet);
    when(runningService.getAllRunningTasks()).thenReturn(jobMap);
    jobRunningStatisticJob.execute(null);
    verify(repository).findLatestJobRunningStatistics();
    verify(repository).findLatestTaskRunningStatistics();
    verify(repository, times(3)).add(any(JobRunningStatistics.class));
    verify(repository, times(3)).add(any(TaskRunningStatistics.class));
    verify(runningService).getAllRunningTasks();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TaskContext(com.dangdang.ddframe.job.context.TaskContext) HashMap(java.util.HashMap) JobRunningStatistics(com.dangdang.ddframe.job.statistics.type.job.JobRunningStatistics) TaskRunningStatistics(com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TaskRunningStatistics (com.dangdang.ddframe.job.statistics.type.task.TaskRunningStatistics)7 Date (java.util.Date)5 Test (org.junit.Test)4 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 TaskContext (com.dangdang.ddframe.job.context.TaskContext)1 JobRunningStatistics (com.dangdang.ddframe.job.statistics.type.job.JobRunningStatistics)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1