package net.libertacasa.pubsh.web; import java.time.Instant; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.Task; import com.github.kagkarlsson.scheduler.task.helper.Tasks; //import static com.github.kagkarlsson.scheduler.task.schedule.Schedules.fixedDelay; @Configuration public class SchedulerBean { // @Bean // Task recurringSampleTask(CounterService counter) { // return Tasks // .recurring("recurring-sample-task", fixedDelay(Duration.ofMinutes(1))) // .execute((instance, ctx) -> { // System.out.printf("Recurring testing task. Instance: %s, ctx: %s\n", instance, ctx); // CounterService.increase(); // }); // } @Bean public static Task shellRemovalTask() { return Tasks.oneTime("shell-removal") .execute((instance, ctx) -> { System.out.printf("Running container removal task - Instance: %s, ctx: %s\n", instance, ctx); String username = instance.getId().split("&")[0]; String containerid = instance.getId().split("&")[1]; Docker.deleteShell(username, containerid); }); } @Bean Task sampleOneTimeTask() { return Tasks.oneTime("one-time-sample-task") .execute((instance, ctx) -> { System.out.printf("OK, one-shot testing task.\n"); }); } // keeping this as a quick way to check if the scheduler booted up after an application restart @Bean CommandLineRunner executeOnStartup(Scheduler scheduler, Task sampleOneTimeTask) { System.out.println("Scheduling one-shot testing task to execute now."); return ignored -> scheduler.schedule( sampleOneTimeTask.instance("command-line-runner"), Instant.now() ); } }