Skip to content

Commit

Permalink
Feature/more cleanup (#938)
Browse files Browse the repository at this point in the history
* More PR feedback resolving and cleanup

* Create BackgroundJobConfigurationReader

* Use serverTimeoutPollIntervalMultiplicand

* Further cleanup

* Fix build

* Fix PR feedback
  • Loading branch information
rdehuyss committed Feb 16, 2024
1 parent 270eab8 commit 44c54dc
Show file tree
Hide file tree
Showing 34 changed files with 281 additions and 283 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jobrunr.jobs.states;

import org.jobrunr.server.BackgroundJobServer;
import org.jobrunr.server.BackgroundJobServerConfiguration;
import org.jobrunr.server.BackgroundJobServerConfigurationReader;

import java.time.Instant;
import java.util.UUID;
Expand All @@ -21,7 +21,7 @@ public ProcessingState(BackgroundJobServer backgroundJobServer) {
this(backgroundJobServer.getConfiguration());
}

public ProcessingState(BackgroundJobServerConfiguration backgroundJobServerConfiguration) {
public ProcessingState(BackgroundJobServerConfigurationReader backgroundJobServerConfiguration) {
this(backgroundJobServerConfiguration.getId(), backgroundJobServerConfiguration.getName());
}

Expand Down
12 changes: 8 additions & 4 deletions core/src/main/java/org/jobrunr/server/BackgroundJobServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class BackgroundJobServer implements BackgroundJobServerMBean {

private static final Logger LOGGER = LoggerFactory.getLogger(BackgroundJobServer.class);

private final BackgroundJobServerConfiguration configuration;
private final BackgroundJobServerConfigurationReader configuration;
private final StorageProvider storageProvider;
private final DashboardNotificationManager dashboardNotificationManager;
private final JsonMapper jsonMapper;
Expand All @@ -75,12 +75,16 @@ public BackgroundJobServer(StorageProvider storageProvider, JsonMapper jsonMappe
}

public BackgroundJobServer(StorageProvider storageProvider, JsonMapper jsonMapper, JobActivator jobActivator, BackgroundJobServerConfiguration configuration) {
this(storageProvider, jsonMapper, jobActivator, new BackgroundJobServerConfigurationReader(configuration));
}

protected BackgroundJobServer(StorageProvider storageProvider, JsonMapper jsonMapper, JobActivator jobActivator, BackgroundJobServerConfigurationReader configuration) {
if (storageProvider == null)
throw new IllegalArgumentException("A StorageProvider is required to use a BackgroundJobServer. Please see the documentation on how to setup a job StorageProvider.");

this.configuration = configuration;
this.storageProvider = new ThreadSafeStorageProvider(storageProvider);
this.dashboardNotificationManager = new DashboardNotificationManager(configuration.getId(), storageProvider);
this.dashboardNotificationManager = new DashboardNotificationManager(this.configuration.getId(), storageProvider);
this.jsonMapper = jsonMapper;
this.backgroundJobRunners = initializeBackgroundJobRunners(jobActivator);
this.jobDefaultFilters = new JobDefaultFilters();
Expand All @@ -90,7 +94,7 @@ public BackgroundJobServer(StorageProvider storageProvider, JsonMapper jsonMappe
this.jobZooKeeper = createJobZooKeeper();
this.backgroundJobPerformerFactory = loadBackgroundJobPerformerFactory();
this.lifecycleLock = new BackgroundJobServerLifecycleLock();
this.storageProvider.validatePollInterval(configuration.getPollInterval());
this.storageProvider.validatePollInterval(this.configuration.getPollInterval());
}

@Override
Expand Down Expand Up @@ -224,7 +228,7 @@ public StorageProvider getStorageProvider() {
return storageProvider;
}

public BackgroundJobServerConfiguration getConfiguration() {
public BackgroundJobServerConfigurationReader getConfiguration() {
return configuration;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
public class BackgroundJobServerConfiguration {

public static final Duration DEFAULT_POLL_INTERVAL = Duration.ofSeconds(15);
public static final int DEFAULT_SERVER_TIMEOUT_POLL_INTERVAL_MULTIPLICAND = 4;
public static final int DEFAULT_PAGE_REQUEST_SIZE = 1000;
public static final Duration DEFAULT_DELETE_SUCCEEDED_JOBS_DURATION = Duration.ofHours(36);
public static final Duration DEFAULT_PERMANENTLY_DELETE_JOBS_DURATION = Duration.ofHours(72);

private int scheduledJobsRequestSize = DEFAULT_PAGE_REQUEST_SIZE;
private int orphanedJobsRequestSize = DEFAULT_PAGE_REQUEST_SIZE;
private int succeededJobsRequestSize = DEFAULT_PAGE_REQUEST_SIZE;
private Duration pollInterval = DEFAULT_POLL_INTERVAL;
private UUID id = UUID.randomUUID();
private String name = getHostName();
private Duration deleteSucceededJobsAfter = DEFAULT_DELETE_SUCCEEDED_JOBS_DURATION;
private Duration permanentlyDeleteDeletedJobsAfter = DEFAULT_PERMANENTLY_DELETE_JOBS_DURATION;
private BackgroundJobServerWorkerPolicy backgroundJobServerWorkerPolicy = new DefaultBackgroundJobServerWorkerPolicy();
private ConcurrentJobModificationPolicy concurrentJobModificationPolicy = new DefaultConcurrentJobModificationPolicy();
int scheduledJobsRequestSize = DEFAULT_PAGE_REQUEST_SIZE;
int orphanedJobsRequestSize = DEFAULT_PAGE_REQUEST_SIZE;
int succeededJobsRequestSize = DEFAULT_PAGE_REQUEST_SIZE;
Duration pollInterval = DEFAULT_POLL_INTERVAL;
int serverTimeoutPollIntervalMultiplicand = DEFAULT_SERVER_TIMEOUT_POLL_INTERVAL_MULTIPLICAND;
UUID id = UUID.randomUUID();
String name = getHostName();
Duration deleteSucceededJobsAfter = DEFAULT_DELETE_SUCCEEDED_JOBS_DURATION;
Duration permanentlyDeleteDeletedJobsAfter = DEFAULT_PERMANENTLY_DELETE_JOBS_DURATION;
BackgroundJobServerWorkerPolicy backgroundJobServerWorkerPolicy = new DefaultBackgroundJobServerWorkerPolicy();
ConcurrentJobModificationPolicy concurrentJobModificationPolicy = new DefaultConcurrentJobModificationPolicy();

private BackgroundJobServerConfiguration() {

Expand Down Expand Up @@ -93,6 +95,20 @@ public BackgroundJobServerConfiguration andPollInterval(Duration pollInterval) {
return this;
}

/**
* Allows to set the pollInterval multiplicand after which a BackgroundJobServer will be seen as timed out (e.g. because it crashed, was stopped, ...).
* <p>
* You can increase this value if you have long stop the world GC cycles or are running on shared hosting and experiencing CPU starvation.
*
* @param multiplicand the pollInterval multiplicand
* @return the same configuration instance which provides a fluent api
*/
public BackgroundJobServerConfiguration andServerTimeoutPollIntervalMultiplicand(int multiplicand) {
if (multiplicand < 4) throw new IllegalArgumentException("The smallest possible ServerTimeoutPollIntervalMultiplicand is 4 (4 is also the default)");
this.serverTimeoutPollIntervalMultiplicand = multiplicand;
return this;
}

/**
* Allows to set the workerCount for the BackgroundJobServer which defines the maximum number of jobs that will be run in parallel
*
Expand Down Expand Up @@ -185,45 +201,7 @@ public BackgroundJobServerConfiguration andConcurrentJobModificationPolicy(Concu
return this;
}

public UUID getId() {
return id;
}

public String getName() {
return name;
}

public int getScheduledJobsRequestSize() {
return scheduledJobsRequestSize;
}

public int getOrphanedJobsRequestSize() {
return orphanedJobsRequestSize;
}

public int getSucceededJobsRequestSize() {
return succeededJobsRequestSize;
}

public Duration getPollInterval() {
return pollInterval;
}

public Duration getDeleteSucceededJobsAfter() {
return deleteSucceededJobsAfter;
}

public Duration getPermanentlyDeleteDeletedJobsAfter() {
return permanentlyDeleteDeletedJobsAfter;
}

public BackgroundJobServerWorkerPolicy getBackgroundJobServerWorkerPolicy() {
return backgroundJobServerWorkerPolicy;
}

public ConcurrentJobModificationPolicy getConcurrentJobModificationPolicy() {
return concurrentJobModificationPolicy;
}

private static String getHostName() {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jobrunr.server;

import org.jobrunr.server.configuration.BackgroundJobServerWorkerPolicy;
import org.jobrunr.server.configuration.ConcurrentJobModificationPolicy;

import java.time.Duration;
import java.util.UUID;

public class BackgroundJobServerConfigurationReader {

private final BackgroundJobServerConfiguration configuration;

public BackgroundJobServerConfigurationReader(BackgroundJobServerConfiguration configuration) {
this.configuration = configuration;
}

public UUID getId() {
return configuration.id;
}

public String getName() {
return configuration.name;
}

public int getScheduledJobsRequestSize() {
return configuration.scheduledJobsRequestSize;
}

public int getOrphanedJobsRequestSize() {
return configuration.orphanedJobsRequestSize;
}

public int getSucceededJobsRequestSize() {
return configuration.succeededJobsRequestSize;
}

public Duration getPollInterval() {
return configuration.pollInterval;
}

public Integer getServerTimeoutPollIntervalMultiplicand() {
return configuration.serverTimeoutPollIntervalMultiplicand;
}

public Duration getDeleteSucceededJobsAfter() {
return configuration.deleteSucceededJobsAfter;
}

public Duration getPermanentlyDeleteDeletedJobsAfter() {
return configuration.permanentlyDeleteDeletedJobsAfter;
}

public BackgroundJobServerWorkerPolicy getBackgroundJobServerWorkerPolicy() {
return configuration.backgroundJobServerWorkerPolicy;
}

public ConcurrentJobModificationPolicy getConcurrentJobModificationPolicy() {
return configuration.concurrentJobModificationPolicy;
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jobrunr/server/JobZooKeeper.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void onboardNewWorkIfPossible(ZooKeeperRunTaskInfo runInfo) {
onboardNewWorkTask.run(runInfo);
}

BackgroundJobServerConfiguration backgroundJobServerConfiguration() {
BackgroundJobServerConfigurationReader backgroundJobServerConfiguration() {
return backgroundJobServer.getConfiguration();
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jobrunr/server/ServerZooKeeper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ServerZooKeeper(BackgroundJobServer backgroundJobServer) {
this.backgroundJobServer = backgroundJobServer;
this.storageProvider = backgroundJobServer.getStorageProvider();
this.dashboardNotificationManager = backgroundJobServer.getDashboardNotificationManager();
this.timeoutDuration = backgroundJobServer.getConfiguration().getPollInterval().multipliedBy(4);
this.timeoutDuration = backgroundJobServer.getConfiguration().getPollInterval().multipliedBy(backgroundJobServer.getConfiguration().getServerTimeoutPollIntervalMultiplicand());
this.restartAttempts = new AtomicInteger();
this.lastSignalAlive = Instant.now();
this.lastServerTimeoutCheck = Instant.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import org.jobrunr.server.BackgroundJobServer;
import org.jobrunr.server.dashboard.DashboardNotificationManager;
import org.jobrunr.server.dashboard.NewJobRunrVersionNotification;
import org.jobrunr.utils.annotations.VisibleFor;
import org.jobrunr.utils.JarUtils;
import org.jobrunr.utils.VersionNumber;
import org.jobrunr.utils.annotations.VisibleFor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,11 +16,10 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.jobrunr.utils.StringUtils.*;
import static org.jobrunr.utils.VersionNumber.v;

public class CheckForNewJobRunrVersion implements Runnable {

Expand All @@ -31,7 +31,6 @@ public class CheckForNewJobRunrVersion implements Runnable {

public CheckForNewJobRunrVersion(BackgroundJobServer backgroundJobServer) {
dashboardNotificationManager = backgroundJobServer.getDashboardNotificationManager();

CheckForNewJobRunrVersion.isFirstRun = true; // why: otherwise latest version API is spammed during testing
}

Expand All @@ -40,19 +39,19 @@ public void run() {
if (isFirstRun) {
final NewJobRunrVersionNotification newJobRunrVersionNotification = dashboardNotificationManager.getDashboardNotification(NewJobRunrVersionNotification.class);
if (newJobRunrVersionNotification != null) {
VersionNumber actualVersion = new VersionNumber(getActualVersion());
VersionNumber latestVersion = new VersionNumber(newJobRunrVersionNotification.getLatestVersion());
VersionNumber actualVersion = v(getActualVersion());
VersionNumber latestVersion = v(newJobRunrVersionNotification.getLatestVersion());
if (actualVersion.equals(latestVersion)) {
dashboardNotificationManager.deleteNotification(NewJobRunrVersionNotification.class);
}
}
} else {
try {
VersionNumber latestVersion = new VersionNumber(getLatestVersion());
VersionNumber actualVersion = new VersionNumber(getActualVersion());
VersionNumber latestVersion = v(getLatestVersion());
VersionNumber actualVersion = v(getActualVersion());
if (latestVersion.compareTo(actualVersion) > 0) {
dashboardNotificationManager.notify(new NewJobRunrVersionNotification(latestVersion.getCompleteVersion()));
LOGGER.info("JobRunr version {} is available.", latestVersion.completeVersion);
LOGGER.info("JobRunr version {} is available.", latestVersion);
} else {
dashboardNotificationManager.deleteNotification(NewJobRunrVersionNotification.class);
}
Expand Down Expand Up @@ -112,54 +111,4 @@ static String getActualVersion() {
static void resetCheckForNewVersion() {
CheckForNewJobRunrVersion.isFirstRun = true;
}

protected static class VersionNumber implements Comparable<VersionNumber> {

private final String completeVersion;
private final String version;
private final String qualifier;

public VersionNumber(String completeVersion) {
this.completeVersion = completeVersion;
this.version = substringBefore(completeVersion, "-");
this.qualifier = substringAfter(completeVersion, "-");
}

public String getCompleteVersion() {
return completeVersion;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof VersionNumber) {
return completeVersion.equals(((VersionNumber) obj).completeVersion);
}
return false;
}

@Override
public int hashCode() {
return Objects.hashCode(completeVersion);
}

@Override
public int compareTo(VersionNumber o) {
int versionsCompared = this.version.compareTo(o.version);
if (versionsCompared == 0) {
if (isNullOrEmpty(qualifier) && isNullOrEmpty(o.qualifier)) {
return 0;
} else if (isNullOrEmpty(qualifier) && isNotNullOrEmpty(o.qualifier)) {
return 1;
} else if (isNotNullOrEmpty(qualifier) && isNullOrEmpty(o.qualifier)) {
return -1;
} else {
return qualifier.compareTo(o.qualifier);
}
} else {
return versionsCompared;
}
}


}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package org.jobrunr.server.zookeeper;

import org.jobrunr.server.BackgroundJobServerConfiguration;
import org.jobrunr.server.BackgroundJobServerConfigurationReader;
import org.jobrunr.server.zookeeper.tasks.ZooKeeperTaskInfo;

import java.time.Instant;

public class ThreadIdleTaskInfo implements ZooKeeperTaskInfo {

private final BackgroundJobServerConfiguration getBackgroundJobServerConfiguration;
private final BackgroundJobServerConfigurationReader configuration;
private final Instant runStartTime;

public ThreadIdleTaskInfo(BackgroundJobServerConfiguration getBackgroundJobServerConfiguration) {
this.getBackgroundJobServerConfiguration = getBackgroundJobServerConfiguration;
public ThreadIdleTaskInfo(BackgroundJobServerConfigurationReader getBackgroundJobServerConfiguration) {
this.configuration = getBackgroundJobServerConfiguration;
this.runStartTime = Instant.now();
}

@Override
public BackgroundJobServerConfiguration getBackgroundJobServerConfiguration() {
return getBackgroundJobServerConfiguration;
public BackgroundJobServerConfigurationReader getBackgroundJobServerConfiguration() {
return configuration;
}

@Override
Expand Down

0 comments on commit 44c54dc

Please sign in to comment.