Job Filters
Extend JobRunr with extra business processes using Job Filter Beans
A Job Filter
can be used to extend the functionality of JobRunr with extra business processes when a job succeeds or fails. They also exist in the free version but in the Pro version integration is a lot easier as any Spring / Micronaut / Quarkus Bean can become a Job Filter
.
Usage
To create a Job Filter, just implement a bean with the interface JobClientFilter
or JobServerFilter
. Other filters are also available like the ApplyStateFilter
and the ElectStateFilter
.
@Component
public class NotifyJobCreatedFilter implements JobClientFilter, JobServerFilter {
private SlackNotificationService slackNotificationService;
public NotifyJobCreatedFilter(SlackNotificationService slackNotificationService) {
this.slackNotificationService = slackNotificationService;
}
void onCreated(Job job) {
if("Monthly Stripe Report".equals(job.getJobName())) {
slackNotificationService.sendNotification("#accounting-team", "Monthly Stripe Report is being generated");
}
}
void onProcessingSucceeded(Job job) {
if("Monthly Stripe Report".equals(job.getJobName())) {
slackNotificationService.sendNotification("#accounting-team", "Monthly Stripe Report is generated.");
}
}
void onProcessingFailed(Job job, Exception e) {
if("Monthly Stripe Report".equals(job.getJobName())) {
// optional logging that the job failed. Not sending a notification as Job will still be retried
}
}
void onFailedAfterRetries(Job job) {
if("Monthly Stripe Report".equals(job.getJobName())) {
Optional<FailedState> lastJobStateOfType = job.getLastJobStateOfType(FailedState.class);
String exceptionMessage = lastJobStateOfType.map(s -> s.getException().getMessage()).orElse("Unknown exception");
slackNotificationService.sendNotification("#support-team", "Error generating Monthly Stripe Report." + exceptionMessage);
}
}
}