Search in sources :

Example 1 with ShutdownCommand

use of com.cloud.legacymodel.communication.command.ShutdownCommand in project cosmic by MissionCriticalCloud.

the class Agent method processRequest.

protected void processRequest(final Request request, final Link link) {
    boolean requestLogged = false;
    Response response = null;
    try {
        final Command[] cmds = request.getCommands();
        final Answer[] answers = new Answer[cmds.length];
        for (int i = 0; i < cmds.length; i++) {
            final Command cmd = cmds[i];
            Answer answer;
            if (cmd.getContextParam("logid") != null) {
                MDC.put("logcontextid", cmd.getContextParam("logid"));
            }
            if (// ensures request is logged only once per method call
            !requestLogged) {
                final String requestMsg = request.toString();
                if (requestMsg != null) {
                    logger.debug("Request:" + requestMsg);
                }
                requestLogged = true;
            }
            logger.debug("Processing command: " + cmd.toString());
            if (cmd instanceof CronCommand) {
                final CronCommand watch = (CronCommand) cmd;
                scheduleWatch(link, request, (long) watch.getInterval() * 1000, watch.getInterval() * 1000);
                answer = new Answer(cmd, true, null);
            } else if (cmd instanceof ShutdownCommand) {
                final ShutdownCommand shutdown = (ShutdownCommand) cmd;
                logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
                cancelTasks();
                this._reconnectAllowed = false;
                answer = new Answer(cmd, true, null);
            } else if (cmd instanceof ReadyCommand && ((ReadyCommand) cmd).getDetails() != null) {
                logger.debug("Not ready to connect to mgt server: " + ((ReadyCommand) cmd).getDetails());
                System.exit(1);
                return;
            } else if (cmd instanceof MaintainCommand) {
                logger.debug("Received maintainCommand");
                cancelTasks();
                this._reconnectAllowed = false;
                answer = new MaintainAnswer((MaintainCommand) cmd);
            } else if (cmd instanceof AgentControlCommand) {
                answer = null;
                synchronized (this._controlListeners) {
                    for (final IAgentControlListener listener : this._controlListeners) {
                        answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
                        if (answer != null) {
                            break;
                        }
                    }
                }
                if (answer == null) {
                    logger.warn("No handler found to process cmd: " + cmd.toString());
                    answer = new AgentControlAnswer(cmd);
                }
            } else {
                if (cmd instanceof ReadyCommand) {
                    processReadyCommand(cmd);
                }
                this._inProgress.incrementAndGet();
                try {
                    answer = this.resource.executeRequest(cmd);
                } finally {
                    this._inProgress.decrementAndGet();
                }
                if (answer == null) {
                    logger.debug("Response: unsupported command" + cmd.toString());
                    answer = Answer.createUnsupportedCommandAnswer(cmd);
                }
            }
            answers[i] = answer;
            if (!answer.getResult() && request.stopOnError()) {
                for (i++; i < cmds.length; i++) {
                    answers[i] = new Answer(cmds[i], false, "Stopped by previous failure");
                }
                break;
            }
        }
        response = new Response(request, answers);
    } catch (final RuntimeException e) {
        logger.error("Error while handling request: " + e.getMessage());
        logger.error(ExceptionUtils.getRootCauseMessage(e));
    } finally {
        if (logger.isDebugEnabled()) {
            final String responseMsg = response.toString();
            if (responseMsg != null) {
                logger.debug(response.toString());
            }
        }
        if (response != null) {
            try {
                link.send(response.toBytes());
            } catch (final ClosedChannelException e) {
                logger.warn("Unable to send response: " + response.toString());
            }
        }
    }
}
Also used : MaintainCommand(com.cloud.legacymodel.communication.command.MaintainCommand) ClosedChannelException(java.nio.channels.ClosedChannelException) MaintainAnswer(com.cloud.legacymodel.communication.answer.MaintainAnswer) CronCommand(com.cloud.legacymodel.communication.command.CronCommand) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) Response(com.cloud.common.transport.Response) MaintainAnswer(com.cloud.legacymodel.communication.answer.MaintainAnswer) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) StartupAnswer(com.cloud.legacymodel.communication.answer.StartupAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) MaintainCommand(com.cloud.legacymodel.communication.command.MaintainCommand) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) CronCommand(com.cloud.legacymodel.communication.command.CronCommand) PingCommand(com.cloud.legacymodel.communication.command.PingCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) Command(com.cloud.legacymodel.communication.command.Command) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) IAgentControlListener(com.cloud.common.agent.IAgentControlListener)

