2121import java .io .FileInputStream ;
2222import java .lang .reflect .Type ;
2323import java .util .Date ;
24+ import java .util .HashMap ;
2425import java .util .List ;
2526import java .util .Map ;
2627import java .util .Properties ;
4041import org .apache .cloudstack .api .ServerApiException ;
4142import org .apache .cloudstack .api .command .user .job .QueryAsyncJobResultCmd ;
4243import org .apache .cloudstack .api .response .ExceptionResponse ;
44+ import org .apache .cloudstack .framework .events .EventBus ;
45+ import org .apache .cloudstack .framework .events .EventBusException ;
4346import org .apache .log4j .Logger ;
4447import org .apache .log4j .NDC ;
4548import org .springframework .stereotype .Component ;
49+ import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
4650
4751import com .cloud .api .ApiDispatcher ;
52+ import com .cloud .api .ApiDBUtils ;
4853import com .cloud .api .ApiGsonHelper ;
4954import com .cloud .api .ApiSerializerHelper ;
5055import 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+
5160import com .cloud .cluster .ClusterManager ;
5261import com .cloud .cluster .ClusterManagerListener ;
5362import com .cloud .cluster .ManagementServerHostVO ;
5463import com .cloud .configuration .Config ;
5564import com .cloud .configuration .dao .ConfigurationDao ;
5665import com .cloud .exception .InvalidParameterValueException ;
5766import com .cloud .exception .PermissionDeniedException ;
67+ import com .cloud .event .EventCategory ;
68+ import com .cloud .event .EventTypes ;
69+ import com .cloud .server .ManagementServer ;
5870import com .cloud .user .Account ;
5971import com .cloud .user .AccountManager ;
6072import 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