Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Speed up shutdown
  • Loading branch information
lzw2006 committed Jul 16, 2018
1 parent fc9cb60 commit d462e9d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public void shutdown() {
}
}

@Override
public void shutdownGracefully() {
if (scheduler != null) {
scheduler.shutdownNow();
}
}

public ExecutorService getExecutorService() {
return jobScheduler.getExecutorService();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,32 @@ public void shutdown(boolean removejob) {
}
}

public void shutdownGracefully() {
synchronized (isShutdownFlag) {
isShutdownFlag.set(true);
if (job != null) {
job.shutdownGracefully();
}
listenerManager.shutdown();
shardingService.shutdown();
configService.shutdown();
leaderElectionService.shutdown();
serverService.shutdown();
executionContextService.shutdown();
executionService.shutdown();
failoverService.shutdown();
statisticsService.shutdown();
analyseService.shutdown();
limitMaxJobsService.shutdown();
zkCacheManager.shutdown();

JobRegistry.clearJob(executorName, jobName);
if (executorService != null && !executorService.isShutdown()) {
executorService.shutdown();
}
}
}

/**
* 重新调度作业.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public interface Stopable {
* 关闭作业
*/
void shutdown();

/**
* 优雅关闭作业
*/
void shutdownGracefully();
}
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ private void shutdownAllCountThread() {
}
}

private void shutdownUnfinishJob() {
private void shutdownUnfinishJob(boolean shutdownGracefully) {
Map<String, JobScheduler> schdMap = JobRegistry.getSchedulerMap().get(executorName);
if (schdMap != null) {
Iterator<String> it = schdMap.keySet().iterator();
Expand All @@ -470,7 +470,11 @@ private void shutdownUnfinishJob() {
log.info("[{}] msg=job {} is enabled, force shutdown.", jobName, jobName);
jobScheduler.stopJob(true);
}
jobScheduler.shutdown(false);
if (shutdownGracefully) {
jobScheduler.shutdownGracefully();
} else {
jobScheduler.shutdown(false);
}
}
}
}
Expand All @@ -486,7 +490,7 @@ private void shutdown0() throws Exception {
if (saturnExecutorService != null) {
saturnExecutorService.unregisterJobsWatcher();
}
shutdownUnfinishJob();
shutdownUnfinishJob(false);
if (saturnExecutorService != null) {
saturnExecutorService.unregisterExecutor();
}
Expand Down Expand Up @@ -533,7 +537,7 @@ private void shutdownGracefully0() throws Exception {
TimeoutSchedulerExecutor.shutdownScheduler(executorName);
try {
blockUntilJobCompletedIfNotTimeout();
shutdownUnfinishJob();
shutdownUnfinishJob(true);
JobRegistry.clearExecutor(executorName);
} finally {
if (connectionLostListener != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/**
*
*
*/
package com.vip.saturn.job.trigger;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import com.vip.saturn.job.basic.AbstractElasticJob;
import org.quartz.SchedulerException;
import org.quartz.Trigger;

import com.vip.saturn.job.basic.AbstractElasticJob;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
* @author chembo.huang
Expand Down Expand Up @@ -59,6 +58,11 @@ public void shutdown() {
executor.shutdown();
}

public void shutdownNow() {
saturnQuartzWorker.halt();
executor.shutdownNow();
}

public void triggerJob() {
saturnQuartzWorker.trigger();
}
Expand Down

0 comments on commit d462e9d

Please sign in to comment.