summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java
blob: 135a4246514814cf04aa6769095eec4f70a89a38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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<Void> 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<Void> shellRemovalTask() {
	  return Tasks.oneTime("shell-removal")
	      .execute((instance, ctx) -> {
	          System.out.printf("Running container removal task - Instance: %s, ctx: %s\n", instance, ctx);
	          try {
	        	  String username = instance.getId().split("&")[0];
	        	  String containerid = instance.getId().split("&")[1];
	        	  Docker.deleteShell(username, containerid);
	          } catch (com.github.dockerjava.api.exception.NotFoundException exception) {
	        	  System.out.printf("Container does not exist\n");
	          }
	      });
	}

    @Bean
    Task<Void> 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<Void> sampleOneTimeTask) {
        System.out.println("Scheduling one-shot testing task to execute now.");

        return ignored -> scheduler.schedule(
            sampleOneTimeTask.instance("command-line-runner"),
            Instant.now()
        );    
   }
}