mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-24 05:18:58 +00:00
(service) Task heartbeat creates event log entries
This commit is contained in:
parent
ce293029c7
commit
807fb2d052
@ -20,6 +20,7 @@ public class ServiceHeartbeat {
|
|||||||
private final String serviceBase;
|
private final String serviceBase;
|
||||||
private final String instanceUUID;
|
private final String instanceUUID;
|
||||||
private final ServiceConfiguration configuration;
|
private final ServiceConfiguration configuration;
|
||||||
|
private final ServiceEventLog eventLog;
|
||||||
private final HikariDataSource dataSource;
|
private final HikariDataSource dataSource;
|
||||||
|
|
||||||
|
|
||||||
@ -30,11 +31,13 @@ public class ServiceHeartbeat {
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ServiceHeartbeat(ServiceConfiguration configuration,
|
public ServiceHeartbeat(ServiceConfiguration configuration,
|
||||||
|
ServiceEventLog eventLog,
|
||||||
HikariDataSource dataSource)
|
HikariDataSource dataSource)
|
||||||
{
|
{
|
||||||
this.serviceName = configuration.serviceName() + ":" + configuration.node();
|
this.serviceName = configuration.serviceName() + ":" + configuration.node();
|
||||||
this.serviceBase = configuration.serviceName();
|
this.serviceBase = configuration.serviceName();
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
|
this.eventLog = eventLog;
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
|
|
||||||
this.instanceUUID = configuration.instanceUuid().toString();
|
this.instanceUUID = configuration.instanceUuid().toString();
|
||||||
@ -45,7 +48,7 @@ public class ServiceHeartbeat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Enum<T>> ServiceTaskHeartbeat<T> createServiceTaskHeartbeat(Class<T> steps, String processName) {
|
public <T extends Enum<T>> ServiceTaskHeartbeat<T> createServiceTaskHeartbeat(Class<T> steps, String processName) {
|
||||||
return new ServiceTaskHeartbeat<>(steps, configuration, processName, dataSource);
|
return new ServiceTaskHeartbeat<>(steps, configuration, processName, eventLog, dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@ public class ServiceTaskHeartbeat<T extends Enum<T>> implements AutoCloseable {
|
|||||||
private final int heartbeatInterval = Integer.getInteger("mcp.heartbeat.interval", 1);
|
private final int heartbeatInterval = Integer.getInteger("mcp.heartbeat.interval", 1);
|
||||||
private final String serviceInstanceUUID;
|
private final String serviceInstanceUUID;
|
||||||
private final int stepCount;
|
private final int stepCount;
|
||||||
|
private final ServiceEventLog eventLog;
|
||||||
|
|
||||||
private volatile boolean running = false;
|
private volatile boolean running = false;
|
||||||
private volatile int stepNum = 0;
|
private volatile int stepNum = 0;
|
||||||
@ -35,8 +36,10 @@ public class ServiceTaskHeartbeat<T extends Enum<T>> implements AutoCloseable {
|
|||||||
ServiceTaskHeartbeat(Class<T> stepClass,
|
ServiceTaskHeartbeat(Class<T> stepClass,
|
||||||
ServiceConfiguration configuration,
|
ServiceConfiguration configuration,
|
||||||
String taskName,
|
String taskName,
|
||||||
|
ServiceEventLog eventLog,
|
||||||
HikariDataSource dataSource)
|
HikariDataSource dataSource)
|
||||||
{
|
{
|
||||||
|
this.eventLog = eventLog;
|
||||||
this.taskName = configuration.serviceName() + "." + taskName + ":" + configuration.node();
|
this.taskName = configuration.serviceName() + "." + taskName + ":" + configuration.node();
|
||||||
this.taskBase = configuration.serviceName() + "." + taskName;
|
this.taskBase = configuration.serviceName() + "." + taskName;
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
@ -46,6 +49,8 @@ public class ServiceTaskHeartbeat<T extends Enum<T>> implements AutoCloseable {
|
|||||||
|
|
||||||
this.stepCount = stepClass.getEnumConstants().length;
|
this.stepCount = stepClass.getEnumConstants().length;
|
||||||
|
|
||||||
|
heartbeatInit();
|
||||||
|
|
||||||
runnerThread = new Thread(this::run);
|
runnerThread = new Thread(this::run);
|
||||||
runnerThread.start();
|
runnerThread.start();
|
||||||
}
|
}
|
||||||
@ -58,6 +63,7 @@ public class ServiceTaskHeartbeat<T extends Enum<T>> implements AutoCloseable {
|
|||||||
public void progress(T step) {
|
public void progress(T step) {
|
||||||
this.step = step.name();
|
this.step = step.name();
|
||||||
|
|
||||||
|
|
||||||
// off by one since we calculate the progress based on the number of steps,
|
// off by one since we calculate the progress based on the number of steps,
|
||||||
// and Enum.ordinal() is zero-based (so the 5th step in a 5 step task is 4, not 5; resulting in the
|
// and Enum.ordinal() is zero-based (so the 5th step in a 5 step task is 4, not 5; resulting in the
|
||||||
// final progress being 80% and not 100%)
|
// final progress being 80% and not 100%)
|
||||||
@ -65,6 +71,7 @@ public class ServiceTaskHeartbeat<T extends Enum<T>> implements AutoCloseable {
|
|||||||
this.stepNum = 1 + step.ordinal();
|
this.stepNum = 1 + step.ordinal();
|
||||||
|
|
||||||
logger.info("ServiceTask {} progress: {}", taskBase, step.name());
|
logger.info("ServiceTask {} progress: {}", taskBase, step.name());
|
||||||
|
eventLog.logEvent("TASK-STEP", taskName + " = " + step.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void shutDown() {
|
public void shutDown() {
|
||||||
@ -89,8 +96,6 @@ public class ServiceTaskHeartbeat<T extends Enum<T>> implements AutoCloseable {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
heartbeatInit();
|
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
heartbeatUpdate();
|
heartbeatUpdate();
|
||||||
@ -102,13 +107,13 @@ public class ServiceTaskHeartbeat<T extends Enum<T>> implements AutoCloseable {
|
|||||||
TimeUnit.SECONDS.sleep(heartbeatInterval);
|
TimeUnit.SECONDS.sleep(heartbeatInterval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (InterruptedException|SQLException ex) {
|
catch (InterruptedException ex) {
|
||||||
logger.error("ServiceHeartbeat caught irrecoverable exception, killing service", ex);
|
logger.error("ServiceHeartbeat caught irrecoverable exception, killing service", ex);
|
||||||
System.exit(255);
|
System.exit(255);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void heartbeatInit() throws SQLException {
|
private void heartbeatInit() {
|
||||||
try (var connection = dataSource.getConnection()) {
|
try (var connection = dataSource.getConnection()) {
|
||||||
try (var stmt = connection.prepareStatement(
|
try (var stmt = connection.prepareStatement(
|
||||||
"""
|
"""
|
||||||
@ -131,6 +136,12 @@ public class ServiceTaskHeartbeat<T extends Enum<T>> implements AutoCloseable {
|
|||||||
stmt.executeUpdate();
|
stmt.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (SQLException ex) {
|
||||||
|
logger.error("ServiceHeartbeat failed to initialize", ex);
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
eventLog.logEvent("TASK-STARTED", taskName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void heartbeatUpdate() throws SQLException {
|
private void heartbeatUpdate() throws SQLException {
|
||||||
@ -173,6 +184,7 @@ public class ServiceTaskHeartbeat<T extends Enum<T>> implements AutoCloseable {
|
|||||||
stmt.executeUpdate();
|
stmt.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
eventLog.logEvent("TASK-TERMINATED", taskName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user