@@ -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,28 @@ 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
+ return ;
100
+ }
101
+
99
102
log .debug ("Received event: {}" , event );
100
103
101
104
final Predicate <CustomResource > selector = event .getCustomResourcesSelector ();
@@ -109,6 +112,16 @@ public void handleEvent(Event event) {
109
112
}
110
113
}
111
114
115
+ @ Override
116
+ public void close () {
117
+ try {
118
+ lock .lock ();
119
+ this .running = false ;
120
+ } finally {
121
+ lock .unlock ();
122
+ }
123
+ }
124
+
112
125
private void executeBufferedEvents (String customResourceUid ) {
113
126
boolean newEventForResourceId = eventBuffer .containsEvents (customResourceUid );
114
127
boolean controllerUnderExecution = isControllerUnderExecution (customResourceUid );
@@ -143,6 +156,10 @@ void eventProcessingFinished(
143
156
ExecutionScope <R > executionScope , PostExecutionControl <R > postExecutionControl ) {
144
157
try {
145
158
lock .lock ();
159
+ if (!running ) {
160
+ return ;
161
+ }
162
+
146
163
log .debug (
147
164
"Event processing finished. Scope: {}, PostExecutionControl: {}" ,
148
165
executionScope ,
@@ -279,6 +296,12 @@ private void unsetUnderExecution(String customResourceUid) {
279
296
underProcessing .remove (customResourceUid );
280
297
}
281
298
299
+ public interface EventMonitor {
300
+ void processedEvent (String uid , Event event );
301
+
302
+ void failedEvent (String uid , Event event );
303
+ }
304
+
282
305
private class ControllerExecution implements Runnable {
283
306
private final ExecutionScope <R > executionScope ;
284
307
0 commit comments