Skip to content

Commit

Permalink
Cleanup some feedback from errorprone (#895)
Browse files Browse the repository at this point in the history
* Cleanup some feedback from errorprone

* Cleanup some feedback from errorprone
  • Loading branch information
rdehuyss committed Dec 14, 2023
1 parent c211a78 commit ad91b60
Show file tree
Hide file tree
Showing 38 changed files with 228 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* A wrapper around a Micrometer {@link MeterRegistry} that allows to integrate Micrometer with JobRunr.
*
* <p>
* This wrapper is needed as otherwise the JobRunrConfiguration class would have a dependency on Micrometer which is optional.
*/
public class JobRunrMicroMeterIntegration implements AutoCloseable {
Expand All @@ -23,14 +23,15 @@ public JobRunrMicroMeterIntegration(MeterRegistry meterRegistry) {

public void initialize(StorageProvider storageProvider, BackgroundJobServer backgroundJobServer) {
storageProviderMetricsBinder = new StorageProviderMetricsBinder(storageProvider, meterRegistry);
if(backgroundJobServer != null) {
if (backgroundJobServer != null) {
backgroundJobServerMetricsBinder = new BackgroundJobServerMetricsBinder(backgroundJobServer, meterRegistry);
}
}

@Override
public void close() {
storageProviderMetricsBinder.close();
if(backgroundJobServerMetricsBinder != null) {
if (backgroundJobServerMetricsBinder != null) {
backgroundJobServerMetricsBinder.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public boolean matches(String matchUrl) {
Iterator<UrlPathPart> iter1 = new MatchUrl(matchUrl).pathParts.iterator();
Iterator<UrlPathPart> iter2 = pathParts.iterator();
while (iter1.hasNext() && iter2.hasNext())
if (!(iter1.next().matches(iter2.next()))) return false;
if (!iter1.next().matches(iter2.next())) return false;

return !iter1.hasNext() && !iter2.hasNext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
public interface ApplyStateFilter extends JobFilter {

/**
* Will be invoked on state change of a {@link Job}
*
* @param job the job in which to apply the filter
* @param oldState the previous state - can be null
* @param newState the new state
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/jobrunr/jobs/lambdas/JobLambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
*
* <pre>{@code
*
* &commat;Inject
* @Inject
* MyService myService;
*
* BackgroundJob.enqueue(myService -&gt; myService.doWork("some argument"))
* BackgroundJob.enqueue(myService -> myService.doWork("some argument"))
* }</pre>
* <p>
* or
* <pre>{@code
*
* &commat;Inject
* @Inject
* MyService myService;
*
* jobScheduler.enqueue(myService -&gt; myService.doWork("some argument"))
* jobScheduler.enqueue(myService -> myService.doWork("some argument"))
* }</pre>
* <p>
* This functional interface allows you to enqueue background jobs while having an actual instance available of your service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* <pre>{@code
*
* &commat;Inject
* @Inject
* MyService myService;
*
* Stream<User> userStream = userRepository.getAllUsers();
Expand All @@ -16,7 +16,7 @@
* or
* <pre>{@code
*
* &commat;Inject
* @Inject
* MyService myService;
*
* Stream<User> userStream = userRepository.getAllUsers();
Expand Down
22 changes: 13 additions & 9 deletions core/src/main/java/org/jobrunr/scheduling/JobBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.jobrunr.utils.CollectionUtils.asSet;

/**
* This class is used to build a {@link Job} using a job lambda or a {@Link JobRequest}.
* This class is used to build a {@link Job} using a job lambda or a {@link JobRequest}.
* <p>
* You can use it as follows:
* <h5>A job lambda example:</h5>
Expand Down Expand Up @@ -66,8 +66,9 @@ public static JobBuilder aJob() {

/**
* Allows to set the id of the job. If a job with that id already exists, JobRunr will not save it again.
*
* @param jobId the id of the job
* @return the same builder instance which provides a fluent api
* @return the same builder instance which provides a fluent api
*/
public JobBuilder withId(UUID jobId) {
this.jobId = jobId;
Expand All @@ -76,8 +77,9 @@ public JobBuilder withId(UUID jobId) {

/**
* Allows to set the name of the job for the dashboard.
*
* @param jobName the name of the job for the dashboard
* @return the same builder instance which provides a fluent api
* @return the same builder instance which provides a fluent api
*/
public JobBuilder withName(String jobName) {
this.jobName = jobName;
Expand All @@ -86,8 +88,9 @@ public JobBuilder withName(String jobName) {

/**
* Allows to specify the duration after which the job should be enqueued.
*
* @param duration the duration after which the job should be enqueued
* @return the same builder instance which provides a fluent api
* @return the same builder instance which provides a fluent api
*/
public JobBuilder scheduleIn(Duration duration) {
this.scheduleAt = Instant.now().plus(duration);
Expand All @@ -96,8 +99,9 @@ public JobBuilder scheduleIn(Duration duration) {

/**
* Allows to specify the instant on which the job will be enqueued.
*
* @param scheduleAt the instant on which the job will be enqueued
* @return the same builder instance which provides a fluent api
* @return the same builder instance which provides a fluent api
*/
public JobBuilder scheduleAt(Instant scheduleAt) {
this.scheduleAt = scheduleAt;
Expand Down Expand Up @@ -186,7 +190,7 @@ public JobBuilder withJobRequest(JobRequest jobRequest) {
* @return the actual {@link Job} to create
*/
protected Job build(JobDetailsGenerator jobDetailsGenerator) {
if(jobLambda == null) {
if (jobLambda == null) {
throw new IllegalArgumentException("A jobLambda must be present.");
}
JobDetails jobDetails = jobDetailsGenerator.toJobDetails(jobLambda);
Expand All @@ -199,15 +203,15 @@ protected Job build(JobDetailsGenerator jobDetailsGenerator) {
* @return the actual {@link Job} to create
*/
protected Job build() {
if(jobRequest == null) {
if (jobRequest == null) {
throw new IllegalArgumentException("JobRequest must be present.");
}
JobDetails jobDetails = new JobDetails(jobRequest);
return build(jobDetails);
}

private Job build(JobDetails jobDetails) {
if(JobUtils.getJobAnnotation(jobDetails).isPresent()) {
if (JobUtils.getJobAnnotation(jobDetails).isPresent()) {
throw new IllegalStateException("You are combining the JobBuilder with the Job annotation which is not allowed. You can only use one of them.");
}

Expand All @@ -219,7 +223,7 @@ private Job build(JobDetails jobDetails) {
}

private void setJobName(Job job) {
if(jobName != null) {
if (jobName != null) {
job.setJobName(jobName);
}
}
Expand Down
17 changes: 12 additions & 5 deletions core/src/main/java/org/jobrunr/scheduling/JobRequestScheduler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jobrunr.scheduling;

import org.jobrunr.jobs.Job;
import org.jobrunr.jobs.JobDetails;
import org.jobrunr.jobs.JobId;
import org.jobrunr.jobs.RecurringJob;
Expand All @@ -9,7 +10,12 @@
import org.jobrunr.scheduling.interval.Interval;
import org.jobrunr.storage.StorageProvider;

import java.time.*;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;
Expand Down Expand Up @@ -50,6 +56,7 @@ public JobRequestScheduler(StorageProvider storageProvider, List<JobFilter> jobF

/**
* Creates a new {@link org.jobrunr.jobs.Job} using a {@link JobBuilder} that can be enqueued or scheduled and provides an alternative to the job annotation.
*
* @param jobBuilder the jobBuilder with all the details of the job
* @return the id of the job
*/
Expand Down Expand Up @@ -116,7 +123,7 @@ public JobId enqueue(UUID id, JobRequest jobRequest) {
public void enqueue(Stream<? extends JobRequest> input) {
input
.map(JobDetails::new)
.map(org.jobrunr.jobs.Job::new)
.map(Job::new)
.collect(batchCollector(BATCH_SIZE, this::saveJobs));
}

Expand Down Expand Up @@ -343,7 +350,7 @@ public String scheduleRecurrently(String id, String cron, ZoneId zoneId, JobRequ
* BackgroundJob.scheduleRecurrently(Duration.parse("P5D"), new MyJobRequest());
* }</pre>
*
* @param duration the duration defining the time between each instance of this recurring job.
* @param duration the duration defining the time between each instance of this recurring job.
* @param jobRequest the jobRequest which defines the recurring job
* @return the id of this recurring job which can be used to alter or delete it
*/
Expand All @@ -361,8 +368,8 @@ public String scheduleRecurrently(Duration duration, JobRequest jobRequest) {
* BackgroundJob.scheduleRecurrently("my-recurring-job", Duration.parse("P5D"), new MyJobRequest());
* }</pre>
*
* @param id the id of this recurring job which can be used to alter or delete it
* @param duration the duration defining the time between each instance of this recurring job
* @param id the id of this recurring job which can be used to alter or delete it
* @param duration the duration defining the time between each instance of this recurring job
* @param jobRequest the jobRequest which defines the recurring job
* @return the id of this recurring job which can be used to alter or delete it
*/
Expand Down
15 changes: 11 additions & 4 deletions core/src/main/java/org/jobrunr/scheduling/JobScheduler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jobrunr.scheduling;

import org.jobrunr.jobs.Job;
import org.jobrunr.jobs.JobDetails;
import org.jobrunr.jobs.JobId;
import org.jobrunr.jobs.RecurringJob;
Expand All @@ -14,7 +15,12 @@
import org.jobrunr.scheduling.interval.Interval;
import org.jobrunr.storage.StorageProvider;

import java.time.*;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;
Expand Down Expand Up @@ -65,6 +71,7 @@ public JobScheduler(StorageProvider storageProvider, JobDetailsGenerator jobDeta

/**
* Creates a new {@link org.jobrunr.jobs.Job} using a {@link JobBuilder} that can be enqueued or scheduled and provides an alternative to the job annotation.
*
* @param jobBuilder the jobBuilder with all the details of the job
* @return the id of the job
*/
Expand Down Expand Up @@ -134,7 +141,7 @@ public JobId enqueue(UUID id, JobLambda job) {
public <T> void enqueue(Stream<T> input, JobLambdaFromStream<T> jobFromStream) {
input
.map(x -> jobDetailsGenerator.toJobDetails(x, jobFromStream))
.map(org.jobrunr.jobs.Job::new)
.map(Job::new)
.collect(batchCollector(BATCH_SIZE, this::saveJobs));
}

Expand Down Expand Up @@ -184,7 +191,7 @@ public <S> JobId enqueue(UUID id, IocJobLambda<S> iocJob) {
public <S, T> void enqueue(Stream<T> input, IocJobLambdaFromStream<S, T> iocJobFromStream) {
input
.map(x -> jobDetailsGenerator.toJobDetails(x, iocJobFromStream))
.map(org.jobrunr.jobs.Job::new)
.map(Job::new)
.collect(batchCollector(BATCH_SIZE, this::saveJobs));
}

Expand Down Expand Up @@ -459,7 +466,7 @@ public <S> JobId schedule(UUID id, Instant instant, IocJobLambda<S> iocJob) {
* If no zoneId is set on the builder the jobs will be scheduled using the systemDefault timezone.
* <h5>An example:</h5>
*
<pre>{@code
* <pre>{@code
* jobScheduler.createRecurrently(aRecurringJob()
* .withCron("* * 0 * * *")
* .withDetails(() -> service.doWork());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static org.jobrunr.utils.CollectionUtils.asSet;

/**
* This class is used to build a {@link RecurringJob} using a job lambda or a {@Link JobRequest}.
* This class is used to build a {@link RecurringJob} using a job lambda or a {@link JobRequest}.
* <p>
* You can use it as follows:
* <h5>A job lambda example:</h5>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import org.jobrunr.scheduling.Schedule;

import java.time.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.BitSet;
import java.util.Calendar;

Expand Down Expand Up @@ -378,6 +384,7 @@ private BitSet getUpdatedDays(int year, int month) {
return updatedDays;
}

@Override
public void validateSchedule() {
Instant base = Instant.EPOCH;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Interval(String durationExpression) {
this.duration = Duration.parse(durationExpression);
}

@Override
public Instant next(Instant createdAtInstant, Instant currentInstant, ZoneId zoneId) {
Duration durationUntilNow = Duration.between(createdAtInstant, currentInstant);
long amountOfDurationsUntilNow = durationUntilNow.toNanos() / duration.toNanos();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.atomic.AtomicInteger;

import static org.jobrunr.jobs.states.StateName.*;
import static org.jobrunr.jobs.states.StateName.DELETED;
import static org.jobrunr.jobs.states.StateName.FAILED;
import static org.jobrunr.jobs.states.StateName.PROCESSING;
import static org.jobrunr.utils.exceptions.Exceptions.hasCause;

public class BackgroundJobPerformer implements Runnable {
Expand All @@ -35,6 +37,7 @@ public BackgroundJobPerformer(BackgroundJobServer backgroundJobServer, Job job)
this.job = job;
}

@Override
public void run() {
try {
backgroundJobServer.getJobZooKeeper().notifyThreadOccupied();
Expand Down

0 comments on commit ad91b60

Please sign in to comment.