Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test that non-existing job won't keep the executor forever #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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