Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the german logger package and implemented basic functionality to get history #24

Merged
merged 5 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ A UI extension of [db-scheduler](https://github.com/kagkarlsson/db-scheduler)

* An existing Spring Boot application, with [db-scheduler](https://github.com/kagkarlsson/db-scheduler)
* Minimum Java 11 and SpringBoot 2.7
* db-scheduler-log version 0.7.0
* Minimum db-scheduler version 12.5

## Getting started
Expand All @@ -41,6 +42,13 @@ A UI extension of [db-scheduler](https://github.com/kagkarlsson/db-scheduler)
<version>0.0.1</version>
</dependency>
```
```xml
<dependency>
<groupId>io.rocketbase.extension</groupId>
<artifactId>db-scheduler-log-spring-boot-starter</artifactId>
<version>0.7.0</version>
</dependency>
```


## How it works
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;

@AutoConfiguration
public class UiApiAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(UiApiAutoConfiguration.class);
Expand All @@ -19,8 +21,8 @@ public UiApiAutoConfiguration(){

@Bean
@ConditionalOnMissingBean
public TaskLogic taskLogic(Scheduler scheduler){
return new TaskLogic(scheduler);
public TaskLogic taskLogic(Scheduler scheduler, DataSource dataSource){
return new TaskLogic(scheduler, dataSource);
}

@Bean
Expand Down
4 changes: 4 additions & 0 deletions db-scheduler-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.github.bekk.dbscheduleruiapi.controller;

import com.github.bekk.dbscheduleruiapi.model.GetTasksResponse;
import com.github.bekk.dbscheduleruiapi.model.LogModel;
import com.github.bekk.dbscheduleruiapi.service.TaskLogic;
import com.github.bekk.dbscheduleruiapi.model.TaskRequestParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
Expand All @@ -31,4 +34,9 @@ public void runNow(@RequestParam String id, @RequestParam String name) {
public void deleteTaskNow(@RequestParam String id, @RequestParam String name) {
taskLogic.deleteTask(id, name);
}

@GetMapping("/logs")
public List<LogModel> getLogs(){
return taskLogic.getLogs();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.github.bekk.dbscheduleruiapi.model;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.math.BigInteger;
import java.time.Instant;

public class LogModel {
private Long id;
private String taskName;
private String taskInstance;
private byte[] taskData;
private Instant timeStarted;
private Instant timeFinished;

private boolean succeeded;
private Long durationMs;
private String exceptionClass;
private String exceptionMessage;
private String exceptionStackTrace;


public LogModel(
Long id, String taskName, String taskInstance, byte[] taskData,
Instant timeStarted, Instant timeFinished, boolean succeeded,
Long durationMs, String exceptionClass, String exceptionMessage, String exceptionStackTrace
) {
this.id = id;
this.taskName = taskName;
this.taskInstance = taskInstance;
this.taskData = taskData;
this.timeStarted = timeStarted;
this.timeFinished = timeFinished;
this.succeeded = succeeded;
this.durationMs = durationMs;
this.exceptionClass = exceptionClass;
this.exceptionMessage = exceptionMessage;
this.exceptionStackTrace = exceptionStackTrace;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTaskName() {
return taskName;
}

public void setTaskName(String taskName) {
this.taskName = taskName;
}

public String getTaskInstance() {
return taskInstance;
}

public void setTaskInstance(String taskInstance) {
this.taskInstance = taskInstance;
}

public byte[] getTaskData() {
return taskData;
}

public void setTaskData(byte[] taskData) {
this.taskData = taskData;
}

public Instant getTimeStarted() {
return timeStarted;
}

public void setTimeStarted(Instant timeStarted) {
this.timeStarted = timeStarted;
}

public Instant getTimeFinished() {
return timeFinished;
}

public void setTimeFinished(Instant timeFinished) {
this.timeFinished = timeFinished;
}

public boolean isSucceeded() {
return succeeded;
}

public void setSucceeded(boolean succeeded) {
this.succeeded = succeeded;
}

public Long getDurationMs() {
return durationMs;
}

public void setDurationMs(Long durationMs) {
this.durationMs = durationMs;
}

public String getExceptionClass() {
return exceptionClass;
}

public void setExceptionClass(String exceptionClass) {
this.exceptionClass = exceptionClass;
}

public String getExceptionMessage() {
return exceptionMessage;
}

public void setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
}

public String getExceptionStackTrace() {
return exceptionStackTrace;
}

public void setExceptionStackTrace(String exceptionStackTrace) {
this.exceptionStackTrace = exceptionStackTrace;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.github.bekk.dbscheduleruiapi.service;

import com.github.bekk.dbscheduleruiapi.model.GetTasksResponse;
import com.github.bekk.dbscheduleruiapi.model.LogModel;
import com.github.bekk.dbscheduleruiapi.model.TaskModel;
import com.github.bekk.dbscheduleruiapi.model.TaskRequestParams;
import com.github.bekk.dbscheduleruiapi.util.mapper.LogModelRowMapper;
import com.github.bekk.dbscheduleruiapi.util.mapper.TaskMapper;
import com.github.kagkarlsson.scheduler.ScheduledExecution;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.task.TaskInstanceId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import javax.sql.DataSource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -23,10 +27,16 @@ public class TaskLogic {

private final Scheduler scheduler;

private final DataSource dataSource;

private final JdbcTemplate jdbcTemplate;

@Autowired
public TaskLogic(Scheduler scheduler) {
public TaskLogic(Scheduler scheduler, DataSource dataSource){
this.scheduler = scheduler;
this.scheduler.start();
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public void runTaskNow(String taskId, String taskName) {
Expand Down Expand Up @@ -90,4 +100,8 @@ public GetTasksResponse getAllTasks(TaskRequestParams params) {
return new GetTasksResponse(totalTasks, numberOfPages, pagedTasks);
}

public List<LogModel> getLogs() {
return jdbcTemplate.query("SELECT * FROM scheduled_execution_logs", new LogModelRowMapper());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.bekk.dbscheduleruiapi.util.mapper;

import com.github.bekk.dbscheduleruiapi.model.LogModel;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import org.springframework.jdbc.core.RowMapper;

public class LogModelRowMapper implements RowMapper<LogModel> {

@Override
public LogModel mapRow(ResultSet rs, int rowNum) throws SQLException{
long id = rs.getLong("id");
String taskName = rs.getString("task_name");
String taskInstance = rs.getString("task_instance");
byte[] taskData = rs.getBytes("task_data");
Instant timeStarted = rs.getTimestamp("time_started").toInstant();
Instant timeFinished = rs.getTimestamp("time_finished").toInstant();
boolean succeeded = rs.getBoolean("succeeded");
Long durationMs = rs.getLong("duration_ms");
String exceptionClass = rs.getString("exception_class");
String exceptionMessage = rs.getString("exception_message");
String exceptionStackTrace = rs.getString("exception_stacktrace");
return new LogModel(
rs.getLong("id"),
rs.getString("task_name"),
rs.getString("task_instance"),
rs.getBytes("task_data"),
rs.getTimestamp("time_started").toInstant(),
rs.getTimestamp("time_finished").toInstant(),
rs.getBoolean("succeeded"),
rs.getLong("duration_ms"),
rs.getString("exception_class"),
rs.getString("exception_message"),
rs.getString("exception_stacktrace")
);
}
}
6 changes: 5 additions & 1 deletion example-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>io.rocketbase.extension</groupId>
<artifactId>db-scheduler-log-spring-boot-starter</artifactId>
<version>0.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.github.bekk.exampleapp.tasks.TaskDefinitions;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.serializer.JavaSerializer;
import io.rocketbase.extension.jdbc.JdbcLogRepository;
import io.rocketbase.extension.jdbc.Snowflake;
import io.rocketbase.extension.stats.LogStatsPlainRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -17,12 +21,16 @@ public class SchedulerConfig {
public SchedulerConfig(DataSource dataSource) {
this.dataSource = dataSource;
}

@Bean
public JdbcLogRepository jdbcLogRepository(){
return new JdbcLogRepository(dataSource, new JavaSerializer(),JdbcLogRepository.DEFAULT_TABLE_NAME , new Snowflake());
}
@Bean
public Scheduler scheduler() {
return Scheduler.create(dataSource, TaskDefinitions.getAllKnownTaskDefinitions())
.startTasks()
.pollingInterval(Duration.ofSeconds(5))
.statsRegistry(new LogStatsPlainRegistry(jdbcLogRepository()))
.registerShutdownHook()
.build();
}
Expand Down
2 changes: 2 additions & 0 deletions example-app/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
server.port=${PORT:8081}
spring.datasource.url=jdbc:h2:file:./db-scheduler-application/test-db;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH
spring.datasource.driver-class-name=org.h2.Driver
db-scheduler-log.enabled=true
db-scheduler-log.table-name=scheduled_execution_logs
19 changes: 19 additions & 0 deletions example-app/src/main/resources/db/migration/V2__add_logging.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
create table scheduled_execution_logs
(
id BIGINT not null primary key,
task_name text not null,
task_instance text not null,
task_data bytea,
picked_by text,
time_started timestamp with time zone not null,
time_finished timestamp with time zone not null,
succeeded BOOLEAN not null,
duration_ms BIGINT not null,
exception_class text,
exception_message text,
exception_stacktrace text
);

CREATE INDEX stl_started_idx ON scheduled_execution_logs (time_started);
CREATE INDEX stl_task_name_idx ON scheduled_execution_logs (task_name);
CREATE INDEX stl_exception_class_idx ON scheduled_execution_logs (exception_class);