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;
}
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();
}
Aggregations