AccessUpdateTaskControllerImpl.kt
/*
Copyright (C) 2026 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.archtool.controller
import com.github.kagkarlsson.scheduler.ScheduledExecutionsFilter
import com.github.kagkarlsson.scheduler.SchedulerClient
import org.archtool.configuration.tasks.AccessUpdateRelaunch
import org.archtool.configuration.tasks.AccessUpdateTaskConfig
import org.archtool.data.dto.TaskStatusDto
import org.archtool.data.dto.TaskStatusResponseDto
import org.archtool.data.model.AccessUpdateTaskModel
import org.archtool.service.HistoryService
import org.archtool.service.TaskRelaunchService
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RestController
@RestController
class AccessUpdateTaskControllerImpl(
private val schedulerClient: SchedulerClient,
private val historyService: HistoryService,
private val taskRelaunchService: TaskRelaunchService
) : AccessUpdateTaskController {
private val log = LoggerFactory.getLogger(javaClass)
override fun update(): ResponseEntity<TaskStatusResponseDto> {
val isScheduled = taskRelaunchService.submit(
schedulerClient,
AccessUpdateRelaunch,
AccessUpdateTaskModel()
)
return if (isScheduled) {
log.info("Процесс обновления seaf-ams успешно запущен.")
ResponseEntity.ok(fetchCurrentStatus().copy(message = "Обновление seaf-ams запущено асинхронно"))
} else {
log.info("Обновление seaf-ams уже выполняется. Запрос поставлен в очередь.")
ResponseEntity.ok(fetchCurrentStatus().copy(message = "Запрос поставлен в очередь, выполнится после текущего прогона"))
}
}
override fun status(): ResponseEntity<TaskStatusResponseDto> {
return ResponseEntity.ok(fetchCurrentStatus())
}
private fun fetchCurrentStatus(): TaskStatusResponseDto {
val execution = schedulerClient.getScheduledExecutionsForTask(
AccessUpdateTaskConfig.TASK_NAME,
AccessUpdateTaskModel::class.java,
// Без фильтра db-scheduler не отдаёт захваченные (выполняющиеся) экземпляры: посреди прогона статус был бы IDLE
ScheduledExecutionsFilter.all()
).find {
it.taskInstance.taskName == AccessUpdateTaskConfig.TASK_NAME && it.taskInstance.id == AccessUpdateTaskConfig.GLOBAL_INSTANCE_ID
}
return TaskStatusResponseDto(
orchestrator = execution?.data?.let {
TaskStatusDto(
currentStep = it.currentStep.name
)
} ?: TaskStatusDto.idle(),
history = historyService.getTaskHistory(AccessUpdateTaskConfig.TASK_NAME)
)
}
}