Skip to content

Commit a96c4e5

Browse files
committed
Merge branch 'cancel-modules'
Improves ModuleRunner cancelation so that Modules are canceled when their preprocessors are canceled.
2 parents c842420 + c4de809 commit a96c4e5

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/main/java/org/scijava/module/ModuleRunner.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,19 @@ public void run() {
156156
final ModulePreprocessor canceler = preProcess();
157157
if (canceler != null) {
158158
// module execution was canceled by preprocessor
159-
cancel(title, canceler.getCancelReason());
159+
final String reason = canceler.getCancelReason();
160+
cancel(reason);
161+
cleanupAndBroadcastCancelation(title, reason);
160162
return;
161163
}
162164

163165
// execute module
164166
if (es != null) es.publish(new ModuleExecutingEvent(module));
165167
module.run();
166-
if (module instanceof Cancelable) {
167-
final Cancelable cancelable = (Cancelable) module;
168-
if (cancelable.isCanceled()) {
169-
// module execution was canceled by the module itself
170-
cancel(title, cancelable.getCancelReason());
171-
return;
172-
}
168+
if (isCanceled()) {
169+
// module execution was canceled by the module itself
170+
cleanupAndBroadcastCancelation(title, getCancelReason());
171+
return;
173172
}
174173
if (es != null) es.publish(new ModuleExecutedEvent(module));
175174

@@ -183,7 +182,9 @@ public void run() {
183182

184183
// -- Helper methods --
185184

186-
private void cancel(final String title, final String reason) {
185+
private void cleanupAndBroadcastCancelation(final String title,
186+
final String reason)
187+
{
187188
if (ss != null) ss.showStatus("Canceling command: " + title);
188189
module.cancel();
189190
if (es != null) es.publish(new ModuleCanceledEvent(module, reason));
@@ -193,4 +194,18 @@ private void cancel(final String title, final String reason) {
193194
}
194195
}
195196

197+
private boolean isCanceled() {
198+
return module instanceof Cancelable && ((Cancelable) module).isCanceled();
199+
}
200+
201+
private String getCancelReason() {
202+
return module instanceof Cancelable ?
203+
((Cancelable) module).getCancelReason() : null;
204+
}
205+
206+
private void cancel(final String reason) {
207+
if (!(module instanceof Cancelable)) return;
208+
((Cancelable) module).cancel(reason);
209+
}
210+
196211
}

0 commit comments

Comments
 (0)