2
2
3
3
import java .time .Duration ;
4
4
import java .time .LocalDateTime ;
5
+ import java .util .concurrent .Executors ;
6
+ import java .util .concurrent .ScheduledExecutorService ;
7
+ import java .util .concurrent .TimeUnit ;
5
8
6
9
import io .fabric8 .kubernetes .api .model .HasMetadata ;
7
10
import io .javaoperatorsdk .operator .api .reconciler .IndexedResourceCache ;
8
11
9
12
public class PeriodicCleanerExpectationManager <P extends HasMetadata >
10
13
extends ExpectationManager <P > {
11
14
15
+ private final ScheduledExecutorService scheduler =
16
+ Executors .newScheduledThreadPool (
17
+ 1 ,
18
+ r -> {
19
+ Thread thread = Executors .defaultThreadFactory ().newThread (r );
20
+ thread .setDaemon (true );
21
+ return thread ;
22
+ });
23
+
12
24
private final Duration cleanupDelayAfterExpiration ;
13
25
private final IndexedResourceCache <P > primaryCache ;
14
26
15
- // todo fixes schedule
16
27
public PeriodicCleanerExpectationManager (Duration period , Duration cleanupDelayAfterExpiration ) {
17
- this .cleanupDelayAfterExpiration = cleanupDelayAfterExpiration ;
18
- this .primaryCache = null ;
28
+ this (period , cleanupDelayAfterExpiration , null );
19
29
}
20
30
21
31
public PeriodicCleanerExpectationManager (Duration period , IndexedResourceCache <P > primaryCache ) {
22
- this .cleanupDelayAfterExpiration = null ;
32
+ this (period , null , primaryCache );
33
+ }
34
+
35
+ private PeriodicCleanerExpectationManager (
36
+ Duration period , Duration cleanupDelayAfterExpiration , IndexedResourceCache <P > primaryCache ) {
37
+ this .cleanupDelayAfterExpiration = cleanupDelayAfterExpiration ;
23
38
this .primaryCache = primaryCache ;
39
+ scheduler .scheduleWithFixedDelay (
40
+ this ::clean , period .toMillis (), period .toMillis (), TimeUnit .MICROSECONDS );
24
41
}
25
42
26
43
public void clean () {
@@ -40,4 +57,8 @@ public void clean() {
40
57
}
41
58
});
42
59
}
60
+
61
+ void stop () {
62
+ scheduler .shutdownNow ();
63
+ }
43
64
}
0 commit comments