Skip to content

Commit

Permalink
fixup! Create JobKitWatchdog, clean SpoolerTest and BackgroundService…
Browse files Browse the repository at this point in the history
…EventTest #140
  • Loading branch information
hdsdi3g committed Jul 3, 2023
1 parent c179a07 commit 4953b3c
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand All @@ -26,6 +27,7 @@ public class JobKitEngine implements JobTrait {
private final SupervisableManager supervisableManager;
private final AtomicBoolean shutdown;
private final Set<String> spoolsNamesToKeepRunningToTheEnd;
@Getter
private final JobKitWatchdog jobKitWatchdog;

public JobKitEngine(final ScheduledExecutorService scheduledExecutor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ SupervisableServiceSupplier getSupervisableSupplier(final SupervisableManager su
JobKitEngine getJobKitEngine(final ScheduledExecutorService scheduledExecutor,
final ExecutionEvent executionEvent,
final BackgroundServiceEvent backgroundServiceEvent,
final SupervisableManager supervisableManager) {
// TODO inject YML -> JobWatchdogPolicy
return new JobKitEngine(scheduledExecutor, executionEvent, backgroundServiceEvent, supervisableManager);
final SupervisableManager supervisableManager,
final JobKitWatchdogConfig watchdogConfig) {
final var jobKit = new JobKitEngine(scheduledExecutor, executionEvent, backgroundServiceEvent,
supervisableManager);
final var watchdog = jobKit.getJobKitWatchdog();
watchdogConfig.getMaxSpoolQueueSize().forEach(watchdog::addPolicies);
watchdogConfig.getLimitedExecTime().forEach(watchdog::addPolicies);
watchdogConfig.getLimitedServiceExecTime().forEach(watchdog::addPolicies);
return jobKit;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of jobkit.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* Copyright (C) hdsdi3g for hd3g.tv 2023
*
*/
package tv.hd3g.jobkit.mod;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import jakarta.annotation.PostConstruct;
import lombok.Data;
import tv.hd3g.jobkit.engine.watchdog.LimitedExecTimePolicy;
import tv.hd3g.jobkit.engine.watchdog.LimitedServiceExecTimePolicy;
import tv.hd3g.jobkit.engine.watchdog.MaxSpoolQueueSizePolicy;

@Configuration
@ConfigurationProperties(prefix = "jobkit.watchdogpolicies")
@Data
public class JobKitWatchdogConfig {

private List<MaxSpoolQueueSizePolicy> maxSpoolQueueSize;
private List<LimitedExecTimePolicy> limitedExecTime;
private List<LimitedServiceExecTimePolicy> limitedServiceExecTime;

@PostConstruct
void init() {
maxSpoolQueueSize = Optional.ofNullable(maxSpoolQueueSize).orElse(new ArrayList<>());
limitedExecTime = Optional.ofNullable(limitedExecTime).orElse(new ArrayList<>());
limitedServiceExecTime = Optional.ofNullable(limitedServiceExecTime).orElse(new ArrayList<>());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,35 @@
package tv.hd3g.jobkit.mod;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.MockitoAnnotations.openMocks;

import java.time.Duration;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;

import tv.hd3g.jobkit.engine.JobKitEngine;
import tv.hd3g.jobkit.engine.watchdog.LimitedExecTimePolicy;
import tv.hd3g.jobkit.engine.watchdog.LimitedServiceExecTimePolicy;
import tv.hd3g.jobkit.engine.watchdog.MaxSpoolQueueSizePolicy;

@SpringBootTest
class JobKitSetupTest {

@Autowired
ScheduledExecutorService scheduledExecutorService;
@Autowired
JobKitEngine jobKitEngine;

@Test
void testGetScheduledExecutor() throws InterruptedException {
Expand All @@ -39,4 +54,59 @@ void testGetScheduledExecutor() throws InterruptedException {
latch.await(100, TimeUnit.MILLISECONDS);
}

@Test
void testGetJobKitEngineWatchdog() {
assertTrue(jobKitEngine.getJobKitWatchdog().getPolicies().isEmpty());
}

@SpringBootTest
@TestPropertySource(locations = "classpath:application-watchdogpolicies.yml")
static class Watchdog {

@Autowired
JobKitEngine jobKitEngine;
@Autowired
JobKitWatchdogConfig watchdogConfig;

@BeforeEach
void init() throws Exception {
openMocks(this).close();
}

@AfterEach
void end() {
}

@Test
void testWatchdogPolicies() {
final var p = jobKitEngine.getJobKitWatchdog().getPolicies();
assertEquals(3, p.size());

final var maxSpoolQueueSize = p.stream()
.filter(f -> f instanceof MaxSpoolQueueSizePolicy)
.map(f -> (MaxSpoolQueueSizePolicy) f)
.findFirst()
.get();
assertEquals(10, maxSpoolQueueSize.getMaxSize());
assertEquals(Set.of("AA"), maxSpoolQueueSize.getOnlySpools());

final var limitedExecTime = p.stream()
.filter(f -> f instanceof LimitedExecTimePolicy)
.map(f -> (LimitedExecTimePolicy) f)
.findFirst()
.get();
assertEquals(Duration.ofMillis(10000), limitedExecTime.getMaxExecTime());
assertEquals(Set.of("BB"), limitedExecTime.getOnlySpools());

final var limitedServiceExecTimePolicy = p.stream()
.filter(f -> f instanceof LimitedServiceExecTimePolicy)
.map(f -> (LimitedServiceExecTimePolicy) f)
.findFirst()
.get();
assertEquals(5, limitedServiceExecTimePolicy.getWaitFactor());
assertEquals(Set.of("CC"), limitedServiceExecTimePolicy.getOnlySpools());
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
spring:
main:
banner-mode: "off"
log-startup-info: false
web-application-type: none

jobkit:
watchdogpolicies:
maxSpoolQueueSize:
- maxSize: 10
onlySpools: ["AA"]
limitedExecTime:
- maxExecTime: 10s
onlySpools: ["BB"]
limitedServiceExecTime:
- waitFactor: 5
onlySpools: ["CC"]
55 changes: 1 addition & 54 deletions jobkit/springboot-service/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,4 @@ spring:
main:
banner-mode: "off"
log-startup-info: false
server:
port: 0

jobkit:
processrunners:
disabled-at-start: true
reply-to: reply@jobkkit.local
send-from: from@jobkkit.local
send-to-admin: [admin1@jobkkit.local, admin2@jobkkit.local]
sender-reference: send-ref-email
default-template-name-done: "def-tpl-ok"
default-template-name-error: "def-tpl-err"
services:
#0
- name: java-version
spool-name: test-spool
command-line: java0 -version
comment: Just run java version
env:
env1: value1
period-time: 5s
priority: 2
run-first-at-boot: true
working-dir: "."
retry-after-time-factor: 3
after-done:
add-to-template-vars:
varA: valueA
lang: fr_FR
reply-to: reply-java-version@jobkkit.local
send-to: java-version@jobkkit.local
send-cc: java-version-cc@jobkkit.local
template-name: tpl-java-version-ok
after-error:
add-to-template-vars:
varB: valueB
lang: en_US
reply-to: never-working-rply@jobkkit.local
send-to: to-never-working@jobkkit.local
send-cc: never-working-cc@jobkkit.local
template-name: tpl-never-working-err
#1
- name: java-lazy-version
spool-name: test-spool2
command-line: java-wo-cmdline
comment: Just run lazy java version
period-time: 1m
#2
- name: never-configured-workingdir
spool-name: test-spool
command-line: java2 -version
comment: Never run
period-time: 5s
working-dir: /this/never/will/exists
web-application-type: none

0 comments on commit 4953b3c

Please sign in to comment.