RolesMgrController.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 io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.security.SecurityRequirements
import io.swagger.v3.oas.annotations.tags.Tag
import org.archtool.data.dto.UpdateStatusResponse
import org.archtool.data.model.UpdateStatus
import org.archtool.scheduler.VcsReposInfoScheduler.Companion.log
import org.archtool.service.SettingsService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@Tag(name = "Контроллер управления ролевой моделью", description = "Отвечает за управление процессом обновления данных ролевой модели")
@SecurityRequirements
@RequestMapping("/roles-mgr")
@RestController
class RolesMgrController(
private val settingsService: SettingsService,
) {
@Operation(
summary = "Запуск обновления данных",
description = "Устанавливает статус \"обновление запланировано\", что приводит к запуску процесса обновления данных. " +
"Ответ SCHEDULED — запрос принят: обновление начнётся после него. Ответ IN_PROGRESS — обновление, " +
"начатое до запроса, ещё выполняется, и запрос не принят: вызывающий должен повторить его после завершения обновления.",
responses = [
ApiResponse(
responseCode = "200", description = "Обновление запланировано",
content = [Content(schema = Schema(implementation = UpdateStatusResponse::class))]
)
]
)
@PutMapping("/update")
suspend fun scheduleUpdate(): UpdateStatusResponse {
val status = settingsService.getUpdateStatus()
if(status == null || status == UpdateStatus.SUCCESS || status== UpdateStatus.ERROR) {
settingsService.setUpdateStatus(UpdateStatus.SCHEDULED)
} else if (status == UpdateStatus.SCHEDULED) {
log.debug("Обновление уже запланировано, запрос будет выполнен им")
} else {
// Штатная ситуация: вызывающий (оркестратор) повторяет запрос, пока обновление не завершится
log.debug("Обновление уже выполняется (статус '{}'), запрос не принят", status)
}
return settingsService.getUpdateStatus().let {
UpdateStatusResponse(it?.name, it?.value)
}
}
@Operation(
summary = "Получение текущего статуса обновления",
description = "Возвращает текущий статус обновления данных, его текстовое описание, " +
"а также время завершения (при статусе SUCCESS) или сообщение об ошибке (при статусе ERROR)",
responses = [
ApiResponse(
responseCode = "200", description = "Статус обновления данных",
content = [Content(schema = Schema(implementation = UpdateStatusResponse::class))]
)
]
)
@GetMapping("/status")
suspend fun getUpdateStatus(): UpdateStatusResponse {
val status = settingsService.getUpdateStatus()
val completionTime = if (status == UpdateStatus.SUCCESS) {
settingsService.getUpdateCompletionTime()
} else null
val errorMessage = if (status == UpdateStatus.ERROR) {
settingsService.getUpdateErrorMessage()
} else null
return UpdateStatusResponse(
status = status?.name,
text = status?.value,
completionTime = completionTime,
errorMessage = errorMessage
)
}
}