Skip to content

Commit

Permalink
Fix: Handle termination for online-only mode without a timer
Browse files Browse the repository at this point in the history
  • Loading branch information
umegane committed May 29, 2024
1 parent 00027e1 commit dfee2b9
Showing 1 changed file with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public class PhoneBill extends ExecutableCommand {
private AtomicInteger tryCounter = new AtomicInteger(0);
private AtomicInteger abortCounter = new AtomicInteger(0);


Config config; // UTからConfigを書き換え可能にするためにパッケージプライベートにしている

public static void main(String[] args) throws Exception {
Expand Down Expand Up @@ -91,6 +90,9 @@ public void execute(Config config) throws Exception {
PhoneBillDbManager.initCounter();
// オンラインアプリを実行する
list.stream().forEach(task -> service.submit(task));
if (service != null) {
service.shutdown();
}

// 指定の実行時間になったら停止するためのタイマーをセット
CountDownLatch latch = new CountDownLatch(1);
Expand All @@ -106,11 +108,20 @@ public void run() {
timer.schedule(timerTask, config.execTimeLimitSecs * 1000L);
}

// バッチの実行
if (config.onlineOnly) {
// online onlyが指定された場合は、タイマーが呼び出されるまで待つ
latch.await();
// online onlyが指定された場合
if (config.execTimeLimitSecs > 0) {
// タイマーが呼び出されるまで待つ
latch.await();
} else {
// オンラインアプリのスレッドが生きている限り無限に待ち続ける
while (service != null && !service.awaitTermination(5, TimeUnit.MINUTES)) {
// do nothing
}
LOG.warn("All online applications have been terminated. Exiting.");
}
} else {
// バッチの実行
Duration d = toDuration(config.targetMonth);
doCalc(d.getStatDate(), d.getEndDate());
}
Expand All @@ -119,7 +130,6 @@ public void run() {
// オンラインアプリを終了する
list.stream().forEach(task -> task.terminate());
if (service != null) {
service.shutdown();
service.awaitTermination(5, TimeUnit.MINUTES);
}
// 終了していないオンラインアプリがある場合は異常終了する。
Expand All @@ -131,15 +141,13 @@ public void run() {
}
}



/**
* Configに従ってオンラインアプリのインスタンスを生成する
*
* @return オンラインアプリのインスタンスのリスト
* @throws IOException
*/
@SuppressFBWarnings(value={"DMI_RANDOM_USED_ONLY_ONCE"})
@SuppressFBWarnings(value = { "DMI_RANDOM_USED_ONLY_ONCE" })
public static List<AbstractOnlineApp> createOnlineApps(Config config, ContractBlockInfoAccessor accessor)
throws IOException {
if (!config.hasOnlineApp()) {
Expand All @@ -161,7 +169,6 @@ public static List<AbstractOnlineApp> createOnlineApps(Config config, ContractBl
config.onlineAppRandomCoverRate);
}


List<AbstractOnlineApp> list = new ArrayList<AbstractOnlineApp>();
if (config.historyInsertThreadCount > 0 && config.historyInsertTransactionPerMin != 0) {
list.addAll(HistoryInsertApp.createHistoryInsertApps(config, new Random(random.nextInt()), accessor,
Expand Down Expand Up @@ -191,7 +198,6 @@ public static List<AbstractOnlineApp> createOnlineApps(Config config, ContractBl
return list;
}


/**
* 指定の日付の一日から月の最終日までのDurationを作成する
*
Expand All @@ -205,7 +211,6 @@ public static Duration toDuration(Date date) {
return new Duration(start, end);
}


/**
* 料金計算のメイン処理
*
Expand Down Expand Up @@ -264,7 +269,8 @@ void doCalc(Date start, Date end) throws Exception {
SessionHoldingType.INSTANCE_FIELD);
managers.add(managerForTask);
}
CalculationTask task = new CalculationTask(queue, managerForTask, config, batchExecId, abortRequested, tryCounter, abortCounter);
CalculationTask task = new CalculationTask(queue, managerForTask, config, batchExecId, abortRequested,
tryCounter, abortCounter);
futures.add(service.submit(task));
}
} catch (RuntimeException e) {
Expand All @@ -284,11 +290,9 @@ void doCalc(Date start, Date end) throws Exception {
}

public String getStatus() {
return queue == null ? "Initializing": queue.getStatus();
return queue == null ? "Initializing" : queue.getStatus();
}



/**
* @param conn
* @param futures
Expand Down Expand Up @@ -330,7 +334,7 @@ private void cleanup(Set<Future<Exception>> futures, List<PhoneBillDbManager> ma
}
}
}
managers.stream().forEach(m->m.close());
managers.stream().forEach(m -> m.close());
if (cause != null) {
LOG.error("Phone bill batch aborting by exception.");
throw cause;
Expand Down

0 comments on commit dfee2b9

Please sign in to comment.