VcsReposDataScheduler.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

  In any derivative products, you must retain the information of
  owner of the original code and provide clear attribution to the project

  https://dochub.info

  The use of this product or its derivatives for any purpose cannot be a secret.


  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.

  Maintainers:
      Alexander Romashin <romashin.a.va@sberbank.ru>, Sber - 2025

  Contributors:
      Alexander Romashin <romashin.a.va@sberbank.ru>, Sber - 2025
      Alexander Romashin <romashin.a.va@sberbank.ru>, Sber - 2026
      Марат Ниязматов <mrusniyazmatov@sberbank.ru>, Sber - 2026
*/

package org.archtool.scheduler

import io.micrometer.core.instrument.kotlin.asContextElement
import io.micrometer.observation.ObservationRegistry
import io.micrometer.tracing.Span
import io.micrometer.tracing.Tracer
import io.micrometer.tracing.handler.TracingObservationHandler
import io.micrometer.tracing.propagation.Propagator
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import kotlinx.coroutines.withContext
import org.archtool.configuration.ServiceProperties
import org.archtool.data.model.RepoInfoModel
import org.archtool.data.model.UpdateStatus
import org.archtool.helper.LockHelper
import org.archtool.helper.SeafGitClient
import org.archtool.provider.RepoRef
import org.archtool.provider.VcsProvider
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import java.time.Instant
import java.util.concurrent.atomic.AtomicReference
import kotlin.time.measureTime

@Component
class VcsReposDataScheduler(
    private val vcsProvider: VcsProvider,
    private val seafGitClient: SeafGitClient,
    private val serviceProperties: ServiceProperties,
    private val lock: LockHelper,
    private val observationRegistry: ObservationRegistry,
    private val tracer: Tracer,
    private val propagator: Propagator
) {
    private val _updateStatus = AtomicReference(UpdateStatus.SCHEDULED)
    val updateStatus: UpdateStatus get() = _updateStatus.get()
    private val _errorMessage = AtomicReference<String?>(null)
    val errorMessage: String? get() = _errorMessage.get()
    private val _updateCompletionTime = AtomicReference<Instant?>(null)
    val updateCompletionTime: Instant? get() = _updateCompletionTime.get()

    private val savedTraceHeaders = AtomicReference<Map<String, String>?>()

    suspend fun scheduleUpdate() {
        val newStatus = _updateStatus.updateAndGet { current ->
            if (current == UpdateStatus.SUCCESS || current == UpdateStatus.ERROR) {
                UpdateStatus.SCHEDULED
            } else {
                current
            }
        }
        if (newStatus != UpdateStatus.SCHEDULED) {
            // Штатная ситуация: вызывающий (оркестратор) повторяет запрос, пока обновление не завершится
            log.debug("Обновление уже выполняется (статус '{}'), запрос не принят", newStatus)
        } else {
            val contextMap = mutableMapOf<String, String>()
            tracer.currentTraceContext().context()?.let { currentContext ->
                propagator.inject(currentContext, contextMap) { map, key, value ->
                    map?.let { it[key] = value }
                }
            }
            savedTraceHeaders.set(contextMap)
        }
    }

    @Scheduled(fixedRateString = "\${seaf.force-update-scheduler-rate}")
    suspend fun scheduleUpdateRepos() {
        lock(UPDATE_REPOS_LOCK_KEY, Unit) {
            val wasScheduled = _updateStatus.compareAndSet(UpdateStatus.SCHEDULED, UpdateStatus.IN_PROGRESS)
            if (!wasScheduled) {
                log.trace("Статус обновления '{}', пропускаем проход", updateStatus)
                return@lock
            }
            val headers = savedTraceHeaders.getAndSet(null)
            var newLocalSpan: Span? = null
            var originalSpan: Span? = null
            var targetTracingContext: TracingObservationHandler.TracingContext? = null
            if (!headers.isNullOrEmpty()) {
                val parentSpan = propagator.extract(headers) { map, key -> map[key] }.start()
                val currentObservation = observationRegistry.currentObservation
                if (currentObservation != null) {
                    targetTracingContext = currentObservation.context
                        .getOrDefault(
                            TracingObservationHandler.TracingContext::class.java,
                            TracingObservationHandler.TracingContext()
                        )
                    originalSpan = targetTracingContext.span
                    newLocalSpan = tracer.nextSpan(parentSpan)
                        ?.name("async-continuation")?.start()
                    targetTracingContext.span = newLocalSpan
                    currentObservation.context.put(
                        TracingObservationHandler.TracingContext::class.java,
                        targetTracingContext
                    )
                }
            }
            try {
                withContext(observationRegistry.asContextElement()) {
                    try {
                        _errorMessage.set(null)
                        val repositories = fetchReposFromVcs()
                        val threadCount = serviceProperties.repoUpdateThreads
                        log.info("Обновляем локальные копии репозиториев (потоков: $threadCount)")
                        val elapsedTime = measureTime {
                            coroutineScope {
                                val semaphore = Semaphore(threadCount)
                                for (repo in repositories) {
                                    val repoRef = RepoRef(localPath = repo.localPath)
                                    launch {
                                        semaphore.withPermit {
                                            try {
                                                seafGitClient.update(
                                                    repoRef = repoRef,
                                                    credentials = vcsProvider.credentials(),
                                                    cloneLink = repo.cloneLink,
                                                )
                                                log.debug("Обновлен репозиторий {}", repo.localPath)
                                            } catch (e: Exception) {
                                                log.error("Ошибка при обновлении репозитория ${repo.localPath}", e)
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        _updateStatus.set(UpdateStatus.SUCCESS)
                        log.info("Обновление локальных копий репозиториев завершено за $elapsedTime")
                    } catch (e: CancellationException) {
                        log.warn("Обновление информации из VCS прервано остановкой сервиса")
                        _updateStatus.set(UpdateStatus.ERROR)
                        _errorMessage.set("Обновление прервано остановкой сервиса")
                        _updateCompletionTime.set(Instant.now())
                        throw e
                    } catch (e: Exception) {
                        _updateStatus.set(UpdateStatus.ERROR)
                        _errorMessage.set(e.message ?: e.toString())
                        log.error("Ошибка при обновлении информации из VCS", e)
                    } catch (e: Throwable) {
                        // Error оставил бы _updateStatus в IN_PROGRESS навсегда (compareAndSet
                        // (SCHEDULED, IN_PROGRESS) заблокировал бы все будущие тики) — фиксируем
                        // ERROR и пробрасываем дальше.
                        _updateStatus.set(UpdateStatus.ERROR)
                        _errorMessage.set(e.message ?: e.toString())
                        log.error("Критическая ошибка JVM при обновлении информации из VCS", e)
                        _updateCompletionTime.set(Instant.now())
                        throw e
                    }
                    _updateCompletionTime.set(Instant.now())
                }
            } finally {
                if (targetTracingContext != null) {
                    newLocalSpan?.end()
                    targetTracingContext.span = originalSpan
                    observationRegistry.currentObservation?.context?.put(
                        TracingObservationHandler.TracingContext::class.java,
                        targetTracingContext
                    )
                }
            }
        }
    }

    private suspend fun fetchReposFromVcs(): List<RepoInfoModel> {
        log.debug("Запрашиваем список репозиториев")
        return vcsProvider.listRepositories()
    }

    companion object {
        const val KEY_PREFIX = "vcs-adapter"
        const val UPDATE_REPOS_LOCK_KEY = "${KEY_PREFIX}:update-repos-lock"
        val log: Logger = LoggerFactory.getLogger(VcsReposDataScheduler::class.java)
    }
}