mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-19 21:50:45 +00:00
different exception propagation.
This commit is contained in:
@ -26,6 +26,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import com.kingsrook.qqq.backend.core.actions.ActionHelper;
|
||||
import com.kingsrook.qqq.backend.core.actions.scripts.logging.QCodeExecutionLoggerInterface;
|
||||
import com.kingsrook.qqq.backend.core.actions.scripts.logging.ScriptExecutionLoggerInterface;
|
||||
@ -73,79 +74,86 @@ public class RunAdHocRecordScriptAction
|
||||
*******************************************************************************/
|
||||
public void run(RunAdHocRecordScriptInput input, RunAdHocRecordScriptOutput output) throws QException
|
||||
{
|
||||
ActionHelper.validateSession(input);
|
||||
|
||||
/////////////////////////
|
||||
// figure out the code //
|
||||
/////////////////////////
|
||||
ScriptRevision scriptRevision = getScriptRevision(input);
|
||||
if(scriptRevision == null)
|
||||
try
|
||||
{
|
||||
throw (new QException("Script revision was not found."));
|
||||
}
|
||||
ActionHelper.validateSession(input);
|
||||
|
||||
////////////////////////////
|
||||
// figure out the records //
|
||||
////////////////////////////
|
||||
QTableMetaData table = QContext.getQInstance().getTable(input.getTableName());
|
||||
if(CollectionUtils.nullSafeIsEmpty(input.getRecordList()))
|
||||
/////////////////////////
|
||||
// figure out the code //
|
||||
/////////////////////////
|
||||
ScriptRevision scriptRevision = getScriptRevision(input);
|
||||
if(scriptRevision == null)
|
||||
{
|
||||
throw (new QException("Script revision was not found."));
|
||||
}
|
||||
|
||||
////////////////////////////
|
||||
// figure out the records //
|
||||
////////////////////////////
|
||||
QTableMetaData table = QContext.getQInstance().getTable(input.getTableName());
|
||||
if(CollectionUtils.nullSafeIsEmpty(input.getRecordList()))
|
||||
{
|
||||
QueryInput queryInput = new QueryInput();
|
||||
queryInput.setTableName(input.getTableName());
|
||||
queryInput.setFilter(new QQueryFilter(new QFilterCriteria(table.getPrimaryKeyField(), QCriteriaOperator.IN, input.getRecordPrimaryKeyList())));
|
||||
QueryOutput queryOutput = new QueryAction().execute(queryInput);
|
||||
input.setRecordList(queryOutput.getRecords());
|
||||
}
|
||||
|
||||
if(CollectionUtils.nullSafeIsEmpty(input.getRecordList()))
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// just return if nothing found? idk //
|
||||
////////////////////////////////////////
|
||||
LOG.info("No records supplied as input (or found via primary keys); exiting with noop");
|
||||
return;
|
||||
}
|
||||
|
||||
/////////////
|
||||
// run it! //
|
||||
/////////////
|
||||
ExecuteCodeInput executeCodeInput = new ExecuteCodeInput();
|
||||
executeCodeInput.setInput(new HashMap<>(Objects.requireNonNullElseGet(input.getInputValues(), HashMap::new)));
|
||||
executeCodeInput.getInput().put("records", new ArrayList<>(input.getRecordList()));
|
||||
executeCodeInput.setContext(new HashMap<>());
|
||||
if(input.getOutputObject() != null)
|
||||
{
|
||||
executeCodeInput.getContext().put("output", input.getOutputObject());
|
||||
}
|
||||
|
||||
if(input.getScriptUtils() != null)
|
||||
{
|
||||
executeCodeInput.getContext().put("scriptUtils", input.getScriptUtils());
|
||||
}
|
||||
|
||||
executeCodeInput.getContext().put("api", new ScriptApi());
|
||||
|
||||
executeCodeInput.setCodeReference(new QCodeReference().withInlineCode(scriptRevision.getContents()).withCodeType(QCodeType.JAVA_SCRIPT)); // todo - code type as attribute of script!!
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// let caller supply a logger, or by default use StoreScriptLogAndScriptLogLineExecutionLogger //
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
QCodeExecutionLoggerInterface executionLogger = Objects.requireNonNullElseGet(input.getLogger(), () -> new StoreScriptLogAndScriptLogLineExecutionLogger(scriptRevision.getScriptId(), scriptRevision.getId()));
|
||||
executeCodeInput.setExecutionLogger(executionLogger);
|
||||
if(executionLogger instanceof ScriptExecutionLoggerInterface scriptExecutionLoggerInterface)
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// if logger is aware of scripts (as opposed to a generic CodeExecution logger), give it the ids. //
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
scriptExecutionLoggerInterface.setScriptId(scriptRevision.getScriptId());
|
||||
scriptExecutionLoggerInterface.setScriptRevisionId(scriptRevision.getId());
|
||||
}
|
||||
|
||||
ExecuteCodeOutput executeCodeOutput = new ExecuteCodeOutput();
|
||||
new ExecuteCodeAction().run(executeCodeInput, executeCodeOutput);
|
||||
|
||||
output.setOutput(executeCodeOutput.getOutput());
|
||||
output.setLogger(executionLogger);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
QueryInput queryInput = new QueryInput();
|
||||
queryInput.setTableName(input.getTableName());
|
||||
queryInput.setFilter(new QQueryFilter(new QFilterCriteria(table.getPrimaryKeyField(), QCriteriaOperator.IN, input.getRecordPrimaryKeyList())));
|
||||
QueryOutput queryOutput = new QueryAction().execute(queryInput);
|
||||
input.setRecordList(queryOutput.getRecords());
|
||||
output.setException(Optional.of(e));
|
||||
}
|
||||
|
||||
if(CollectionUtils.nullSafeIsEmpty(input.getRecordList()))
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// just return if nothing found? idk //
|
||||
////////////////////////////////////////
|
||||
LOG.info("No records supplied as input (or found via primary keys); exiting with noop");
|
||||
return;
|
||||
}
|
||||
|
||||
/////////////
|
||||
// run it! //
|
||||
/////////////
|
||||
ExecuteCodeInput executeCodeInput = new ExecuteCodeInput();
|
||||
executeCodeInput.setInput(new HashMap<>(Objects.requireNonNullElseGet(input.getInputValues(), HashMap::new)));
|
||||
executeCodeInput.getInput().put("records", new ArrayList<>(input.getRecordList()));
|
||||
executeCodeInput.setContext(new HashMap<>());
|
||||
if(input.getOutputObject() != null)
|
||||
{
|
||||
executeCodeInput.getContext().put("output", input.getOutputObject());
|
||||
}
|
||||
|
||||
if(input.getScriptUtils() != null)
|
||||
{
|
||||
executeCodeInput.getContext().put("scriptUtils", input.getScriptUtils());
|
||||
}
|
||||
|
||||
executeCodeInput.getContext().put("api", new ScriptApi());
|
||||
|
||||
executeCodeInput.setCodeReference(new QCodeReference().withInlineCode(scriptRevision.getContents()).withCodeType(QCodeType.JAVA_SCRIPT)); // todo - code type as attribute of script!!
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// let caller supply a logger, or by default use StoreScriptLogAndScriptLogLineExecutionLogger //
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
QCodeExecutionLoggerInterface executionLogger = Objects.requireNonNullElseGet(input.getLogger(), () -> new StoreScriptLogAndScriptLogLineExecutionLogger(scriptRevision.getScriptId(), scriptRevision.getId()));
|
||||
executeCodeInput.setExecutionLogger(executionLogger);
|
||||
if(executionLogger instanceof ScriptExecutionLoggerInterface scriptExecutionLoggerInterface)
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// if logger is aware of scripts (as opposed to a generic CodeExecution logger), give it the ids. //
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
scriptExecutionLoggerInterface.setScriptId(scriptRevision.getScriptId());
|
||||
scriptExecutionLoggerInterface.setScriptRevisionId(scriptRevision.getId());
|
||||
}
|
||||
|
||||
ExecuteCodeOutput executeCodeOutput = new ExecuteCodeOutput();
|
||||
new ExecuteCodeAction().run(executeCodeInput, executeCodeOutput);
|
||||
|
||||
output.setOutput(executeCodeOutput.getOutput());
|
||||
output.setLogger(executionLogger);
|
||||
}
|
||||
|
||||
|
||||
|
@ -23,6 +23,7 @@ package com.kingsrook.qqq.backend.core.model.actions.scripts;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
import com.kingsrook.qqq.backend.core.actions.scripts.logging.QCodeExecutionLoggerInterface;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.AbstractActionOutput;
|
||||
|
||||
@ -32,9 +33,9 @@ import com.kingsrook.qqq.backend.core.model.actions.AbstractActionOutput;
|
||||
*******************************************************************************/
|
||||
public class RunAdHocRecordScriptOutput extends AbstractActionOutput
|
||||
{
|
||||
private Serializable output;
|
||||
|
||||
private Serializable output;
|
||||
private QCodeExecutionLoggerInterface logger;
|
||||
private Optional<Exception> exception = Optional.empty();
|
||||
|
||||
|
||||
|
||||
@ -101,4 +102,35 @@ public class RunAdHocRecordScriptOutput extends AbstractActionOutput
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for exception
|
||||
*******************************************************************************/
|
||||
public Optional<Exception> getException()
|
||||
{
|
||||
return (this.exception);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for exception
|
||||
*******************************************************************************/
|
||||
public void setException(Optional<Exception> exception)
|
||||
{
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for exception
|
||||
*******************************************************************************/
|
||||
public RunAdHocRecordScriptOutput withException(Optional<Exception> exception)
|
||||
{
|
||||
this.exception = exception;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -65,6 +65,8 @@ public class RunRecordScriptLoadStep extends AbstractLoadStep implements Process
|
||||
.withSingularPastMessage("had the script ran against it.")
|
||||
.withPluralPastMessage("had the script ran against them.");
|
||||
|
||||
private ProcessSummaryLine unloggedExceptionLine = new ProcessSummaryLine(Status.ERROR, null, "had an error that was not logged.");
|
||||
|
||||
private List<Serializable> okScriptLogIds = new ArrayList<>();
|
||||
private List<Serializable> errorScriptLogIds = new ArrayList<>();
|
||||
|
||||
@ -115,18 +117,24 @@ public class RunRecordScriptLoadStep extends AbstractLoadStep implements Process
|
||||
Integer scriptId = runBackendStepInput.getValueInteger("scriptId");
|
||||
StoreScriptLogAndScriptLogLineExecutionLogger scriptLogger = new StoreScriptLogAndScriptLogLineExecutionLogger(null, null); // downstream these will get set!
|
||||
|
||||
RunAdHocRecordScriptInput input = new RunAdHocRecordScriptInput();
|
||||
input.setRecordList(runBackendStepInput.getRecords());
|
||||
input.setCodeReference(new AdHocScriptCodeReference().withScriptId(scriptId));
|
||||
input.setLogger(scriptLogger);
|
||||
RunAdHocRecordScriptOutput output = new RunAdHocRecordScriptOutput();
|
||||
Exception caughtException = null;
|
||||
try
|
||||
{
|
||||
RunAdHocRecordScriptInput input = new RunAdHocRecordScriptInput();
|
||||
input.setRecordList(runBackendStepInput.getRecords());
|
||||
input.setCodeReference(new AdHocScriptCodeReference().withScriptId(scriptId));
|
||||
input.setLogger(scriptLogger);
|
||||
RunAdHocRecordScriptOutput output = new RunAdHocRecordScriptOutput();
|
||||
new RunAdHocRecordScriptAction().run(input, output);
|
||||
if(output.getException().isPresent())
|
||||
{
|
||||
caughtException = output.getException().get();
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
LOG.info("Exception running record script", e, logPair("scriptId", scriptId));
|
||||
caughtException = e;
|
||||
}
|
||||
|
||||
if(scriptLogger.getScriptLog() != null)
|
||||
@ -138,6 +146,10 @@ public class RunRecordScriptLoadStep extends AbstractLoadStep implements Process
|
||||
(hadError ? errorScriptLogIds : okScriptLogIds).add(id);
|
||||
}
|
||||
}
|
||||
else if(caughtException != null)
|
||||
{
|
||||
unloggedExceptionLine.incrementCount(runBackendStepInput.getRecords().size());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -163,6 +175,8 @@ public class RunRecordScriptLoadStep extends AbstractLoadStep implements Process
|
||||
.withLinkText("Created " + String.format("%,d", errorScriptLogIds.size()) + " Script Log" + StringUtils.plural(errorScriptLogIds) + " with Errors"));
|
||||
}
|
||||
|
||||
unloggedExceptionLine.addSelfToListIfAnyCount(summary);
|
||||
|
||||
return (summary);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user