|
| 1 | +package net.javadiscord.javabot2.tasks; |
| 2 | + |
| 3 | +import net.javadiscord.javabot2.tasks.jobs.DiscordApiJob; |
| 4 | +import net.javadiscord.javabot2.tasks.jobs.UnmuteExpiredJob; |
| 5 | +import org.javacord.api.DiscordApi; |
| 6 | +import org.quartz.*; |
| 7 | +import org.quartz.impl.StdSchedulerFactory; |
| 8 | + |
| 9 | +/** |
| 10 | + * This class is responsible for setting up all scheduled tasks that the bot |
| 11 | + * should run periodically, using the Quartz {@link Scheduler}. To add new tasks |
| 12 | + * to the schedule, add them to the {@link ScheduledTasks#scheduleAllTasks(Scheduler, DiscordApi)} |
| 13 | + * method. |
| 14 | + */ |
| 15 | +public class ScheduledTasks { |
| 16 | + // Hide the constructor. |
| 17 | + private ScheduledTasks() {} |
| 18 | + |
| 19 | + /** |
| 20 | + * Initializes all scheduled jobs and starts the scheduler. Also adds a |
| 21 | + * shutdown hook that gracefully stops the scheduler when the program ends. |
| 22 | + * @param api The Discord API, which may be needed by some jobs. |
| 23 | + * @throws SchedulerException If an error occurs while starting the scheduler. |
| 24 | + */ |
| 25 | + public static void init(DiscordApi api) throws SchedulerException { |
| 26 | + Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); |
| 27 | + scheduleAllTasks(scheduler, api); |
| 28 | + scheduler.start(); |
| 29 | + // Add a hook to shut down the scheduler cleanly when the program terminates. |
| 30 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 31 | + try { |
| 32 | + scheduler.shutdown(); |
| 33 | + } catch (SchedulerException e) { |
| 34 | + e.printStackTrace(); |
| 35 | + } |
| 36 | + })); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * This method is where all tasks are scheduled. <strong>Add scheduled tasks |
| 41 | + * to the scheduler using this method!</strong> |
| 42 | + * @param scheduler The scheduler to use. |
| 43 | + * @param api The Discord API. |
| 44 | + * @throws SchedulerException If an error occurs while adding a task. |
| 45 | + */ |
| 46 | + private static void scheduleAllTasks(Scheduler scheduler, DiscordApi api) throws SchedulerException { |
| 47 | + scheduleApiJob(scheduler, api, UnmuteExpiredJob.class, SimpleScheduleBuilder.repeatMinutelyForever()); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Convenience method for scheduling an API-dependent job using a single |
| 52 | + * trigger that follows a given schedule. |
| 53 | + * @param scheduler The scheduler to add the job to. |
| 54 | + * @param api The Discord API. |
| 55 | + * @param type The type of job to schedule. |
| 56 | + * @param scheduleBuilder A schedule builder that the trigger will use. |
| 57 | + * @throws SchedulerException If an error occurs while adding the job. |
| 58 | + * @see SimpleScheduleBuilder |
| 59 | + * @see CronScheduleBuilder |
| 60 | + * @see CalendarIntervalScheduleBuilder |
| 61 | + */ |
| 62 | + private static void scheduleApiJob( |
| 63 | + Scheduler scheduler, |
| 64 | + DiscordApi api, |
| 65 | + Class<? extends DiscordApiJob> type, |
| 66 | + ScheduleBuilder<?> scheduleBuilder |
| 67 | + ) throws SchedulerException { |
| 68 | + scheduler.scheduleJob( |
| 69 | + DiscordApiJob.build(type, api), |
| 70 | + TriggerBuilder.newTrigger().withSchedule(scheduleBuilder).build() |
| 71 | + ); |
| 72 | + } |
| 73 | +} |
0 commit comments