Search in sources :

Example 1 with IExtensionData

use of org.apache.hop.core.IExtensionData in project hop by apache.

the class LocalPipelineEngine method prepareExecution.

@Override
public void prepareExecution() throws HopException {
    if (!(pipelineRunConfiguration.getEngineRunConfiguration() instanceof LocalPipelineRunConfiguration)) {
        throw new HopException("A local pipeline execution expects a local pipeline configuration, not an instance of class " + pipelineRunConfiguration.getEngineRunConfiguration().getClass().getName());
    }
    LocalPipelineRunConfiguration config = (LocalPipelineRunConfiguration) pipelineRunConfiguration.getEngineRunConfiguration();
    int sizeRowsSet = Const.toInt(resolve(config.getRowSetSize()), Const.ROWS_IN_ROWSET);
    setRowSetSize(sizeRowsSet);
    setSafeModeEnabled(config.isSafeModeEnabled());
    setSortingTransformsTopologically(config.isSortingTransformsTopologically());
    setGatheringMetrics(config.isGatheringMetrics());
    setFeedbackShown(config.isFeedbackShown());
    setFeedbackSize(Const.toInt(resolve(config.getFeedbackSize()), Const.ROWS_UPDATE));
    // See if we need to enable transactions...
    // 
    IExtensionData parentExtensionData = getParentPipeline();
    if (parentExtensionData == null) {
        parentExtensionData = getParentWorkflow();
    }
    String connectionGroup = null;
    if (parentExtensionData != null) {
        connectionGroup = (String) parentExtensionData.getExtensionDataMap().get(Const.CONNECTION_GROUP);
    }
    // 
    if (config.isTransactional() && connectionGroup == null) {
        // Store a value in the parent...
        // 
        connectionGroup = getPipelineMeta().getName() + " - " + UUID.randomUUID();
        // We also need to commit/rollback at the end of this pipeline...
        // We only do this when we created a new group.  Never in a child
        // 
        addExecutionFinishedListener((IExecutionFinishedListener<IPipelineEngine>) pipeline -> {
            String group = (String) pipeline.getExtensionDataMap().get(Const.CONNECTION_GROUP);
            List<Database> databases = DatabaseConnectionMap.getInstance().getDatabases(group);
            Result result = pipeline.getResult();
            for (Database database : databases) {
                try {
                    if (result.getResult() && !result.isStopped() && result.getNrErrors() == 0) {
                        try {
                            database.commit(true);
                            pipeline.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were committed at the end of the pipeline!");
                        } catch (HopDatabaseException e) {
                            throw new HopException("Error committing database connection " + database.getDatabaseMeta().getName(), e);
                        }
                    } else {
                        try {
                            database.rollback(true);
                            pipeline.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were rolled back at the end of the pipeline!");
                        } catch (HopDatabaseException e) {
                            throw new HopException("Error rolling back database connection " + database.getDatabaseMeta().getName(), e);
                        }
                    }
                } finally {
                    try {
                        database.closeConnectionOnly();
                        pipeline.getLogChannel().logDebug("Database connection '" + database.getDatabaseMeta().getName() + "' closed successfully!");
                    } catch (HopDatabaseException hde) {
                        pipeline.getLogChannel().logError("Error disconnecting from database - closeConnectionOnly failed:" + Const.CR + hde.getMessage());
                        pipeline.getLogChannel().logError(Const.getStackTracker(hde));
                    }
                }
                DatabaseConnectionMap.getInstance().removeConnection(group, null, database);
            }
        });
    }
    // 
    if (connectionGroup != null && getExtensionDataMap() != null) {
        // Set the connection group for this pipeline
        // 
        getExtensionDataMap().put(Const.CONNECTION_GROUP, connectionGroup);
    }
    super.prepareExecution();
}
Also used : IPipelineEngineRunConfiguration(org.apache.hop.pipeline.config.IPipelineEngineRunConfiguration) Database(org.apache.hop.core.database.Database) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) ILoggingObject(org.apache.hop.core.logging.ILoggingObject) IVariables(org.apache.hop.core.variables.IVariables) HopException(org.apache.hop.core.exception.HopException) IHopMetadataProvider(org.apache.hop.metadata.api.IHopMetadataProvider) UUID(java.util.UUID) Const(org.apache.hop.core.Const) IPipelineEngine(org.apache.hop.pipeline.engine.IPipelineEngine) ArrayList(java.util.ArrayList) PipelineEnginePlugin(org.apache.hop.pipeline.engine.PipelineEnginePlugin) INamedParameters(org.apache.hop.core.parameters.INamedParameters) List(java.util.List) Pipeline(org.apache.hop.pipeline.Pipeline) DatabaseConnectionMap(org.apache.hop.core.database.map.DatabaseConnectionMap) Result(org.apache.hop.core.Result) IExtensionData(org.apache.hop.core.IExtensionData) PipelineRunConfiguration(org.apache.hop.pipeline.config.PipelineRunConfiguration) PipelineMeta(org.apache.hop.pipeline.PipelineMeta) PipelineEngineCapabilities(org.apache.hop.pipeline.engine.PipelineEngineCapabilities) IExecutionFinishedListener(org.apache.hop.pipeline.IExecutionFinishedListener) IExtensionData(org.apache.hop.core.IExtensionData) HopException(org.apache.hop.core.exception.HopException) Database(org.apache.hop.core.database.Database) ArrayList(java.util.ArrayList) List(java.util.List) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) IPipelineEngine(org.apache.hop.pipeline.engine.IPipelineEngine) Result(org.apache.hop.core.Result)

