|
| 1 | +/* |
| 2 | + * This file is part of takenaka, licensed under the Apache License, Version 2.0 (the "License"). |
| 3 | + * |
| 4 | + * Copyright (c) 2023-2024 Matous Kucera |
| 5 | + * |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package me.kcra.takenaka.generator.common.provider.impl |
| 19 | + |
| 20 | +import io.github.oshai.kotlinlogging.KotlinLogging |
| 21 | +import me.kcra.takenaka.core.mapping.MappingContributor |
| 22 | +import net.fabricmc.mappingio.MappingVisitor |
| 23 | + |
| 24 | +private val logger = KotlinLogging.logger {} |
| 25 | + |
| 26 | +/** |
| 27 | + * A wrapping mapping contributor that retries for [retries] times, if an exception is thrown. |
| 28 | + * |
| 29 | + * @property contributor the wrapped contributor |
| 30 | + * @property retries the number of retries after which we should give up |
| 31 | + * @property name the contributor name (used for diagnostics) |
| 32 | + * @author Matouš Kučera |
| 33 | + */ |
| 34 | +internal class RetryingContributor( |
| 35 | + val contributor: MappingContributor, |
| 36 | + val retries: Int, |
| 37 | + val name: String = contributor.javaClass.simpleName |
| 38 | +) : MappingContributor { |
| 39 | + override val targetNamespace by contributor::targetNamespace |
| 40 | + |
| 41 | + override fun accept(visitor: MappingVisitor) { |
| 42 | + repeat(retries - 1) { i -> |
| 43 | + try { |
| 44 | + return contributor.accept(visitor) |
| 45 | + } catch (e: Throwable) { |
| 46 | + logger.error(e) { "Mapping contributor '$name' threw exception, try ${i + 1}/$retries" } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return contributor.accept(visitor) |
| 51 | + } |
| 52 | +} |
0 commit comments