13
13
import io .camunda .zeebe .gateway .impl .broker .request .BrokerActivateJobsRequest ;
14
14
import io .camunda .zeebe .gateway .protocol .GatewayOuterClass .ActivateJobsRequest ;
15
15
import io .camunda .zeebe .gateway .protocol .GatewayOuterClass .ActivateJobsResponse ;
16
+ import io .camunda .zeebe .util .Either ;
16
17
import io .camunda .zeebe .util .sched .ScheduledTimer ;
17
18
import java .time .Duration ;
18
19
import java .util .Objects ;
19
20
import org .slf4j .Logger ;
20
21
21
- public final class LongPollingActivateJobsRequest {
22
+ public class InflightActivateJobsRequest {
22
23
23
24
private static final Logger LOG = Loggers .GATEWAY_LOGGER ;
24
25
private final long requestId ;
@@ -32,8 +33,9 @@ public final class LongPollingActivateJobsRequest {
32
33
private ScheduledTimer scheduledTimer ;
33
34
private boolean isTimedOut ;
34
35
private boolean isCompleted ;
36
+ private boolean isAborted ;
35
37
36
- public LongPollingActivateJobsRequest (
38
+ public InflightActivateJobsRequest (
37
39
final long requestId ,
38
40
final ActivateJobsRequest request ,
39
41
final ServerStreamObserver <ActivateJobsResponse > responseObserver ) {
@@ -47,7 +49,7 @@ public LongPollingActivateJobsRequest(
47
49
request .getRequestTimeout ());
48
50
}
49
51
50
- private LongPollingActivateJobsRequest (
52
+ private InflightActivateJobsRequest (
51
53
final long requestId ,
52
54
final BrokerActivateJobsRequest request ,
53
55
final ServerStreamObserver <ActivateJobsResponse > responseObserver ,
@@ -66,7 +68,7 @@ private LongPollingActivateJobsRequest(
66
68
}
67
69
68
70
public void complete () {
69
- if (isCompleted () || isCanceled ()) {
71
+ if (! isOpen ()) {
70
72
return ;
71
73
}
72
74
cancelTimerIfScheduled ();
@@ -82,26 +84,44 @@ public boolean isCompleted() {
82
84
return isCompleted ;
83
85
}
84
86
85
- public void onResponse (final ActivateJobsResponse grpcResponse ) {
86
- if (!(isCompleted () || isCanceled ())) {
87
+ /**
88
+ * Sends activated jobs to the respective client.
89
+ *
90
+ * @param activatedJobs to send back to the client
91
+ * @return an instance of {@link Either} indicating the following:
92
+ * <ul>
93
+ * <li>{@link Either#get() == true}: if the activated jobs have been sent back to the client
94
+ * <li>{@link Either#get() == false}: if the activated jobs couldn't be sent back to the
95
+ * client
96
+ * <li>{@link Either#getLeft() != null}: if sending back the activated jobs failed with an
97
+ * exception (note: in this case {@link Either#isRight() == false})
98
+ * </ul>
99
+ */
100
+ public Either <Exception , Boolean > tryToSendActivatedJobs (
101
+ final ActivateJobsResponse activatedJobs ) {
102
+ if (isOpen ()) {
87
103
try {
88
- responseObserver .onNext (grpcResponse );
104
+ responseObserver .onNext (activatedJobs );
105
+ return Either .right (true );
89
106
} catch (final Exception e ) {
90
107
LOG .warn ("Failed to send response to client." , e );
108
+ return Either .left (e );
91
109
}
92
110
}
111
+ return Either .right (false );
93
112
}
94
113
95
114
public void onError (final Throwable error ) {
96
- if (isCompleted () || isCanceled ()) {
115
+ if (! isOpen ()) {
97
116
return ;
98
117
}
99
118
cancelTimerIfScheduled ();
100
119
try {
101
120
responseObserver .onError (error );
102
121
} catch (final Exception e ) {
103
- LOG .warn ("Failed to send response to client." , e );
122
+ LOG .warn ("Failed to send terminating error to client." , e );
104
123
}
124
+ isAborted = true ;
105
125
}
106
126
107
127
public void timeout () {
@@ -163,6 +183,14 @@ private void cancelTimerIfScheduled() {
163
183
}
164
184
}
165
185
186
+ public boolean isAborted () {
187
+ return isAborted ;
188
+ }
189
+
190
+ public boolean isOpen () {
191
+ return !(isCompleted () || isCanceled () || isAborted ());
192
+ }
193
+
166
194
@ Override
167
195
public int hashCode () {
168
196
return Objects .hash (jobType , maxJobsToActivate , requestId , worker );
@@ -179,7 +207,7 @@ public boolean equals(Object obj) {
179
207
if (getClass () != obj .getClass ()) {
180
208
return false ;
181
209
}
182
- final var other = (LongPollingActivateJobsRequest ) obj ;
210
+ final var other = (InflightActivateJobsRequest ) obj ;
183
211
return Objects .equals (jobType , other .jobType )
184
212
&& maxJobsToActivate == other .maxJobsToActivate
185
213
&& requestId == other .requestId
0 commit comments