Example 2 with IExtensionData

use of org.apache.hop.core.IExtensionData in project hop by apache.

the class DatabaseCreatedXp method callExtensionPoint.

@Override
public void callExtensionPoint(ILogChannel log, IVariables variables, Database database) throws HopException {
    // 
    if (!(database.getParentObject() instanceof IExtensionData)) {
        return;
    }
    IExtensionData extensionData = (IExtensionData) database.getParentObject();
    String connectionGroup = (String) extensionData.getExtensionDataMap().get(Const.CONNECTION_GROUP);
    if (connectionGroup != null) {
        database.setConnectionGroup(connectionGroup);
    }
}
Also used : IExtensionData(org.apache.hop.core.IExtensionData)

Example 3 with IExtensionData

use of org.apache.hop.core.IExtensionData in project hop by apache.

the class LocalWorkflowEngine method startExecution.

@Override
public Result startExecution() {
    if (!(workflowRunConfiguration.getEngineRunConfiguration() instanceof LocalWorkflowRunConfiguration)) {
        log.logError("Error starting workflow", new HopException("A local workflow execution expects a local workflow configuration, not an instance of class " + workflowRunConfiguration.getEngineRunConfiguration().getClass().getName()));
        result = new Result();
        result.setNrErrors(1L);
        return result;
    }
    LocalWorkflowRunConfiguration config = (LocalWorkflowRunConfiguration) workflowRunConfiguration.getEngineRunConfiguration();
    // See if we need to enable transactions...
    // 
    IExtensionData parentExtensionData = getParentPipeline();
    if (parentExtensionData == null) {
        parentExtensionData = getParentWorkflow();
    }
    String connectionGroup = null;
    if (parentExtensionData != null && parentExtensionData.getExtensionDataMap() != null) {
        connectionGroup = (String) parentExtensionData.getExtensionDataMap().get(Const.CONNECTION_GROUP);
    }
    // 
    if (config.isTransactional() && connectionGroup == null) {
        // Store a value in the parent...
        // 
        connectionGroup = getWorkflowMeta().getName() + " - " + UUID.randomUUID();
        // We also need to commit/rollback at the end of this workflow...
        // 
        addWorkflowFinishedListener(workflow -> {
            String group = (String) workflow.getExtensionDataMap().get(Const.CONNECTION_GROUP);
            List<Database> databases = DatabaseConnectionMap.getInstance().getDatabases(group);
            Result result = workflow.getResult();
            for (Database database : databases) {
                // 
                try {
                    if (result.getResult() && !result.isStopped() && result.getNrErrors() == 0) {
                        try {
                            database.commit(true);
                            workflow.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were committed at the end of the workflow!");
                        } catch (HopDatabaseException e) {
                            workflow.getLogChannel().logError("Error committing database connection " + database.getDatabaseMeta().getName(), e);
                            result.setNrErrors(result.getNrErrors() + 1);
                        }
                    } else {
                        // Error? Rollback!
                        try {
                            database.rollback(true);
                            workflow.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were rolled back at the end of the workflow!");
                        } catch (HopDatabaseException e) {
                            workflow.getLogChannel().logError("Error rolling back database connection " + database.getDatabaseMeta().getName(), e);
                            result.setNrErrors(result.getNrErrors() + 1);
                        }
                    }
                } finally {
                    // Always close connection!
                    try {
                        database.closeConnectionOnly();
                        workflow.getLogChannel().logDebug("Database connection '" + database.getDatabaseMeta().getName() + "' closed successfully!");
                    } catch (HopDatabaseException hde) {
                        workflow.getLogChannel().logError("Error disconnecting from database - closeConnectionOnly failed:" + Const.CR + hde.getMessage());
                        workflow.getLogChannel().logError(Const.getStackTracker(hde));
                    }
                    // Definitely remove the connection reference the connections map
                    DatabaseConnectionMap.getInstance().removeConnection(group, null, database);
                }
            }
        });
    }
    // 
    if (connectionGroup != null && getExtensionDataMap() != null) {
        // Set the connection group for this workflow
        // 
        getExtensionDataMap().put(Const.CONNECTION_GROUP, connectionGroup);
    }
    // Pass down the value of the connection group value to actions before they're executed...
    // 
    addActionListener(new IActionListener() {

        @Override
        public void beforeExecution(IWorkflowEngine workflow, ActionMeta actionMeta, IAction action) {
            String connectionGroup = (String) workflow.getExtensionDataMap().get(Const.CONNECTION_GROUP);
            if (connectionGroup != null) {
                action.getExtensionDataMap().put(Const.CONNECTION_GROUP, connectionGroup);
            }
        }

        @Override
        public void afterExecution(IWorkflowEngine workflow, ActionMeta actionMeta, IAction action, Result result) {
        // Nothing
        }
    });
    return super.startExecution();
}
Also used : IActionListener(org.apache.hop.workflow.IActionListener) IExtensionData(org.apache.hop.core.IExtensionData) IAction(org.apache.hop.workflow.action.IAction) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) Result(org.apache.hop.core.Result) IWorkflowEngine(org.apache.hop.workflow.engine.IWorkflowEngine) ActionMeta(org.apache.hop.workflow.action.ActionMeta) Database(org.apache.hop.core.database.Database)

