From: <pn...@hy...> - 2010-04-09 03:41:29
|
Author: pnguyen Date: 2010-04-08 20:41:19 -0700 (Thu, 08 Apr 2010) New Revision: 14488 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14488 Modified: trunk/src/org/hyperic/hq/control/server/session/ControlJob.java trunk/src/org/hyperic/hq/control/server/session/ControlScheduleManagerEJBImpl.java Log: [HQ-2080] Control action for a resource (non-group) intended for immediate execution no longer use the Quartz scheduler. Modified: trunk/src/org/hyperic/hq/control/server/session/ControlJob.java =================================================================== --- trunk/src/org/hyperic/hq/control/server/session/ControlJob.java 2010-04-08 19:15:40 UTC (rev 14487) +++ trunk/src/org/hyperic/hq/control/server/session/ControlJob.java 2010-04-09 03:41:19 UTC (rev 14488) @@ -6,7 +6,7 @@ * normal use of the program, and does *not* fall under the heading of * "derived work". * - * Copyright (C) [2004-2008], Hyperic, Inc. + * Copyright (C) [2004-2010], Hyperic, Inc. * This file is part of HQ. * * HQ is free software; you can redistribute it and/or modify @@ -27,24 +27,10 @@ import java.util.Date; -import javax.ejb.CreateException; -import javax.naming.NamingException; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hyperic.hq.agent.AgentConnectionException; -import org.hyperic.hq.agent.AgentRemoteException; -import org.hyperic.hq.appdef.shared.AgentNotFoundException; import org.hyperic.hq.appdef.shared.AppdefEntityID; -import org.hyperic.hq.appdef.shared.AppdefEntityNotFoundException; -import org.hyperic.hq.appdef.shared.PlatformManagerUtil; import org.hyperic.hq.authz.server.session.AuthzSubject; -import org.hyperic.hq.common.SystemException; -import org.hyperic.hq.control.agent.client.ControlCommandsClient; -import org.hyperic.hq.control.agent.client.ControlCommandsClientFactory; -import org.hyperic.hq.control.shared.ControlConstants; -import org.hyperic.hq.control.shared.ControlScheduleManagerLocal; -import org.hyperic.hq.control.shared.ControlScheduleManagerUtil; import org.hyperic.hq.product.PluginException; import org.hyperic.hq.scheduler.server.session.BaseJob; @@ -77,83 +63,10 @@ String args) throws PluginException { - long startTime = System.currentTimeMillis(); - ControlHistory commandHistory = null; - String errorMsg = null; - Integer groupId = (gid != null) ? gid.getId() : null; - - try { - ControlCommandsClient client = - ControlCommandsClientFactory.getInstance().getClient(id); - String pluginName = id.toString(); - String productName = PlatformManagerUtil.getLocalHome().create() - .getPlatformPluginName(id); - - ControlScheduleManagerLocal cLocal = - ControlScheduleManagerUtil.getLocalHome().create(); - - // Regular command - - commandHistory = - cLocal.createHistory(id, groupId, batchId, - subject.getName(), action, args, - scheduled, startTime, startTime, - dateScheduled.getTime(), - ControlConstants.STATUS_INPROGRESS, - description, null); - - client.controlPluginCommand(pluginName, - productName, - commandHistory.getId(), - action, args); - - } catch (AgentNotFoundException e) { - errorMsg = "Agent not found: " + e.getMessage(); - } catch (AgentConnectionException e) { - errorMsg = "Error getting agent connection: " + e.getMessage(); - } catch (AgentRemoteException e) { - errorMsg = "Agent error: " + e.getMessage(); - } catch (SystemException e) { - errorMsg = "System error"; - } catch (NamingException e) { - errorMsg = "System error"; - } catch (CreateException e) { - errorMsg = "System error"; - } catch (AppdefEntityNotFoundException e) { - errorMsg = "System error"; - } finally { - - if (errorMsg != null) { - this.log.error("Unable to execute command: " + errorMsg); - - // Add the failed job to the history - try { - AppdefEntityID aid = (gid != null) ? gid : id; - - ControlScheduleManagerLocal cLocal = - ControlScheduleManagerUtil.getLocalHome().create(); - - // Remove the old history - if (commandHistory != null) - cLocal.removeHistory(commandHistory.getId()); - - // Add the new one - cLocal.createHistory(aid, groupId, batchId, - subject.getName(), action, args, - scheduled, startTime, - System.currentTimeMillis(), - dateScheduled.getTime(), - ControlConstants.STATUS_FAILED, - description, errorMsg); - } catch (Exception exc) { - this.log.error("Unable to create history entry for " + - "failed control action"); - } - - throw new PluginException(errorMsg); - } - } - - return commandHistory.getId(); + return ControlScheduleManagerEJBImpl.getOne() + .doAgentControlCommand(id, gid, batchId, + subject, dateScheduled, + scheduled, description, + action, args); } } Modified: trunk/src/org/hyperic/hq/control/server/session/ControlScheduleManagerEJBImpl.java =================================================================== --- trunk/src/org/hyperic/hq/control/server/session/ControlScheduleManagerEJBImpl.java 2010-04-08 19:15:40 UTC (rev 14487) +++ trunk/src/org/hyperic/hq/control/server/session/ControlScheduleManagerEJBImpl.java 2010-04-09 03:41:19 UTC (rev 14488) @@ -6,7 +6,7 @@ * normal use of the program, and does *not* fall under the heading of * "derived work". * - * Copyright (C) [2004-2009], Hyperic, Inc. + * Copyright (C) [2004-2010], Hyperic, Inc. * This file is part of HQ. * * HQ is free software; you can redistribute it and/or modify @@ -45,7 +45,11 @@ import org.hibernate.ObjectNotFoundException; import org.hyperic.dao.DAOFactory; import org.hyperic.hibernate.Util; +import org.hyperic.hq.agent.AgentConnectionException; +import org.hyperic.hq.agent.AgentRemoteException; import org.hyperic.hq.appdef.server.session.AppdefManagerEJBImpl; +import org.hyperic.hq.appdef.server.session.PlatformManagerEJBImpl; +import org.hyperic.hq.appdef.shared.AgentNotFoundException; import org.hyperic.hq.appdef.shared.AppdefEntityID; import org.hyperic.hq.appdef.shared.AppdefEntityNotFoundException; import org.hyperic.hq.appdef.shared.AppdefEntityValue; @@ -56,6 +60,8 @@ import org.hyperic.hq.authz.shared.PermissionManagerFactory; import org.hyperic.hq.common.ApplicationException; import org.hyperic.hq.common.SystemException; +import org.hyperic.hq.control.agent.client.ControlCommandsClient; +import org.hyperic.hq.control.agent.client.ControlCommandsClientFactory; import org.hyperic.hq.control.shared.ControlConstants; import org.hyperic.hq.control.shared.ControlFrequencyValue; import org.hyperic.hq.control.shared.ControlScheduleManagerLocal; @@ -718,6 +724,116 @@ } /** + * Do a control command on a single appdef entity + * + * @return The control history id + * + * @ejb:interface-method + * @ejb:transaction type="Required" + */ + public Integer doAgentControlCommand(AppdefEntityID id, + AppdefEntityID gid, + Integer batchId, + AuthzSubject subject, + Date dateScheduled, + Boolean scheduled, + String description, + String action, + String args) + throws PluginException + { + final StopWatch watch = new StopWatch(); + final boolean debug = log.isDebugEnabled(); + + long startTime = System.currentTimeMillis(); + ControlHistory commandHistory = null; + String errorMsg = null; + Integer groupId = (gid != null) ? gid.getId() : null; + + try { + ControlCommandsClient client = + ControlCommandsClientFactory.getInstance().getClient(id); + String pluginName = id.toString(); + String productName = PlatformManagerEJBImpl.getOne() + .getPlatformPluginName(id); + + // Regular command + + if (debug) watch.markTimeBegin("createHistory"); + + commandHistory = + createHistory(id, groupId, batchId, + subject.getName(), action, args, + scheduled, startTime, startTime, + dateScheduled.getTime(), + ControlConstants.STATUS_INPROGRESS, + description, null); + + if (debug) { + watch.markTimeEnd("createHistory"); + watch.markTimeBegin("controlPluginCommand"); + } + + client.controlPluginCommand(pluginName, + productName, + commandHistory.getId(), + action, args); + + if (debug) watch.markTimeEnd("controlPluginCommand"); + + } catch (AgentNotFoundException e) { + errorMsg = "Agent not found: " + e.getMessage(); + } catch (AgentConnectionException e) { + errorMsg = "Error getting agent connection: " + e.getMessage(); + } catch (AgentRemoteException e) { + errorMsg = "Agent error: " + e.getMessage(); + } catch (SystemException e) { + errorMsg = "System error"; + } catch (AppdefEntityNotFoundException e) { + errorMsg = "System error"; + } finally { + if (debug) { + log.debug("doAgentControlCommand: " + watch + + " {resource=" + id + + ", action=" + action + + ", dateScheduled=" + dateScheduled + + ", error=" + errorMsg + + "}"); + } + + if (errorMsg != null) { + this.log.error("Unable to execute command: " + errorMsg); + + // Add the failed job to the history + try { + AppdefEntityID aid = (gid != null) ? gid : id; + + // Remove the old history + if (commandHistory != null) { + removeHistory(commandHistory.getId()); + } + + // Add the new one + createHistory(aid, groupId, batchId, + subject.getName(), action, args, + scheduled, startTime, + System.currentTimeMillis(), + dateScheduled.getTime(), + ControlConstants.STATUS_FAILED, + description, errorMsg); + } catch (Exception exc) { + this.log.error("Unable to create history entry for " + + "failed control action"); + } + + throw new PluginException(errorMsg); + } + } + + return commandHistory.getId(); + } + + /** * Execute a single action on an appdef entity * * @ejb:interface-method @@ -726,31 +842,50 @@ String action, String args, int order[]) throws PluginException { - // Even one time actions go through the scheduler, but there - // is no need to keep track of them in the ScheduleEntity + final boolean debug = log.isDebugEnabled(); - String jobName = getJobName(subject, id, action); + // HQ-2080: Control action for a resource (non-group) intended + // for immediate execution no longer use the Quartz scheduler. + // However, group control actions still need to go through the + // Quartz scheduler because we need to wait for each job to complete. + // There is no need to keep track of them in the ScheduleEntity. - // Setup the quartz job class that will handle this control action. - Class jobClass = id.isGroup() ? - ControlActionGroupJob.class : ControlActionJob.class; + if (id.isGroup()) { + String jobName = getJobName(subject, id, action); + + JobDetail jobDetail = new JobDetail(jobName, GROUP, + ControlActionGroupJob.class); + jobDetail.setVolatility(true); - JobDetail jobDetail = new JobDetail(jobName, GROUP, jobClass); - jobDetail.setVolatility(true); + setupJobData(jobDetail, subject, id, action, args, "Single execution", + Boolean.FALSE, null, order); - setupJobData(jobDetail, subject, id, action, args, "Single execution", - Boolean.FALSE, null, order); + // All single actions use cron triggers + SimpleTrigger trigger = + new SimpleTrigger(getTriggerName(subject, id, action), GROUP); + trigger.setVolatility(true); - // All single actions use cron triggers - SimpleTrigger trigger = - new SimpleTrigger(getTriggerName(subject, id, action), GROUP); - trigger.setVolatility(true); + try { + if (debug) log.debug("Scheduling job for immediate execution: " + jobDetail); + _scheduler.scheduleJob(jobDetail, trigger); + } catch (SchedulerException e) { + log.error("Unable to schedule job: " + e.getMessage(), e); + } + } else { + try { + if (debug) { + log.debug("Running control action for immediate execution: " + + " {resource=" + id + + ", action=" + action + + "}"); + } - try { - log.debug("Scheduling job for immediate execution: " + jobDetail); - _scheduler.scheduleJob(jobDetail, trigger); - } catch (SchedulerException e) { - log.error("Unable to schedule job: " + e.getMessage(), e); + doAgentControlCommand(id, null, null, subject, + new Date(), + Boolean.FALSE, "", action, args); + } catch(PluginException e) { + log.error("Unable to execute control action: " + e.getMessage(), e); + } } } |