Skip to content

Commit e7c8a35

Browse files
Ryan Dietrichmurali-reddy
authored andcommitted
CLOUDSTACK-3190: Async jobs actions now trigger event bus messages to be
published -AsyncJobManagerImpl to publish async job events when async jobs are created, updated and completed
1 parent be17ea8 commit e7c8a35

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎api/src/com/cloud/event/EventCategory.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ public static EventCategory getEventCategory(String categoryName) {
5252
public static final EventCategory ALERT_EVENT = new EventCategory("AlertEvent");
5353
public static final EventCategory USAGE_EVENT = new EventCategory("UsageEvent");
5454
public static final EventCategory RESOURCE_STATE_CHANGE_EVENT = new EventCategory("ResourceStateEvent");
55+
public static final EventCategory ASYNC_JOB_CHANGE_EVENT = new EventCategory("AsyncJobEvent");
5556
}

‎server/src/com/cloud/api/ApiServer.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ private String queueCommand(BaseCmd cmdObj, Map<String, String> params) throws E
502502
}
503503

504504
params.put("ctxStartEventId", String.valueOf(startEventId));
505+
params.put("cmdEventType", asyncCmd.getEventType().toString());
505506

506507
ctx.setAccountId(asyncCmd.getEntityOwnerId());
507508

‎server/src/com/cloud/async/AsyncJobManagerImpl.java‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.FileInputStream;
2222
import java.lang.reflect.Type;
2323
import java.util.Date;
24+
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.Properties;
@@ -40,21 +41,32 @@
4041
import org.apache.cloudstack.api.ServerApiException;
4142
import org.apache.cloudstack.api.command.user.job.QueryAsyncJobResultCmd;
4243
import org.apache.cloudstack.api.response.ExceptionResponse;
44+
import org.apache.cloudstack.framework.events.EventBus;
45+
import org.apache.cloudstack.framework.events.EventBusException;
4346
import org.apache.log4j.Logger;
4447
import org.apache.log4j.NDC;
4548
import org.springframework.stereotype.Component;
49+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
4650

4751
import com.cloud.api.ApiDispatcher;
52+
import com.cloud.api.ApiDBUtils;
4853
import com.cloud.api.ApiGsonHelper;
4954
import com.cloud.api.ApiSerializerHelper;
5055
import com.cloud.async.dao.AsyncJobDao;
56+
import com.cloud.domain.dao.DomainDao;
57+
import com.cloud.domain.Domain;
58+
import com.cloud.domain.DomainVO;
59+
5160
import com.cloud.cluster.ClusterManager;
5261
import com.cloud.cluster.ClusterManagerListener;
5362
import com.cloud.cluster.ManagementServerHostVO;
5463
import com.cloud.configuration.Config;
5564
import com.cloud.configuration.dao.ConfigurationDao;
5665
import com.cloud.exception.InvalidParameterValueException;
5766
import com.cloud.exception.PermissionDeniedException;
67+
import com.cloud.event.EventCategory;
68+
import com.cloud.event.EventTypes;
69+
import com.cloud.server.ManagementServer;
5870
import com.cloud.user.Account;
5971
import com.cloud.user.AccountManager;
6072
import com.cloud.user.User;
@@ -93,6 +105,7 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
93105
@Inject private AccountDao _accountDao;
94106
@Inject private AsyncJobDao _jobDao;
95107
@Inject private ConfigurationDao _configDao;
108+
@Inject private DomainDao _domainDao;
96109
private long _jobExpireSeconds = 86400; // 1 day
97110
private long _jobCancelThresholdSeconds = 3600; // 1 hour (for cancelling the jobs blocking other jobs)
98111

@@ -122,6 +135,65 @@ public List<AsyncJobVO> findInstancePendingAsyncJobs(AsyncJob.Type instanceType,
122135
return _jobDao.findInstancePendingAsyncJobs(instanceType, accountId);
123136
}
124137

