Search in sources :

Example 1 with CommandCallback

use of org.ovirt.engine.core.bll.tasks.interfaces.CommandCallback in project ovirt-engine by oVirt.

the class CommandCallbacksPoller method invokeCallbackMethodsImpl.

private void invokeCallbackMethodsImpl() {
    Iterator<Entry<Guid, CallbackTiming>> iterator = commandsRepository.getCallbacksTiming().entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<Guid, CallbackTiming> entry = iterator.next();
        Guid cmdId = entry.getKey();
        CallbackTiming callbackTiming = entry.getValue();
        CommandEntity commandEntity = commandsRepository.getCommandEntity(cmdId);
        CorrelationIdTracker.setCorrelationId(commandEntity != null ? commandEntity.getCommandParameters().getCorrelationId() : null);
        if (commandEntity != null && updateCommandWaitingForEvent(commandEntity, callbackTiming)) {
            continue;
        }
        // Decrement counter; execute if it reaches 0
        callbackTiming.setRemainingDelay(callbackTiming.getRemainingDelay() - pollingRate);
        if (callbackTiming.getRemainingDelay() > 0) {
            continue;
        }
        CommandCallback callback = callbackTiming.getCallback();
        CommandStatus status = commandsRepository.getCommandStatus(cmdId);
        boolean runCallbackAgain = false;
        boolean errorInCallback = false;
        try {
            switch(status) {
                case FAILED:
                case SUCCEEDED:
                    runCallbackAgain = endCallback(cmdId, callback, status);
                    break;
                case ACTIVE:
                    if (commandEntity != null && commandEntity.isExecuted()) {
                        callback.doPolling(cmdId, getChildCommandIds(cmdId));
                    }
                    break;
                case EXECUTION_FAILED:
                    if (callback.pollOnExecutionFailed()) {
                        callback.doPolling(cmdId, getChildCommandIds(cmdId));
                    }
                    break;
                default:
                    break;
            }
        } catch (Exception ex) {
            errorInCallback = true;
            handleError(ex, status, cmdId);
        } finally {
            if ((CommandStatus.FAILED == status || (CommandStatus.SUCCEEDED == status && !errorInCallback)) && !runCallbackAgain) {
                commandsRepository.updateCallbackNotified(cmdId);
                iterator.remove();
                CommandEntity cmdEntity = commandsRepository.getCommandEntity(entry.getKey());
                if (cmdEntity != null) {
                    // When a child finishes, its parent's callback should execute shortly thereafter
                    CallbackTiming rootCmdContainer = commandsRepository.getCallbackTiming(cmdEntity.getRootCommandId());
                    if (rootCmdContainer != null) {
                        rootCmdContainer.setInitialDelay(pollingRate);
                        rootCmdContainer.setRemainingDelay(pollingRate);
                    }
                }
            } else if (status != commandsRepository.getCommandStatus(cmdId)) {
                callbackTiming.setInitialDelay(pollingRate);
                callbackTiming.setRemainingDelay(pollingRate);
            } else {
                long maxDelay = Config.<Long>getValue(ConfigValues.AsyncCommandPollingRateInSeconds);
                callbackTiming.setInitialDelay(Math.min(maxDelay, callbackTiming.getInitialDelay() * 2));
                callbackTiming.setRemainingDelay(callbackTiming.getInitialDelay());
            }
        }
    }
    CorrelationIdTracker.setCorrelationId(null);
    commandsRepository.markExpiredCommandsAsFailure();
}
Also used : Entry(java.util.Map.Entry) CommandEntity(org.ovirt.engine.core.common.businessentities.CommandEntity) CommandStatus(org.ovirt.engine.core.compat.CommandStatus) Guid(org.ovirt.engine.core.compat.Guid) CommandCallback(org.ovirt.engine.core.bll.tasks.interfaces.CommandCallback)

Example 2 with CommandCallback

use of org.ovirt.engine.core.bll.tasks.interfaces.CommandCallback in project ovirt-engine by oVirt.

the class CommandCoordinatorImpl method executeAsyncCommand.

/**
 * Executes the action using a Thread Pool. Used when the calling function
 * would like the execute the command with no delay
 */
@Override
public Future<ActionReturnValue> executeAsyncCommand(ActionType actionType, ActionParametersBase parameters, CommandContext cmdContext) {
    final CommandBase<?> command = CommandsFactory.createCommand(actionType, parameters, cmdContext);
    CommandCallback callBack = command.getCallback();
    command.persistCommand(command.getParameters().getParentCommand(), cmdContext, callBack != null, false);
    if (callBack != null) {
        commandsRepositoryInstance.get().addToCallbackMap(command.getCommandId(), new CallbackTiming(callBack, Config.<Long>getValue(ConfigValues.AsyncCommandPollingLoopInSeconds)));
    }
    return commandExecutorInstance.get().executeAsyncCommand(command, cmdContext);
}
Also used : CommandCallback(org.ovirt.engine.core.bll.tasks.interfaces.CommandCallback)

Aggregations

CommandCallback (org.ovirt.engine.core.bll.tasks.interfaces.CommandCallback)2 Entry (java.util.Map.Entry)1 CommandEntity (org.ovirt.engine.core.common.businessentities.CommandEntity)1 CommandStatus (org.ovirt.engine.core.compat.CommandStatus)1 Guid (org.ovirt.engine.core.compat.Guid)1