Search in sources :

Example 1 with EventBus

use of com.cloud.framework.events.EventBus in project cosmic by MissionCriticalCloud.

the class ApiServer method handleAsyncJobPublishEvent.

@MessageHandler(topic = AsyncJob.Topics.JOB_EVENT_PUBLISH)
private void handleAsyncJobPublishEvent(final String subject, final String senderAddress, final Object args) {
    assert (args != null);
    final Pair<AsyncJob, String> eventInfo = (Pair<AsyncJob, String>) args;
    final AsyncJob job = eventInfo.first();
    final String jobEvent = eventInfo.second();
    if (s_logger.isTraceEnabled()) {
        s_logger.trace("Handle asyjob publish event " + jobEvent);
    }
    final EventBus eventBus;
    try {
        eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (final NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    if (!job.getDispatcher().equalsIgnoreCase("ApiAsyncJobDispatcher")) {
        return;
    }
    final User userJobOwner = _accountMgr.getUserIncludingRemoved(job.getUserId());
    final Account jobOwner = _accountMgr.getAccount(userJobOwner.getAccountId());
    // Get the event type from the cmdInfo json string
    final String info = job.getCmdInfo();
    String cmdEventType = "unknown";
    if (info != null) {
        final Type type = new TypeToken<Map<String, String>>() {
        }.getType();
        final Map<String, String> cmdInfo = ApiGsonHelper.getBuilder().create().fromJson(info, type);
        final String eventTypeObj = cmdInfo.get("cmdEventType");
        if (eventTypeObj != null) {
            cmdEventType = eventTypeObj;
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Retrieved cmdEventType from job info: " + cmdEventType);
            }
        } else {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Unable to locate cmdEventType marker in job info. publish as unknown event");
            }
        }
    }
    // For some reason, the instanceType / instanceId are not abstract, which means we may get null values.
    final String instanceType = job.getInstanceType() != null ? job.getInstanceType() : "unknown";
    final String instanceUuid = job.getInstanceId() != null ? ApiDBUtils.findJobInstanceUuid(job) : "";
    final Event event = new Event("management-server", EventCategory.ASYNC_JOB_CHANGE_EVENT.getName(), jobEvent, instanceType, instanceUuid);
    final Map<String, String> eventDescription = new HashMap<>();
    eventDescription.put("command", job.getCmd());
    eventDescription.put("user", userJobOwner.getUuid());
    eventDescription.put("account", jobOwner.getUuid());
    eventDescription.put("processStatus", "" + job.getProcessStatus());
    eventDescription.put("resultCode", "" + job.getResultCode());
    eventDescription.put("instanceUuid", instanceUuid);
    eventDescription.put("instanceType", instanceType);
    eventDescription.put("commandEventType", cmdEventType);
    eventDescription.put("jobId", job.getUuid());
    eventDescription.put("jobResult", job.getResult());
    eventDescription.put("cmdInfo", job.getCmdInfo());
    eventDescription.put("status", "" + job.getStatus());
    // If the event.accountinfo boolean value is set, get the human readable value for the username / domainname
    final Map<String, String> configs = _configDao.getConfiguration("management-server", new HashMap<String, String>());
    if (Boolean.valueOf(configs.get("event.accountinfo"))) {
        final DomainVO domain = _domainDao.findById(jobOwner.getDomainId());
        eventDescription.put("username", userJobOwner.getUsername());
        eventDescription.put("accountname", jobOwner.getAccountName());
        eventDescription.put("domainname", domain.getName());
    }
    event.setDescription(eventDescription);
    try {
        eventBus.publish(event);
    } catch (final EventBusException evx) {
        final String errMsg = "Failed to publish async job event on the the event bus.";
        s_logger.warn(errMsg, evx);
    }
}
Also used : UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) User(com.cloud.user.User) HashMap(java.util.HashMap) EventBus(com.cloud.framework.events.EventBus) AsyncJob(com.cloud.framework.jobs.AsyncJob) DomainVO(com.cloud.domain.DomainVO) Type(java.lang.reflect.Type) Event(com.cloud.framework.events.Event) EventBusException(com.cloud.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Map(java.util.Map) HashMap(java.util.HashMap) Pair(com.cloud.utils.Pair) NameValuePair(org.apache.http.NameValuePair) MessageHandler(com.cloud.framework.messagebus.MessageHandler)

Aggregations

DomainVO (com.cloud.domain.DomainVO)1 Event (com.cloud.framework.events.Event)1 EventBus (com.cloud.framework.events.EventBus)1 EventBusException (com.cloud.framework.events.EventBusException)1 AsyncJob (com.cloud.framework.jobs.AsyncJob)1 MessageHandler (com.cloud.framework.messagebus.MessageHandler)1 Account (com.cloud.user.Account)1 User (com.cloud.user.User)1 UserAccount (com.cloud.user.UserAccount)1 Pair (com.cloud.utils.Pair)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NameValuePair (org.apache.http.NameValuePair)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1