Search in sources :

Example 1 with GearmanJobServerConnection

use of org.gearman.common.GearmanJobServerConnection in project sakuli by ConSol.

the class GearmanResultServiceImpl method tearDown.

@Override
public void tearDown(Optional<AbstractTestDataEntity> dataEntity, boolean asyncCall) {
    dataEntity.ifPresent(data -> {
        logger.info("======= SEND RESULTS TO GEARMAN SERVER ======");
        GearmanClient gearmanClient = getGearmanClient();
        GearmanJobServerConnection connection = getGearmanConnection(properties.getServerHost(), properties.getServerPort());
        List<NagiosCheckResult> results = new ArrayList<>();
        try {
            results.add(nagiosCheckResultBuilder.build(data));
            if (properties.isCacheEnabled()) {
                results.addAll(cacheService.getCachedResults());
                if (results.size() > 1) {
                    logger.info(String.format("Processing %s cached results first", results.size() - 1));
                }
            }
            if (!gearmanClient.addJobServer(connection)) {
                throw new SakuliForwarderCheckedException(String.format("Failed to connect to Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort()));
            } else {
                // sending in reverse original happened order
                Collections.reverse(results);
                results = results.stream().filter(checkResult -> !sendResult(gearmanClient, checkResult, asyncCall, data)).collect(Collectors.toList());
                Collections.reverse(results);
            }
        } catch (Exception e) {
            handleTeardownException((e instanceof SakuliForwarderException) ? e : new SakuliForwarderRuntimeException(String.format("Could not transfer Sakuli results to the Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort()), e), asyncCall, data);
        }
        // save all not send results
        if (properties.isCacheEnabled()) {
            try {
                cacheService.cacheResults(results);
            } catch (SakuliForwarderCheckedException e) {
                handleTeardownException(e, asyncCall, data);
            }
        }
        gearmanClient.shutdown();
        logger.info("======= FINISHED: SEND RESULTS TO GEARMAN SERVER ======");
    });
}
Also used : GearmanJobServerConnection(org.gearman.common.GearmanJobServerConnection) SakuliForwarderRuntimeException(org.sakuli.exceptions.SakuliForwarderRuntimeException) ArrayList(java.util.ArrayList) NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) SakuliForwarderRuntimeException(org.sakuli.exceptions.SakuliForwarderRuntimeException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException)

Example 2 with GearmanJobServerConnection

use of org.gearman.common.GearmanJobServerConnection in project sakuli by ConSol.

the class GearmanResultServiceImpl method saveAllResults.

@Override
public void saveAllResults() {
    logger.info("======= SEND RESULTS TO GEARMAN SERVER ======");
    GearmanClient gearmanClient = getGearmanClient();
    GearmanJobServerConnection connection = getGearmanConnection(properties.getServerHost(), properties.getServerPort());
    List<NagiosCheckResult> results = new ArrayList<>();
    results.add(nagiosCheckResultBuilder.build());
    if (properties.isCacheEnabled()) {
        results.addAll(cacheService.getCachedResults());
        if (results.size() > 1) {
            logger.info(String.format("Processing %s cached results first", results.size() - 1));
        }
    }
    try {
        if (!gearmanClient.addJobServer(connection)) {
            exceptionHandler.handleException(new SakuliForwarderException(String.format("Failed to connect to Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort())), true);
        } else {
            //sending in reverse original happened order
            Collections.reverse(results);
            results = results.stream().filter(checkResult -> !sendResult(gearmanClient, checkResult)).collect(Collectors.toList());
            Collections.reverse(results);
        }
    } catch (Exception e) {
        exceptionHandler.handleException(new SakuliForwarderException(e, String.format("Could not transfer Sakuli results to the Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort())), true);
    }
    //save all not send results
    if (properties.isCacheEnabled()) {
        cacheService.cacheResults(results);
    }
    gearmanClient.shutdown();
    logger.info("======= FINISHED: SEND RESULTS TO GEARMAN SERVER ======");
}
Also used : GearmanJobServerConnection(org.gearman.common.GearmanJobServerConnection) ArrayList(java.util.ArrayList) NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException)

Aggregations

ArrayList (java.util.ArrayList)2 GearmanJobServerConnection (org.gearman.common.GearmanJobServerConnection)2 SakuliForwarderException (org.sakuli.exceptions.SakuliForwarderException)2 NagiosCheckResult (org.sakuli.services.forwarder.gearman.model.NagiosCheckResult)2 SakuliForwarderCheckedException (org.sakuli.exceptions.SakuliForwarderCheckedException)1 SakuliForwarderRuntimeException (org.sakuli.exceptions.SakuliForwarderRuntimeException)1