Aggregations

IExtensionData (org.apache.hop.core.IExtensionData)3 Result (org.apache.hop.core.Result)2 Database (org.apache.hop.core.database.Database)2 HopDatabaseException (org.apache.hop.core.exception.HopDatabaseException)2 HopException (org.apache.hop.core.exception.HopException)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 Const (org.apache.hop.core.Const)1 DatabaseConnectionMap (org.apache.hop.core.database.map.DatabaseConnectionMap)1 ILoggingObject (org.apache.hop.core.logging.ILoggingObject)1 INamedParameters (org.apache.hop.core.parameters.INamedParameters)1 IVariables (org.apache.hop.core.variables.IVariables)1 IHopMetadataProvider (org.apache.hop.metadata.api.IHopMetadataProvider)1 IExecutionFinishedListener (org.apache.hop.pipeline.IExecutionFinishedListener)1 Pipeline (org.apache.hop.pipeline.Pipeline)1 PipelineMeta (org.apache.hop.pipeline.PipelineMeta)1 IPipelineEngineRunConfiguration (org.apache.hop.pipeline.config.IPipelineEngineRunConfiguration)1 PipelineRunConfiguration (org.apache.hop.pipeline.config.PipelineRunConfiguration)1 IPipelineEngine (org.apache.hop.pipeline.engine.IPipelineEngine)1