@@ -51,6 +51,7 @@ public void failedEvent(String uid, Event event) {}
51
51
private final ExecutorService executor ;
52
52
private final String controllerName ;
53
53
private final ReentrantLock lock = new ReentrantLock ();
54
+ private volatile boolean running ;
54
55
private DefaultEventSourceManager <R > eventSourceManager ;
55
56
56
57
public DefaultEventHandler (ConfiguredController <R > controller ) {
@@ -67,6 +68,7 @@ public DefaultEventHandler(ConfiguredController<R> controller) {
67
68
68
69
private DefaultEventHandler (ExecutorService executor , String relatedControllerName ,
69
70
EventDispatcher <R > eventDispatcher , Retry retry ) {
71
+ this .running = true ;
70
72
this .executor =
71
73
executor == null
72
74
? new ScheduledThreadPoolExecutor (
@@ -75,27 +77,27 @@ private DefaultEventHandler(ExecutorService executor, String relatedControllerNa
75
77
this .controllerName = relatedControllerName ;
76
78
this .eventDispatcher = eventDispatcher ;
77
79
this .retry = retry ;
78
- eventBuffer = new EventBuffer ();
79
- }
80
-
81
- public void setEventSourceManager (DefaultEventSourceManager <R > eventSourceManager ) {
82
- this .eventSourceManager = eventSourceManager ;
80
+ this .eventBuffer = new EventBuffer ();
83
81
}
84
82
85
83
public static void setEventMonitor (EventMonitor monitor ) {
86
84
DefaultEventHandler .monitor = monitor ;
87
85
}
88
86
89
- public interface EventMonitor {
90
- void processedEvent (String uid , Event event );
91
-
92
- void failedEvent (String uid , Event event );
87
+ public void setEventSourceManager (DefaultEventSourceManager <R > eventSourceManager ) {
88
+ this .eventSourceManager = eventSourceManager ;
93
89
}
94
90
95
91
@ Override
96
92
public void handleEvent (Event event ) {
93
+
97
94
try {
98
95
lock .lock ();
96
+
97
+ if (!this .running ) {
98
+ log .debug ("Skipping event: {} because the event handler is shutting down" , event );
99
+ }
100
+
99
101
log .debug ("Received event: {}" , event );
100
102
101
103
final Predicate <CustomResource > selector = event .getCustomResourcesSelector ();
@@ -109,6 +111,16 @@ public void handleEvent(Event event) {
109
111
}
110
112
}
111
113
114
+ @ Override
115
+ public void close () {
116
+ try {
117
+ lock .lock ();
118
+ this .running = false ;
119
+ } finally {
120
+ lock .unlock ();
121
+ }
122
+ }
123
+
112
124
private void executeBufferedEvents (String customResourceUid ) {
113
125
boolean newEventForResourceId = eventBuffer .containsEvents (customResourceUid );
114
126
boolean controllerUnderExecution = isControllerUnderExecution (customResourceUid );
@@ -143,6 +155,10 @@ void eventProcessingFinished(
143
155
ExecutionScope <R > executionScope , PostExecutionControl <R > postExecutionControl ) {
144
156
try {
145
157
lock .lock ();
158
+ if (!running ) {
159
+ return ;
160
+ }
161
+
146
162
log .debug (
147
163
"Event processing finished. Scope: {}, PostExecutionControl: {}" ,
148
164
executionScope ,
@@ -279,6 +295,12 @@ private void unsetUnderExecution(String customResourceUid) {
279
295
underProcessing .remove (customResourceUid );
280
296
}
281
297
298
+ public interface EventMonitor {
299
+ void processedEvent (String uid , Event event );
300
+
301
+ void failedEvent (String uid , Event event );
302
+ }
303
+
282
304
private class ControllerExecution implements Runnable {
283
305
private final ExecutionScope <R > executionScope ;
284
306
0 commit comments