From f6ed6c84770e8bd8ba1870bbe03c7f77e13cac89 Mon Sep 17 00:00:00 2001 From: felixncheng Date: Wed, 3 Apr 2024 16:03:31 +0800 Subject: [PATCH] =?UTF-8?q?bug:=20=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E5=9C=A8=E5=81=9C=E6=AD=A2=E8=BF=87=E7=A8=8B=E4=B8=AD=E8=BF=98?= =?UTF-8?q?=E8=A2=AB=E8=B0=83=E5=BA=A6=E6=89=A7=E8=A1=8C=20#1962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/bkrepo/job/batch/base/BatchJob.kt | 10 +++++----- .../bkrepo/job/config/ScheduledTaskConfigurer.kt | 12 ++++++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/batch/base/BatchJob.kt b/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/batch/base/BatchJob.kt index 8386c3be54..e051c3515c 100644 --- a/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/batch/base/BatchJob.kt +++ b/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/batch/base/BatchJob.kt @@ -199,7 +199,7 @@ abstract class BatchJob(open val batchJobProperties: BatchJobPro if (inProcess && force) { logger.info("Force stop job [${getJobName()}] and unlock.") failover() - lock?.unlockQuietly() + lock?.doUnlock() } stop = true } @@ -277,18 +277,18 @@ abstract class BatchJob(open val batchJobProperties: BatchJobPro try { block() } finally { - unlockQuietly() + doUnlock() } } /** * 静默释放锁 * */ - private fun SimpleLock.unlockQuietly() { + private fun SimpleLock.doUnlock() { try { unlock() - } catch (ignore: Exception) { - // ignore + } catch (e: Exception) { + logger.error("Unlock failed", e) } } diff --git a/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/config/ScheduledTaskConfigurer.kt b/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/config/ScheduledTaskConfigurer.kt index a7b6cdd956..4e0c625e98 100644 --- a/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/config/ScheduledTaskConfigurer.kt +++ b/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/config/ScheduledTaskConfigurer.kt @@ -58,7 +58,10 @@ class ScheduledTaskConfigurer( val taskScheduler = builder.build() taskScheduler.initialize() Companion.taskRegistrar.setTaskScheduler(taskScheduler) - ServiceShutdownHook.add { jobs.forEach { it.stop(it.batchJobProperties.stopTimeout, true) } } + ServiceShutdownHook.add { + cleanScheduledTask() + jobs.forEach { it.stop(it.batchJobProperties.stopTimeout, true) } + } } jobs.filter { it.batchJobProperties.enabled }.forEach { val properties = it.batchJobProperties @@ -114,9 +117,14 @@ class ScheduledTaskConfigurer( val scheduledTaskSet = mutableSetOf() private val logger = LoggerFactory.getLogger(ScheduledTaskConfigurer::class.java) - fun reloadScheduledTask() { + fun cleanScheduledTask() { scheduledTaskSet.forEach { it.cancel() } scheduledTaskSet.clear() + logger.info("Clean scheduled task") + } + + fun reloadScheduledTask() { + cleanScheduledTask() scheduledTaskConfigurer.configureTasks(taskRegistrar) logger.info("Reload scheduled task") }