Skip to content

Commit 7cee7ee

Browse files
allow vm work jobs of async job created before prepare for maintenance
1 parent 1fe37de commit 7cee7ee

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

framework/jobs/src/main/java/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
174174
private ExecutorService _apiJobExecutor;
175175
private ExecutorService _workerJobExecutor;
176176

177-
private boolean asyncJobsEnabled = true;
177+
private boolean asyncJobsDisabled = false;
178+
private long asyncJobsDisabledTime = 0;
178179

179180
@Override
180181
public String getConfigComponentName() {
@@ -218,16 +219,48 @@ public long submitAsyncJob(AsyncJob job) {
218219
return submitAsyncJob(job, false);
219220
}
220221

221-
private void checkAsyncJobsAllowed() {
222-
if (!isAsyncJobsEnabled()) {
223-
throw new CloudRuntimeException("Maintenance or Shutdown has been initiated on this management server. Can not accept new jobs");
222+
private void checkAsyncJobAllowed(AsyncJob job) {
223+
if (isAsyncJobsEnabled()) {
224+
return;
225+
}
226+
227+
if (job instanceof VmWorkJobVO) {
228+
String related = job.getRelated();
229+
if (StringUtils.isNotBlank(related)) {
230+
AsyncJob relatedJob = _jobDao.findByIdIncludingRemoved(Long.parseLong(related));
231+
if (relatedJob != null) {
232+
long relatedJobCreatedTime = relatedJob.getCreated().getTime();
233+
if ((asyncJobsDisabledTime - relatedJobCreatedTime) >= 0) {
234+
return;
235+
}
236+
}
237+
}
238+
}
239+
240+
throw new CloudRuntimeException("Maintenance or Shutdown has been initiated on this management server. Can not accept new jobs");
241+
}
242+
243+
private boolean checkSyncQueueItemAllowed(SyncQueueItemVO item) {
244+
if (isAsyncJobsEnabled()) {
245+
return true;
224246
}
247+
248+
Long contentId = item.getContentId();
249+
AsyncJob relatedJob = _jobDao.findByIdIncludingRemoved(contentId);
250+
if (relatedJob != null) {
251+
long relatedJobCreatedTime = relatedJob.getCreated().getTime();
252+
if ((asyncJobsDisabledTime - relatedJobCreatedTime) >= 0) {
253+
return true;
254+
}
255+
}
256+
257+
return false;
225258
}
226259

227260
@SuppressWarnings("unchecked")
228261
@DB
229262
public long submitAsyncJob(AsyncJob job, boolean scheduleJobExecutionInContext) {
230-
checkAsyncJobsAllowed();
263+
checkAsyncJobAllowed(job);
231264

232265
@SuppressWarnings("rawtypes")
233266
GenericDao dao = GenericDaoBase.getDao(job.getClass());
@@ -248,7 +281,7 @@ public long submitAsyncJob(AsyncJob job, boolean scheduleJobExecutionInContext)
248281
@Override
249282
@DB
250283
public long submitAsyncJob(final AsyncJob job, final String syncObjType, final long syncObjId) {
251-
checkAsyncJobsAllowed();
284+
checkAsyncJobAllowed(job);
252285

253286
try {
254287
@SuppressWarnings("rawtypes")
@@ -1301,16 +1334,18 @@ public long countPendingNonPseudoJobs(Long... msIds) {
13011334

13021335
@Override
13031336
public void enableAsyncJobs() {
1304-
this.asyncJobsEnabled = true;
1337+
this.asyncJobsDisabled = false;
1338+
this.asyncJobsDisabledTime = 0;
13051339
}
13061340

13071341
@Override
13081342
public void disableAsyncJobs() {
1309-
this.asyncJobsEnabled = false;
1343+
this.asyncJobsDisabled = true;
1344+
this.asyncJobsDisabledTime = System.currentTimeMillis();
13101345
}
13111346

13121347
@Override
13131348
public boolean isAsyncJobsEnabled() {
1314-
return asyncJobsEnabled;
1349+
return !asyncJobsDisabled;
13151350
}
13161351
}

0 commit comments

Comments
 (0)