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

Add support for disabled cron expression "-" #216

Closed
donalmurtagh opened this issue Jul 23, 2021 · 8 comments · Fixed by #286
Closed

Add support for disabled cron expression "-" #216

donalmurtagh opened this issue Jul 23, 2021 · 8 comments · Fixed by #286

Comments

@donalmurtagh
Copy link

According to the docs, db-scheduler supports

Spring-style cron-expression.

However, a Spring cron expression of "-" means the task will never run, which is useful for disabling a task in a local/test environment.

I tried this expression with db-scheduler, but it caused this exception:

Caused by: java.lang.IllegalArgumentException: Cron expression contains 1 parts but we expect one of [6]
	at com.github.kagkarlsson.shaded.cronutils.parser.CronParser.parse(CronParser.java:120) ~[db-scheduler-10.3.jar:na]
	at com.github.kagkarlsson.scheduler.task.schedule.CronSchedule.<init>(CronSchedule.java:46) ~[db-scheduler-10.3.jar:na]
	at com.github.kagkarlsson.scheduler.task.schedule.CronSchedule.<init>(CronSchedule.java:56) ~[db-scheduler-10.3.jar:na]
	at com.github.kagkarlsson.scheduler.task.schedule.Schedules.cron(Schedules.java:39) ~[db-scheduler-10.3.jar:na]
@donalmurtagh donalmurtagh changed the title Add support for "-" cron expression Add support for disabled cron expression "-" Jul 26, 2021
@kagkarlsson
Copy link
Owner

Interesting. Yeah, that is not supported. I think I primarily meant that seconds are supported, so I probably should update the doc.

Not sure if it is possible to support that scenario just yet. When we add support for pausing/stopping an execution in db-scheduler, this would be a nice addition

@s-seidel
Copy link

We currently use this workaround, which is okay but not perfect:

    @Bean
    Task<Void> triggerCleanUp(final CleanUpService service,
            @Value("${cleanup.cron.expression}") final String cronPattern) {
        if (cronPattern == null || cronPattern.equals("-")) {
            return Tasks.custom("cleanup", Void.class).execute((i, c) -> new OnCompleteRemove<>());
        }
        return Tasks.recurring("cleanup", Schedules.cron(cronPattern)).execute((instance, ctx) -> {
            service.cleanUp();
        });
    }

@donalmurtagh
Copy link
Author

donalmurtagh commented Dec 16, 2021

This is the workaround that I use. The taskInvitationsSender and autocompleteEventsUpdater beans are jobs that are run by the scheduler.

@Configuration
public class ClusteredSchedulerConfiguration {

    private static final String DISABLED_CRON_EXPRESSION = "-";

    private static final Logger logger = LoggerFactory.getLogger(ClusteredSchedulerConfiguration.class);

    @Bean
    Task<Void> scheduleTaskInvitationsSender(ScheduledTask taskInvitationsSender,
                                             @Value("${cron.task-invitations-sender}") String schedule) {
        return scheduleTask(schedule, taskInvitationsSender);
    }

    @Bean
    Task<Void> scheduleAutocompleteEvents(ScheduledTask autocompleteEventsUpdater,
                                          @Value("${cron.autocomplete-events}") String schedule) {
        return scheduleTask(schedule, autocompleteEventsUpdater);
    }

    private Task<Void> scheduleTask(String cronExpression, ScheduledTask task) {
        boolean isTaskDisabled = DISABLED_CRON_EXPRESSION.equals(cronExpression);

        if (isTaskDisabled) {
            var taskName = task.getTaskName();
            logger.warn("The {} scheduled task is disabled in the current environment", taskName);

            /*
            A workaround for the fact that db-scheduler doesn't support Spring's disabled cron expression
            https://github.com/kagkarlsson/db-scheduler/issues/216

            Return a once-off task that is never scheduled and does nothing when executed
             */
            return Tasks.oneTime(taskName, Void.class).execute((inst, ctx) -> {
            });

        } else {
            Schedule schedule = cron(cronExpression);
            return Tasks.recurring(task.getTaskName(), schedule).execute((instance, ctx) -> task.run());
        }
    }
}

@kagkarlsson
Copy link
Owner

Hoping this is solved with #286. Does that implementation work for you?

@s-seidel
Copy link

Tested and it works well for us.

@kagkarlsson
Copy link
Owner

Excellent, thanks for the feedback!

@kagkarlsson
Copy link
Owner

Fixed in 11.2

@donalmurtagh
Copy link
Author

@kagkarlsson tested and confirmed it works, thanks very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants