Skip to content

Commit 5f8450f

Browse files
Add a shutdownhook to remove jobs owned by the process (apache#8896)
Co-authored-by: Suresh Kumar Anaparti <sureshkumar.anaparti@gmail.com>
1 parent 8a101fb commit 5f8450f

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

usage/src/main/java/com/cloud/usage/UsageManagerImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ public boolean start() {
316316
_sanity = _sanityExecutor.scheduleAtFixedRate(new SanityCheck(), 1, _sanityCheckInterval, TimeUnit.DAYS);
317317
}
318318

319+
Runtime.getRuntime().addShutdownHook(new AbandonJob());
319320
TransactionLegacy usageTxn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
320321
try {
321322
if (_heartbeatLock.lock(3)) { // 3 second timeout
@@ -345,9 +346,11 @@ public boolean stop() {
345346
if (_sanity != null) {
346347
_sanity.cancel(true);
347348
}
349+
348350
return true;
349351
}
350352

353+
351354
@Override
352355
public void run() {
353356
(new ManagedContextRunnable() {
@@ -2183,4 +2186,17 @@ protected void runInContext() {
21832186
}
21842187
}
21852188
}
2189+
private class AbandonJob extends Thread {
2190+
@Override
2191+
public void run() {
2192+
s_logger.info("exitting Usage Manager");
2193+
deleteOpenjob();
2194+
}
2195+
private void deleteOpenjob() {
2196+
UsageJobVO job = _usageJobDao.isOwner(_hostname, _pid);
2197+
if (job != null) {
2198+
_usageJobDao.remove(job.getId());
2199+
}
2200+
}
2201+
}
21862202
}

0 commit comments

Comments
 (0)