|
|
|
@ -25,11 +25,8 @@ import org.thingsboard.common.util.ThingsBoardThreadFactory; |
|
|
|
import org.thingsboard.server.common.data.ApiUsageRecordKey; |
|
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.stats.TbApiUsageReportClient; |
|
|
|
import org.thingsboard.server.common.stats.TbApiUsageStateClient; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.Executor; |
|
|
|
@ -45,9 +42,6 @@ import static java.lang.String.format; |
|
|
|
public abstract class AbstractScriptInvokeService implements ScriptInvokeService { |
|
|
|
|
|
|
|
protected final Map<UUID, BlockedScriptInfo> disabledScripts = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
private final Optional<TbApiUsageStateClient> apiUsageStateClient; |
|
|
|
private final Optional<TbApiUsageReportClient> apiUsageReportClient; |
|
|
|
private final AtomicInteger pushedMsgs = new AtomicInteger(0); |
|
|
|
private final AtomicInteger invokeMsgs = new AtomicInteger(0); |
|
|
|
private final AtomicInteger evalMsgs = new AtomicInteger(0); |
|
|
|
@ -59,11 +53,6 @@ public abstract class AbstractScriptInvokeService implements ScriptInvokeService |
|
|
|
|
|
|
|
protected ScheduledExecutorService timeoutExecutorService; |
|
|
|
|
|
|
|
protected AbstractScriptInvokeService(Optional<TbApiUsageStateClient> apiUsageStateClient, Optional<TbApiUsageReportClient> apiUsageReportClient) { |
|
|
|
this.apiUsageStateClient = apiUsageStateClient; |
|
|
|
this.apiUsageReportClient = apiUsageReportClient; |
|
|
|
} |
|
|
|
|
|
|
|
protected long getMaxEvalRequestsTimeout() { |
|
|
|
return getMaxInvokeRequestsTimeout(); |
|
|
|
} |
|
|
|
@ -88,6 +77,9 @@ public abstract class AbstractScriptInvokeService implements ScriptInvokeService |
|
|
|
|
|
|
|
protected abstract boolean isScriptPresent(UUID scriptId); |
|
|
|
|
|
|
|
protected abstract boolean isExecEnabled(TenantId tenantId); |
|
|
|
protected abstract void reportExecution(TenantId tenantId, CustomerId customerId); |
|
|
|
|
|
|
|
protected abstract ListenableFuture<UUID> doEvalScript(TenantId tenantId, ScriptType scriptType, String scriptBody, UUID scriptId, String[] argNames); |
|
|
|
|
|
|
|
protected abstract TbScriptExecutionTask doInvokeFunction(UUID scriptId, Object[] args); |
|
|
|
@ -122,7 +114,7 @@ public abstract class AbstractScriptInvokeService implements ScriptInvokeService |
|
|
|
|
|
|
|
@Override |
|
|
|
public ListenableFuture<UUID> eval(TenantId tenantId, ScriptType scriptType, String scriptBody, String... argNames) { |
|
|
|
if (!apiUsageStateClient.isPresent() || apiUsageStateClient.get().getApiUsageState(tenantId).isJsExecEnabled()) { |
|
|
|
if (isExecEnabled(tenantId)) { |
|
|
|
if (scriptBodySizeExceeded(scriptBody)) { |
|
|
|
return error(format("Script body exceeds maximum allowed size of %s symbols", getMaxScriptBodySize())); |
|
|
|
} |
|
|
|
@ -137,7 +129,7 @@ public abstract class AbstractScriptInvokeService implements ScriptInvokeService |
|
|
|
|
|
|
|
@Override |
|
|
|
public ListenableFuture<Object> invokeScript(TenantId tenantId, CustomerId customerId, UUID scriptId, Object... args) { |
|
|
|
if (!apiUsageStateClient.isPresent() || apiUsageStateClient.get().getApiUsageState(tenantId).isJsExecEnabled()) { |
|
|
|
if (isExecEnabled(tenantId)) { |
|
|
|
if (!isScriptPresent(scriptId)) { |
|
|
|
return error("No compiled script found for scriptId: [" + scriptId + "]!"); |
|
|
|
} |
|
|
|
@ -148,7 +140,7 @@ public abstract class AbstractScriptInvokeService implements ScriptInvokeService |
|
|
|
)); |
|
|
|
return Futures.immediateFailedFuture(handleScriptException(scriptId, null, t)); |
|
|
|
} |
|
|
|
apiUsageReportClient.ifPresent(client -> client.report(tenantId, customerId, ApiUsageRecordKey.JS_EXEC_COUNT, 1)); |
|
|
|
reportExecution(tenantId, customerId); |
|
|
|
pushedMsgs.incrementAndGet(); |
|
|
|
log.trace("[{}] InvokeScript uuid {} with timeout {}ms", tenantId, scriptId, getMaxInvokeRequestsTimeout()); |
|
|
|
var task = doInvokeFunction(scriptId, args); |
|
|
|
|