Example 2 with ShutdownCommand

use of com.cloud.legacymodel.communication.command.ShutdownCommand in project cosmic by MissionCriticalCloud.

the class Agent method stop.

public void stop(final String reason) {
    logger.info("Stopping the agent due to: {}", reason);
    if (this._connection != null) {
        final ShutdownCommand cmd = new ShutdownCommand(reason);
        try {
            if (this._link != null) {
                final Request req = new Request(this._id != null ? this._id : -1, -1, cmd, false);
                this._link.send(req.toBytes());
            }
        } catch (final ClosedChannelException e) {
            logger.warn("Unable to send: " + cmd.toString());
        } catch (final Exception e) {
            logger.warn("Unable to send: " + cmd.toString() + " due to exception: ", e);
        }
        logger.debug("Sending shutdown to management server");
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException e) {
            logger.debug("Who the heck interrupted me here?");
        }
        this._connection.stop();
        this._connection = null;
    }
    if (this.resource != null) {
        this.resource.stop();
    }
    this._urgentTaskPool.shutdownNow();
    synchronized (this) {
        this.notifyAll();
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) Request(com.cloud.common.transport.Request) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) AgentControlChannelException(com.cloud.legacymodel.exceptions.AgentControlChannelException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) NioConnectionException(com.cloud.legacymodel.exceptions.NioConnectionException)

Example 3 with ShutdownCommand

use of com.cloud.legacymodel.communication.command.ShutdownCommand in project cosmic by MissionCriticalCloud.

the class LibvirtServerDiscoverer method deleteHost.

@Override
public DeleteHostAnswer deleteHost(final HostVO host, final boolean isForced, final boolean isForceDeleteStorage) throws UnableDeleteHostException {
    if (host.getType() != HostType.Routing || host.getHypervisorType() != HypervisorType.KVM) {
        return null;
    }
    this._resourceMgr.deleteRoutingHost(host, isForced, isForceDeleteStorage);
    try {
        final ShutdownCommand cmd = new ShutdownCommand(ShutdownCommand.DeleteHost, null);
        this._agentMgr.send(host.getId(), cmd);
    } catch (final AgentUnavailableException e) {
        s_logger.warn("Sending ShutdownCommand failed: ", e);
    } catch (final OperationTimedoutException e) {
        s_logger.warn("Sending ShutdownCommand failed: ", e);
    }
    return new DeleteHostAnswer(true);
}
Also used : OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand)

Aggregations

ShutdownCommand (com.cloud.legacymodel.communication.command.ShutdownCommand)3 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 IAgentControlListener (com.cloud.common.agent.IAgentControlListener)1 Request (com.cloud.common.transport.Request)1 Response (com.cloud.common.transport.Response)1 AgentControlAnswer (com.cloud.legacymodel.communication.answer.AgentControlAnswer)1 Answer (com.cloud.legacymodel.communication.answer.Answer)1 MaintainAnswer (com.cloud.legacymodel.communication.answer.MaintainAnswer)1 StartupAnswer (com.cloud.legacymodel.communication.answer.StartupAnswer)1 Command (com.cloud.legacymodel.communication.command.Command)1 CronCommand (com.cloud.legacymodel.communication.command.CronCommand)1 MaintainCommand (com.cloud.legacymodel.communication.command.MaintainCommand)1 PingCommand (com.cloud.legacymodel.communication.command.PingCommand)1 ReadyCommand (com.cloud.legacymodel.communication.command.ReadyCommand)1 AgentControlCommand (com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand)1 StartupCommand (com.cloud.legacymodel.communication.command.startup.StartupCommand)1 AgentControlChannelException (com.cloud.legacymodel.exceptions.AgentControlChannelException)1 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)1 NioConnectionException (com.cloud.legacymodel.exceptions.NioConnectionException)1