Search code examples
kotlinmicronautjobrunr

Jobrunr: How to add custom filters for job?


I am trying to integrate jobrunr 5.1.6 to my micronaut application. I am unable to get the filter implementing ApplyStateFilter triggered when the job is processed. Please see below:

class JobCompletionFilter : ApplyStateFilter {

override fun onStateApplied(job: Job?, oldState: JobState?, newState: JobState?) {
    if (newState != null) {
        if (isFailed(newState) && maxAmountOfRetriesReached(job)) {
            // Failed after retry. Add logs and handle strategy logic
        } else if (newState is SucceededState) {
            // Job succeeded. Add job completion logic
        }
    }
}

How do I inject this filter for all of my jobs? Below is how I am enqueueing the jobs

@Inject
lateinit var jobScheduler: JobScheduler

jobScheduler.enqueue {jobProcessor.execute(job)}

Solution

  • In the free version, you will have to manually configure JobRunr via the Fluent Java API instead of using the Spring integration. There, you have the possibility to pass the filter. An example:

    JobRunr
      .configure()
      .withJobFilter(...) // pass your job filters here 
    

    For more info, see the fluent API.

    In the Pro version, any Job Filter which is a Spring Bean (e.g. an @Component or @Service) will automatically be executed without any additional configuration. For more info, see the docs.