Search in sources :

Example 1 with AsyncJobDispatcher

use of com.cloud.framework.jobs.AsyncJobDispatcher in project cosmic by MissionCriticalCloud.

the class AsyncJobManagerImpl method getExecutorRunnable.

private Runnable getExecutorRunnable(final AsyncJob job) {
    return new ManagedContextRunnable() {

        @Override
        public void run() {
            // register place-holder context to avoid installing system account call context
            if (CallContext.current() == null) {
                CallContext.registerPlaceHolderContext();
            }
            final String related = job.getRelated();
            String logContext = job.getShortUuid();
            if (related != null && !related.isEmpty()) {
                MDC.put("job", " (job: " + related + "/" + "job: " + job.getId() + ")");
                final AsyncJob relatedJob = _jobDao.findByIdIncludingRemoved(Long.parseLong(related));
                if (relatedJob != null) {
                    logContext = relatedJob.getShortUuid();
                }
            } else {
                MDC.put("job", " (job: " + job.getId() + ")");
            }
            MDC.put("logcontextid", " (logid: " + logContext + ")");
            try {
                super.run();
            } finally {
                MDC.remove("job");
            }
        }

        @Override
        protected void runInContext() {
            final long runNumber = getJobRunNumber();
            try {
                // 
                try {
                    JmxUtil.registerMBean("AsyncJobManager", "Active Job " + job.getId(), new AsyncJobMBeanImpl(job));
                } catch (final Exception e) {
                    // is expected to fail under situations
                    if (s_logger.isTraceEnabled()) {
                        s_logger.trace("Unable to register active job " + job.getId() + " to JMX monitoring due to exception " + ExceptionUtil.toString(e));
                    }
                }
                _jobMonitor.registerActiveTask(runNumber, job.getId());
                AsyncJobExecutionContext.setCurrentExecutionContext(new AsyncJobExecutionContext(job));
                final String related = job.getRelated();
                String logContext = job.getShortUuid();
                if (related != null && !related.isEmpty()) {
                    final AsyncJob relatedJob = _jobDao.findByIdIncludingRemoved(Long.parseLong(related));
                    if (relatedJob != null) {
                        logContext = relatedJob.getShortUuid();
                    }
                }
                MDC.put("logcontextid", " (logid: " + logContext + ")");
                // execute the job
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Executing " + StringUtils.cleanString(job.toString()));
                }
                if ((getAndResetPendingSignals(job) & AsyncJob.Constants.SIGNAL_MASK_WAKEUP) != 0) {
                    final AsyncJobDispatcher jobDispatcher = getWakeupDispatcher(job);
                    if (jobDispatcher != null) {
                        jobDispatcher.runJob(job);
                    } else {
                        // TODO, job wakeup is not in use yet
                        if (s_logger.isTraceEnabled()) {
                            s_logger.trace("Unable to find a wakeup dispatcher from the joined job: " + job);
                        }
                    }
                } else {
                    final AsyncJobDispatcher jobDispatcher = getDispatcher(job.getDispatcher());
                    if (jobDispatcher != null) {
                        jobDispatcher.runJob(job);
                    } else {
                        s_logger.error("Unable to find job dispatcher, job will be cancelled");
                        final ExceptionResponse response = new ExceptionResponse();
                        response.setErrorCode(ApiErrorCode.INTERNAL_ERROR.getHttpCode());
                        response.setErrorText("Unable to find job dispatcher, job will be cancelled");
                        completeAsyncJob(job.getId(), JobInfo.Status.FAILED, ApiErrorCode.INTERNAL_ERROR.getHttpCode(), JobSerializerHelper.toSerializedString(response));
                    }
                }
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Done executing " + job.getCmd() + " for job-" + job.getId());
                }
            } catch (final Throwable e) {
                s_logger.error("Unexpected exception", e);
                final ExceptionResponse response = new ExceptionResponse();
                response.setErrorCode(ApiErrorCode.INTERNAL_ERROR.getHttpCode());
                response.setErrorText(ExceptionUtils.getRootCauseMessage(e));
                completeAsyncJob(job.getId(), JobInfo.Status.FAILED, ApiErrorCode.INTERNAL_ERROR.getHttpCode(), JobSerializerHelper.toSerializedString(response));
            } finally {
                // guard final clause as well
                try {
                    if (job.getSyncSource() != null) {
                        // here check queue item one more time to double make sure that queue item is removed in case of any uncaught exception
                        _queueMgr.purgeItem(job.getSyncSource().getId());
                    }
                    try {
                        JmxUtil.unregisterMBean("AsyncJobManager", "Active Job " + job.getId());
                    } catch (final Exception e) {
                        // is expected to fail under situations
                        if (s_logger.isTraceEnabled()) {
                            s_logger.trace("Unable to unregister job " + job.getId() + " to JMX monitoring due to exception " + ExceptionUtil.toString(e));
                        }
                    }
                    // 
                    // clean execution environment
                    // 
                    AsyncJobExecutionContext.unregister();
                    _jobMonitor.unregisterActiveTask(runNumber);
                } catch (final Throwable e) {
                    s_logger.error("Double exception", e);
                }
            }
        }
    };
}
Also used : ManagedContextRunnable(com.cloud.managed.context.ManagedContextRunnable) ExceptionResponse(com.cloud.api.response.ExceptionResponse) AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) AsyncJob(com.cloud.framework.jobs.AsyncJob) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) AsyncJobDispatcher(com.cloud.framework.jobs.AsyncJobDispatcher)

Aggregations

ExceptionResponse (com.cloud.api.response.ExceptionResponse)1 AsyncJob (com.cloud.framework.jobs.AsyncJob)1 AsyncJobDispatcher (com.cloud.framework.jobs.AsyncJobDispatcher)1 AsyncJobExecutionContext (com.cloud.framework.jobs.AsyncJobExecutionContext)1 ManagedContextRunnable (com.cloud.managed.context.ManagedContextRunnable)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ConfigurationException (javax.naming.ConfigurationException)1