138+
private void publishOnEventBus(AsyncJobVO job, String jobEvent) {
139+
EventBus eventBus = null;
140+
try {
141+
eventBus = ComponentContext.getComponent(EventBus.class);
142+
} catch(NoSuchBeanDefinitionException nbe) {
143+
return; // no provider is configured to provide events bus, so just return
144+
}
145+
146+
// Get the event type from the cmdInfo json string
147+
String info = job.getCmdInfo();
148+
String cmdEventType;
149+
if ( info == null ) {
150+
cmdEventType = "unknown";
151+
} else {
152+
String marker = "\"cmdEventType\"";
153+
int begin = info.indexOf(marker);
154+
cmdEventType = info.substring(begin + marker.length() + 2, info.indexOf(",", begin) - 1);
155+
}
156+
157+
// For some reason, the instanceType / instanceId are not abstract, which means we may get null values.
158+
org.apache.cloudstack.framework.events.Event event = new org.apache.cloudstack.framework.events.Event(
159+
ManagementServer.Name,
160+
EventCategory.ASYNC_JOB_CHANGE_EVENT.getName(),
161+
jobEvent,
162+
( job.getInstanceType() != null ? job.getInstanceType().toString() : "unknown" ), null);
163+
164+
User userJobOwner = _accountMgr.getUserIncludingRemoved(job.getUserId());
165+
Account jobOwner = _accountMgr.getAccount(userJobOwner.getAccountId());
166+
167+
Map<String, String> eventDescription = new HashMap<String, String>();
168+
eventDescription.put("command", job.getCmd());
169+
eventDescription.put("user", userJobOwner.getUuid());
170+
eventDescription.put("account", jobOwner.getUuid());
171+
eventDescription.put("processStatus", "" + job.getProcessStatus());
172+
eventDescription.put("resultCode", "" + job.getResultCode());
173+
eventDescription.put("instanceUuid", ApiDBUtils.findJobInstanceUuid(job));
174+
eventDescription.put("instanceType", ( job.getInstanceType() != null ? job.getInstanceType().toString() : "unknown" ) );
175+
eventDescription.put("commandEventType", cmdEventType);
176+
eventDescription.put("jobId", job.getUuid());
177+
178+
// If the event.accountinfo boolean value is set, get the human readable value for the username / domainname
179+
Map<String, String> configs = _configDao.getConfiguration("management-server", new HashMap<String, String>());
180+
if ( Boolean.valueOf(configs.get("event.accountinfo")) ) {
181+
DomainVO domain = _domainDao.findById(jobOwner.getDomainId());
182+
eventDescription.put("username", userJobOwner.getUsername());
183+
eventDescription.put("domainname", domain.getName());
184+
}
185+
186+
event.setDescription(eventDescription);
187+
188+
try {
189+
eventBus.publish(event);
190+
} catch (EventBusException evx) {
191+
String errMsg = "Failed to publish async job event on the the event bus.";
192+
s_logger.warn(errMsg, evx);
193+
throw new CloudRuntimeException(errMsg);
194+
}
195+
}
196+
125197
@Override
126198
public long submitAsyncJob(AsyncJobVO job) {
127199
return submitAsyncJob(job, false);
@@ -142,6 +214,7 @@ public long submitAsyncJob(AsyncJobVO job, boolean scheduleJobExecutionInContext
142214
if(s_logger.isDebugEnabled()) {
143215
s_logger.debug("submit async job-" + job.getId() + ", details: " + job.toString());
144216
}
217+
publishOnEventBus(job, "submit");
145218
return job.getId();
146219
} catch(Exception e) {
147220
txt.rollback();
@@ -176,6 +249,8 @@ public void completeAsyncJob(long jobId, int jobStatus, int resultCode, Object r
176249
job.setStatus(jobStatus);
177250
job.setResultCode(resultCode);
178251

252+
publishOnEventBus(job, "complete"); // publish before the instance type and ID are wiped out
253+
179254
// reset attached object
180255
job.setInstanceType(null);
181256
job.setInstanceId(null);
@@ -219,6 +294,7 @@ public void updateAsyncJobStatus(long jobId, int processStatus, Object resultObj
219294
}
220295
job.setLastUpdated(DateUtil.currentGMTTime());
221296
_jobDao.update(jobId, job);
297+
publishOnEventBus(job, "update");
222298
txt.commit();
223299
} catch(Exception e) {
224300
s_logger.error("Unexpected exception while updating async job-" + jobId + " status: ", e);

0 commit comments

Comments
 (0)