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

Better api for recurring tasks with persistent schedule (having multiple instances) #257

Merged
merged 7 commits into from
Feb 23, 2022

Conversation

kagkarlsson
Copy link
Owner

@kagkarlsson kagkarlsson commented Jan 26, 2022

Adding a bit better support for recurring tasks that can have many instances with variable schedule. Feedback is appreciated.

After requests/discussions in among others

Current example:

    @Override
    public void run(DataSource dataSource) {

        final RecurringTaskWithPersistentSchedule<SerializableCronSchedule> task =
            Tasks.recurringWithPersistentSchedule("dynamic-recurring-task", SerializableCronSchedule.class)
                .execute((taskInstance, executionContext) -> {
                    System.out.println("Instance: '" + taskInstance.getId() + "' ran using persistent schedule: " + taskInstance.getData().getSchedule());
                });

        final Scheduler scheduler = Scheduler
            .create(dataSource, task)
            .pollingInterval(Duration.ofSeconds(1))
            .registerShutdownHook()
            .build();

        scheduler.start();
        sleep(2_000);

        scheduler.schedule(task.schedulableInstance("id1", new SerializableCronSchedule("0/6 * * * * ?")));
        scheduler.schedule(task.schedulableInstance("id2", new SerializableCronSchedule("3/6 * * * * ?")));
    }

Resulting in

17:45:22.258 INFO  com.github.kagkarlsson.scheduler.Scheduler - Starting scheduler.
Instance: 'id2' ran using persistent schedule: CronSchedule pattern=3/6 * * * * ?, zone=Europe/Oslo
Instance: 'id1' ran using persistent schedule: CronSchedule pattern=0/6 * * * * ?, zone=Europe/Oslo
Instance: 'id2' ran using persistent schedule: CronSchedule pattern=3/6 * * * * ?, zone=Europe/Oslo
Instance: 'id1' ran using persistent schedule: CronSchedule pattern=0/6 * * * * ?, zone=Europe/Oslo
Instance: 'id2' ran using persistent schedule: CronSchedule pattern=3/6 * * * * ?, zone=Europe/Oslo

@kagkarlsson kagkarlsson marked this pull request as ready for review February 16, 2022 16:50
@kagkarlsson kagkarlsson merged commit e7aa9b9 into master Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant