Search in sources :

Example 1 with Result

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

the class HopServer method monitorRemotePipeline.

/**
 * Monitors a remote pipeline at the specified interval.
 *
 * @param log              the log channel interface
 * @param serverObjectId   the HopServer object ID
 * @param pipelineName     the pipeline name
 * @param sleepTimeSeconds the sleep time (in seconds)
 */
public void monitorRemotePipeline(IVariables variables, ILogChannel log, String serverObjectId, String pipelineName, int sleepTimeSeconds) {
    long errors = 0;
    boolean allFinished = false;
    while (!allFinished && errors == 0) {
        allFinished = true;
        errors = 0L;
        // Check the remote server
        if (allFinished && errors == 0) {
            try {
                HopServerPipelineStatus pipelineStatus = getPipelineStatus(variables, pipelineName, serverObjectId, 0);
                if (pipelineStatus.isRunning()) {
                    if (log.isDetailed()) {
                        log.logDetailed(pipelineName, "Remote pipeline is still running.");
                    }
                    allFinished = false;
                } else {
                    if (log.isDetailed()) {
                        log.logDetailed(pipelineName, "Remote pipeline has finished.");
                    }
                }
                Result result = pipelineStatus.getResult();
                errors += result.getNrErrors();
            } catch (Exception e) {
                errors += 1;
                log.logError(pipelineName, "Unable to contact remote hop server '" + this.getName() + "' to check pipeline status : " + e.toString());
            }
        }
        // 
        if (!allFinished) {
            // Not finished or error: wait a bit longer
            if (log.isDetailed()) {
                log.logDetailed(pipelineName, "The remote pipeline is still running, waiting a few seconds...");
            }
            try {
                Thread.sleep(sleepTimeSeconds * 1000);
            } catch (Exception e) {
            // Ignore errors
            }
        }
    }
    log.logBasic(pipelineName, "The remote pipeline has finished.");
}
Also used : ClientProtocolException(org.apache.http.client.ClientProtocolException) HopException(org.apache.hop.core.exception.HopException) Result(org.apache.hop.core.Result)

Example 2 with Result

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

the class BaseFileInputTransform method init.

/**
 * Initialize transform before execute.
 */
@Override
public boolean init() {
    initErrorHandling();
    meta.additionalOutputFields.normalize();
    data.files = meta.getFileInputList(this);
    data.currentFileIndex = 0;
    // If there are missing files,
    // fail if we don't ignore errors
    // 
    Result previousResult = getPipeline().getPreviousResult();
    Map<String, ResultFile> resultFiles = (previousResult != null) ? previousResult.getResultFiles() : null;
    if ((previousResult == null || resultFiles == null || resultFiles.size() == 0) && data.files.nrOfMissingFiles() > 0 && !meta.inputFiles.acceptingFilenames && !meta.errorHandling.errorIgnored) {
        logError(BaseMessages.getString(PKG, "BaseFileInputTransform.Log.Error.NoFilesSpecified"));
        return false;
    }
    return super.init();
}
Also used : ResultFile(org.apache.hop.core.ResultFile) Result(org.apache.hop.core.Result)

Example 3 with Result

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

the class ActionFtpDelete method execute.

/**
 * Needed for the Vector coming from sshclient.ls() *
 */
@Override
@SuppressWarnings("unchecked")
public Result execute(Result previousResult, int nr) {
    log.logBasic(BaseMessages.getString(PKG, "ActionFTPDelete.Started", serverName));
    RowMetaAndData resultRow = null;
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    result.setResult(false);
    nrErrors = 0;
    nrFilesDeleted = 0;
    successConditionBroken = false;
    HashSet<String> listPreviousFiles = new HashSet<>();
    // Here let's put some controls before stating the workflow
    String realServerName = resolve(serverName);
    String realServerPassword = Utils.resolvePassword(this, password);
    String realFtpDirectory = resolve(remoteDirectory);
    int realServerPort = Const.toInt(resolve(serverPort), 0);
    String realUsername = resolve(userName);
    String realPassword = Utils.resolvePassword(this, password);
    String realProxyHost = resolve(proxyHost);
    String realProxyUsername = resolve(proxyUsername);
    String realProxyPassword = Utils.resolvePassword(this, proxyPassword);
    int realProxyPort = Const.toInt(resolve(proxyPort), 0);
    String realKeyFilename = resolve(keyFilename);
    String realKeyPass = resolve(keyFilePass);
    // The following is used to apply a path for SSH because the SFTPv3Client doesn't let us
    // specify/change dirs
    // 
    String sourceFolder = "";
    if (isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "ActionFTPDelete.Start"));
    }
    if (copyPrevious && rows.size() == 0) {
        if (isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "ActionFTPDelete.ArgsFromPreviousNothing"));
        }
        result.setResult(true);
        return result;
    }
    try {
        // Get all the files in the current directory...
        String[] filelist = null;
        if (protocol.equals(PROTOCOL_FTP)) {
            // establish the connection
            ftpConnect(realFtpDirectory);
            filelist = ftpclient.listNames();
            // CHECK THIS !!!
            if (filelist.length == 1) {
                String translatedWildcard = resolve(wildcard);
                if (!Utils.isEmpty(translatedWildcard)) {
                    if (filelist[0].startsWith(translatedWildcard)) {
                        throw new HopException(filelist[0]);
                    }
                }
            }
        } else if (protocol.equals(PROTOCOL_SFTP)) {
            // establish the secure connection
            sftpConnect(realServerName, realUsername, realServerPort, realPassword, realFtpDirectory);
            // Get all the files in the current directory...
            filelist = sftpclient.dir();
        }
        if (isDetailed()) {
            logDetailed("ActionFTPDelete.FoundNFiles", String.valueOf(filelist.length));
        }
        int found = filelist == null ? 0 : filelist.length;
        if (found == 0) {
            result.setResult(true);
            return result;
        }
        Pattern pattern = null;
        if (copyPrevious) {
            // Copy the input row to the (command line) arguments
            for (int iteration = 0; iteration < rows.size(); iteration++) {
                resultRow = rows.get(iteration);
                // Get file names
                String filePrevious = resultRow.getString(0, null);
                if (!Utils.isEmpty(filePrevious)) {
                    listPreviousFiles.add(filePrevious);
                }
            }
        } else {
            if (!Utils.isEmpty(wildcard)) {
                String realWildcard = resolve(wildcard);
                pattern = Pattern.compile(realWildcard);
            }
        }
        if (!getSuccessCondition().equals(SUCCESS_IF_ALL_FILES_DOWNLOADED)) {
            limitFiles = Const.toInt(resolve(getLimitSuccess()), 10);
        }
        // Get the files in the list...
        for (int i = 0; i < filelist.length && !parentWorkflow.isStopped(); i++) {
            if (successConditionBroken) {
                throw new Exception(BaseMessages.getString(PKG, "ActionFTPDelete.SuccesConditionBroken"));
            }
            boolean getIt = false;
            if (isDebug()) {
                logDebug(BaseMessages.getString(PKG, "ActionFTPDelete.AnalysingFile", filelist[i]));
            }
            try {
                // First see if the file matches the regular expression!
                if (copyPrevious) {
                    if (listPreviousFiles.contains(filelist[i])) {
                        getIt = true;
                    }
                } else {
                    if (pattern != null) {
                        Matcher matcher = pattern.matcher(filelist[i]);
                        getIt = matcher.matches();
                    }
                }
                if (getIt) {
                    // Delete file
                    if (protocol.equals(PROTOCOL_FTP)) {
                        ftpclient.deleteFile(filelist[i]);
                    } else if (protocol.equals(PROTOCOL_SFTP)) {
                        sftpclient.delete(filelist[i]);
                    }
                    if (isDetailed()) {
                        logDetailed("ActionFTPDelete.RemotefileDeleted", filelist[i]);
                    }
                    updateDeletedFiles();
                }
            } catch (Exception e) {
                // Update errors number
                updateErrors();
                logError(BaseMessages.getString(PKG, "ActionFtp.UnexpectedError", e.getMessage()));
                if (successConditionBroken) {
                    throw new Exception(BaseMessages.getString(PKG, "ActionFTPDelete.SuccesConditionBroken"));
                }
            }
        }
    // end for
    } catch (Exception e) {
        updateErrors();
        logError(BaseMessages.getString(PKG, "ActionFTPDelete.ErrorGetting", e.getMessage()));
        logError(Const.getStackTracker(e));
    } finally {
        if (ftpclient != null && ftpclient.isConnected()) {
            try {
                ftpclient.quit();
                ftpclient = null;
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "ActionFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        if (sftpclient != null) {
            try {
                sftpclient.disconnect();
                sftpclient = null;
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "ActionFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        FtpClientUtil.clearSocksJvmSettings();
    }
    result.setResult(!successConditionBroken);
    result.setNrFilesRetrieved(nrFilesDeleted);
    result.setNrErrors(nrErrors);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) HopException(org.apache.hop.core.exception.HopException) Matcher(java.util.regex.Matcher) HopException(org.apache.hop.core.exception.HopException) HopXmlException(org.apache.hop.core.exception.HopXmlException) ICheckResult(org.apache.hop.core.ICheckResult) Result(org.apache.hop.core.Result) RowMetaAndData(org.apache.hop.core.RowMetaAndData) HashSet(java.util.HashSet)

Example 4 with Result

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

the class WorkflowActionEvalTableContentTest method testNrErrorsFailureOldBehavior.

@Test
public void testNrErrorsFailureOldBehavior() throws Exception {
    action.setLimit("1");
    action.setSuccessCondition(ActionEvalTableContent.SUCCESS_CONDITION_ROWS_COUNT_EQUAL);
    action.setTablename("table");
    action.setVariable(Const.HOP_COMPATIBILITY_SET_ERROR_ON_SPECIFIC_WORKFLOW_ACTIONS, "Y");
    Result res = action.execute(new Result(), 0);
    assertFalse("Eval number of rows should fail", res.getResult());
    assertEquals("An error should be reported in result object accoding to the old behavior", res.getNrErrors(), 1);
}
Also used : Result(org.apache.hop.core.Result)

Example 5 with Result

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

the class WorkflowActionEvalTableContentTest method testNrErrorsSuccess.

@Test
public void testNrErrorsSuccess() throws Exception {
    action.setLimit("5");
    action.setSuccessCondition(ActionEvalTableContent.SUCCESS_CONDITION_ROWS_COUNT_EQUAL);
    action.setTablename("table");
    Result res = action.execute(new Result(), 0);
    assertTrue("Eval number of rows should be suceeded", res.getResult());
    assertEquals("Apparently there should no error", res.getNrErrors(), 0);
    // that should work regardless of old/new behavior flag
    action.setVariable(Const.HOP_COMPATIBILITY_SET_ERROR_ON_SPECIFIC_WORKFLOW_ACTIONS, "Y");
    res = action.execute(new Result(), 0);
    assertTrue("Eval number of rows should be suceeded", res.getResult());
    assertEquals("Apparently there should no error", res.getNrErrors(), 0);
}
Also used : Result(org.apache.hop.core.Result)

Aggregations

Result (org.apache.hop.core.Result)108 ICheckResult (org.apache.hop.core.ICheckResult)36 HopXmlException (org.apache.hop.core.exception.HopXmlException)35 HopException (org.apache.hop.core.exception.HopException)34 Test (org.junit.Test)17 FileObject (org.apache.commons.vfs2.FileObject)16 RowMetaAndData (org.apache.hop.core.RowMetaAndData)14 IOException (java.io.IOException)12 ResultFile (org.apache.hop.core.ResultFile)11 Database (org.apache.hop.core.database.Database)11 HopDatabaseException (org.apache.hop.core.exception.HopDatabaseException)10 ValueMetaString (org.apache.hop.core.row.value.ValueMetaString)9 Workflow (org.apache.hop.workflow.Workflow)9 File (java.io.File)7 ArrayList (java.util.ArrayList)7 IVariables (org.apache.hop.core.variables.IVariables)7 IHopMetadataProvider (org.apache.hop.metadata.api.IHopMetadataProvider)7 SimpleDateFormat (java.text.SimpleDateFormat)6 Date (java.util.Date)6 PipelineMeta (org.apache.hop.pipeline.PipelineMeta)6