|
| 1 | +package com.vk.admstorm.admscript |
| 2 | + |
| 3 | +import com.google.common.cache.Cache |
| 4 | +import com.google.common.cache.CacheBuilder |
| 5 | +import com.intellij.openapi.diagnostic.logger |
| 6 | +import com.intellij.openapi.project.Project |
| 7 | +import com.vk.admstorm.CommandRunner |
| 8 | +import com.vk.admstorm.admscript.utils.DataResponse |
| 9 | +import com.vk.admstorm.admscript.utils.DataResponseSerializer |
| 10 | +import com.vk.admstorm.env.Env |
| 11 | +import kotlinx.serialization.KSerializer |
| 12 | +import kotlinx.serialization.json.Json |
| 13 | +import java.util.concurrent.TimeUnit |
| 14 | + |
| 15 | +abstract class AdmScript<TModel>(private val project: Project) : IService<TModel> { |
| 16 | + companion object { |
| 17 | + private val LOG = logger<AdmScript<Any>>() |
| 18 | + |
| 19 | + private val resultCache: Cache<String, String> = CacheBuilder.newBuilder() |
| 20 | + .weakKeys() |
| 21 | + .expireAfterWrite(1, TimeUnit.MINUTES) |
| 22 | + .build() |
| 23 | + |
| 24 | + } |
| 25 | + |
| 26 | + abstract val methodName: String |
| 27 | + |
| 28 | + protected fun <TModel : Any> execCommand(keyName: String, serializer: KSerializer<TModel>): DataResponse<TModel> { |
| 29 | + val workingDir = "${Env.data.projectRoot}/${Env.data.phpSourceFolder}" |
| 30 | + val admScriptName = Env.data.admScriptName |
| 31 | + |
| 32 | + val baseCommand = "php ${workingDir}/${admScriptName}" |
| 33 | + val command = "$baseCommand --method $methodName --key $keyName" |
| 34 | + |
| 35 | + var isFromCache = true |
| 36 | + val cacheValue = resultCache.getIfPresent(keyName) |
| 37 | + val stdout = if (cacheValue == null) { |
| 38 | + val output = CommandRunner.runRemotely( |
| 39 | + project, |
| 40 | + command, |
| 41 | + command, |
| 42 | + workingDir, |
| 43 | + 5_000, |
| 44 | + ) |
| 45 | + |
| 46 | + if (output.exitCode == CommandRunner.FAILED_CODE) { |
| 47 | + return DataResponse(errorMessage = "An error occurred while sending data") |
| 48 | + } |
| 49 | + |
| 50 | + if (output.exitCode == CommandRunner.TIMEOUT_CODE) { |
| 51 | + return DataResponse(errorMessage = "Timeout occurred. Try again.") |
| 52 | + } |
| 53 | + |
| 54 | + val stdout = output.stdout |
| 55 | + if (output.exitCode != 0) { |
| 56 | + return DataResponse(errorMessage = stdout) |
| 57 | + } |
| 58 | + |
| 59 | + isFromCache = false |
| 60 | + stdout |
| 61 | + } else { |
| 62 | + cacheValue |
| 63 | + } |
| 64 | + |
| 65 | + return try { |
| 66 | + val response = Json.decodeFromString(DataResponseSerializer(serializer), stdout) |
| 67 | + if (response.errorMessage == null && !isFromCache) { |
| 68 | + resultCache.put(keyName, stdout) |
| 69 | + } |
| 70 | + |
| 71 | + response |
| 72 | + } catch (e: Exception) { |
| 73 | + LOG.warn("Error when decode the response:", e) |
| 74 | + return DataResponse(errorMessage = "Error when decode the response") |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + abstract override fun execCommand(keyName: String): DataResponse<TModel> |
| 79 | +} |
0 commit comments