Skip to content

Commit

Permalink
Merge pull request #44 from agorapulse/chore/verify-non-existing-job-…
Browse files Browse the repository at this point in the history
…keep-the-executor-hanging

test that non-existing job won't keep the executor forever
  • Loading branch information
musketyr authored Nov 28, 2024
2 parents 768c870 + 0dc9a63 commit ea18e80
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
import com.agorapulse.worker.event.JobExecutionFinishedEvent;
import com.agorapulse.worker.event.JobExecutionResultEvent;
import com.agorapulse.worker.event.JobExecutionStartedEvent;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.runtime.context.scope.Refreshable;
import io.micronaut.runtime.context.scope.refresh.RefreshEvent;
import io.micronaut.runtime.context.scope.refresh.RefreshEventListener;
import io.micronaut.runtime.event.annotation.EventListener;
import jakarta.inject.Singleton;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Singleton
public class JobExecutionRecorder {
@Refreshable
public class JobExecutionRecorder implements RefreshEventListener {

private final List<JobExecutionStartedEvent> startedEvents = new ArrayList<>();
private final List<JobExecutionFinishedEvent> finishedEvents = new ArrayList<>();
Expand All @@ -48,18 +54,30 @@ public void onJobResult(JobExecutionResultEvent event) {
resultEvents.add(event);
}

public final List<JobExecutionStartedEvent> getStartedEvents() {
public List<JobExecutionStartedEvent> getStartedEvents() {
return List.copyOf(startedEvents);
}

public final List<JobExecutionFinishedEvent> getFinishedEvents() {
public List<JobExecutionFinishedEvent> getFinishedEvents() {
return List.copyOf(finishedEvents);
}

public final List<JobExecutionResultEvent> getResultEvents() {
public List<JobExecutionResultEvent> getResultEvents() {
return List.copyOf(resultEvents);
}

@Override
public @NonNull Set<String> getObservedConfigurationPrefixes() {
return Set.of();
}

@Override
public void onApplicationEvent(RefreshEvent event) {
startedEvents.clear();
finishedEvents.clear();
resultEvents.clear();
}

@Override
public String toString() {
return "JobExecutionRecorder{startedEvents=%s, finishedEvents=%s, resultEvents=%s}".formatted(startedEvents, finishedEvents, resultEvents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class JobRunnerSpec extends Specification {
exitHandler.success
}

void 'non-existing job will not hang forever'() {
when:
JobRunner runner = new JobRunner(context)
runner.run('test-job-one', 'test-job-zero', 'test-job-hundred')
then:
'test-job-one' in recorder.finishedEvents*.name
}

void 'runner waits until all events are generated'() {
when:
JobRunner runner = new JobRunner(context)
Expand Down Expand Up @@ -105,7 +113,7 @@ class JobRunnerSpec extends Specification {
}

@Property(name = 'worker.jobs.test-job-three.enabled', value = 'true')
@Property(name = 'worker.jobs.test-job-three.initial-delay', value = '20ms')
@Property(name = 'worker.jobs.test-job-three.initial-delay', value = '5s')
void 'only selected jobs are executed ignoring the enabled setting'() {
when:
JobRunner runner = new JobRunner(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
@Singleton
public class TestJob {

@Job("testJobZero")
public void recordingJobZero() {
// do nothing
}

@Job("test-job-one")
public void recordingJobOne() {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
worker:
jobs:
test-job-zero:
fork: 1
cron: '0 0 4 * * *' #Every day at 4am
enabled: true
producer:
queue-name: report_FacebookCompetitorSynchronize
queue-type: sqs

0 comments on commit ea18e80

Please sign in to comment.