Fix error handling to be in the same thread

This commit is contained in:
Andrii Shvaika 2022-10-20 11:38:16 +03:00
parent 4ea96fa289
commit 4e17a86a8d

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.script.api; package org.thingsboard.script.api;
import com.google.common.util.concurrent.AsyncFunction;
import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
@ -179,18 +180,10 @@ public abstract class AbstractScriptInvokeService implements ScriptInvokeService
future = Futures.withTimeout(future, timeout, TimeUnit.MILLISECONDS, timeoutExecutorService); future = Futures.withTimeout(future, timeout, TimeUnit.MILLISECONDS, timeoutExecutorService);
} }
Futures.addCallback(future, statsCallback, getCallbackExecutor()); Futures.addCallback(future, statsCallback, getCallbackExecutor());
Futures.addCallback(future, new FutureCallback<T>() { return Futures.catchingAsync(future, Exception.class, input -> {
@Override handleScriptException(scriptId, input);
public void onSuccess(@Nullable T result) { return Futures.immediateFailedFuture(input);
//do nothing }, MoreExecutors.directExecutor());
}
@Override
public void onFailure(Throwable t) {
handleScriptException(scriptId, t);
}
}, getCallbackExecutor());
return future;
} }
private void handleScriptException(UUID scriptId, Throwable t) { private void handleScriptException(UUID scriptId, Throwable t) {
@ -216,14 +209,15 @@ public abstract class AbstractScriptInvokeService implements ScriptInvokeService
} }
if (blockList) { if (blockList) {
BlockedScriptInfo disableListInfo = disabledScripts.computeIfAbsent(scriptId, key -> new BlockedScriptInfo(getMaxBlackListDurationSec())); BlockedScriptInfo disableListInfo = disabledScripts.computeIfAbsent(scriptId, key -> new BlockedScriptInfo(getMaxBlackListDurationSec()));
int counter = disableListInfo.incrementAndGet();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Script has exception and will increment counter {} on disabledFunctions for id {}, exception {}, cause {}, scriptBody {}", log.debug("Script has exception counter {} on disabledFunctions for id {}, exception {}, cause {}, scriptBody {}",
disableListInfo.get(), scriptId, t, t.getCause(), scriptBody); counter, scriptId, t, t.getCause(), scriptBody);
} else { } else {
log.warn("Script has exception and will increment counter {} on disabledFunctions for id {}, exception {}", log.warn("Script has exception counter {} on disabledFunctions for id {}, exception {}",
disableListInfo.get(), scriptId, t.getMessage()); counter, scriptId, t.getMessage());
} }
disableListInfo.incrementAndGet();
} }
} }