Skip to content

Commit

Permalink
[api]: Drop Preference table; Fallback on failed to download IERS
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Nov 22, 2024
1 parent 51e8a90 commit 37329b1
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 204 deletions.
46 changes: 29 additions & 17 deletions api/src/main/kotlin/nebulosa/api/Nebulosa.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ import nebulosa.api.core.FileLocker
import nebulosa.api.database.migration.MainDatabaseMigrator
import nebulosa.api.database.migration.SkyDatabaseMigrator
import nebulosa.api.inject.controllersModule
import nebulosa.api.inject.koinApp
import nebulosa.api.inject.coreModule
import nebulosa.api.inject.databaseModule
import nebulosa.api.inject.eventBusModule
import nebulosa.api.inject.httpModule
import nebulosa.api.inject.objectMapperModule
import nebulosa.api.inject.pathModule
import nebulosa.api.inject.phd2Module
import nebulosa.api.inject.repositoriesModule
import nebulosa.api.inject.serverModule
import nebulosa.api.inject.servicesModule
import nebulosa.api.ktor.configureHTTP
import nebulosa.api.ktor.configureMonitoring
import nebulosa.api.ktor.configureRouting
import nebulosa.api.ktor.configureSerialization
import nebulosa.api.ktor.configureSockets
import nebulosa.api.preference.PreferenceService
import nebulosa.json.PathModule
import nebulosa.log.d
import nebulosa.log.loggerFor
Expand All @@ -38,32 +45,32 @@ import java.util.concurrent.ExecutorService
import kotlin.io.path.Path
import kotlin.io.path.exists
import kotlin.io.path.fileSize
import kotlin.io.path.inputStream
import kotlin.io.path.isRegularFile
import kotlin.system.exitProcess

@Command(name = "nebulosa")
class Nebulosa : Runnable {

private val properties = Properties(3)
private val preferencesPath = Path(System.getProperty(APP_DIR_KEY), PreferenceService.FILENAME)
private val preferences = PreferenceService()

init {
Path(System.getProperty(APP_DIR_KEY), PROPERTIES_FILENAME)
preferencesPath
.takeIf { it.exists() && it.isRegularFile() }
?.also { it.inputStream().use(properties::load) }
?.also(preferences::load)
}

@Option(name = ["-h", "--host"])
private var host = properties.getProperty("host")?.ifBlank { null } ?: DEFAULT_HOST
private var host = preferences["host"]?.ifBlank { null } ?: DEFAULT_HOST

@Option(name = ["-p", "--port"])
private var port = properties.getProperty("port")?.ifBlank { null }?.toIntOrNull() ?: DEFAULT_PORT
private var port = preferences["port"]?.toIntOrNull() ?: DEFAULT_PORT

@Option(name = ["-d", "--debug"])
private var debug = properties.getProperty("debug")?.toBoolean() == true
private var debug = preferences["debug"]?.toBoolean() == true

@Option(name = ["-t", "--trace"])
private var trace = properties.getProperty("trace")?.toBoolean() == true
private var trace = preferences["trace"]?.toBoolean() == true

@Option(name = ["-f", "--files"])
private val files = mutableListOf<String>()
Expand Down Expand Up @@ -106,13 +113,19 @@ class Nebulosa : Runnable {
configureMonitoring(debug)
}.start(false)

// app.exception(Exception::class.java, ::handleException)

koinApp.modules(serverModule(server))
koinApp.modules(objectMapperModule(OBJECT_MAPPER))
koinApp.modules(servicesModule())
koinApp.modules(controllersModule())
startKoin(koinApp)
val koinApp = startKoin {
modules(pathModule())
modules(coreModule())
modules(httpModule())
modules(databaseModule())
modules(eventBusModule())
modules(repositoriesModule())
modules(phd2Module())
modules(serverModule(server))
modules(objectMapperModule(OBJECT_MAPPER))
modules(servicesModule(preferences))
modules(controllersModule())
}

with(runBlocking { server.engine.resolvedConnectors().first().port }) {
println("server is started at port: $this")
Expand Down Expand Up @@ -144,7 +157,6 @@ class Nebulosa : Runnable {

internal val LOG = loggerFor<Nebulosa>()

const val PROPERTIES_FILENAME = "nebulosa.properties"
const val DEFAULT_HOST = "0.0.0.0"
const val DEFAULT_PORT = 0

Expand Down
53 changes: 36 additions & 17 deletions api/src/main/kotlin/nebulosa/api/atlas/IERSUpdateTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.koin.core.component.get
import java.nio.file.Path
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
import kotlin.io.path.exists
import kotlin.io.path.inputStream
import kotlin.io.path.outputStream

Expand All @@ -34,53 +35,71 @@ class IERSUpdateTask(
override fun run() {
get<MainDatabaseMigrator>().await()

val iersa = IERSA()
val iersb = IERSB()
var iersa: IERSA? = null
var iersb: IERSB? = null

with(Path.of("$dataPath", "finals2000A.all")) {
download(IERSA.URL, IERSA_UPDATED_AT_KEY)
inputStream().use(iersa::load)
if (download(IERSA.URL, IERSA_UPDATED_AT_KEY)) {
iersa = IERSA()
inputStream().use(iersa::load)
}
}

with(Path.of("$dataPath", "eopc04.1962-now.txt")) {
download(IERSB.URL, IERSB_UPDATED_AT_KEY)
inputStream().use(iersb::load)
if (download(IERSB.URL, IERSB_UPDATED_AT_KEY)) {
iersb = IERSB()
inputStream().use(iersb::load)
}
}

IERS.attach(IERSAB(iersa, iersb))
if (iersa != null && iersb != null) IERS.attach(IERSAB(iersa, iersb))
else if (iersa != null) IERS.attach(iersa)
else if (iersb != null) IERS.attach(iersb)
}

private fun Path.download(url: String, key: String) {
private fun Path.download(url: String, key: String): Boolean {
try {
var request = Request.Builder().head().url(url).build()

var modifiedAt = httpClient.newCall(request).execute()
.use { it.headers.getDate(HttpHeaders.LastModified) }
?.toInstant()?.toEpochMilli()

if (modifiedAt != null && "$modifiedAt" == preferenceService.getText(key)) {
if (exists() && modifiedAt != null && modifiedAt == preferenceService[key]?.toLongOrNull()) {
LOG.info("{} is up to date. modifiedAt={}", url, modifiedAt)
return
return true
}

request = request.newBuilder().get().build()

LOG.d { debug("downloading {}", url) }
LOG.d { debug("{} is out of date. modifiedAt={}", url, modifiedAt) }

httpClient.newCall(request).execute().use {
it.body!!.byteStream().transferAndClose(outputStream())
modifiedAt = it.headers.getDate(HttpHeaders.LastModified)
preferenceService.putText(key, "$modifiedAt")
LOG.d { debug("{} downloaded. modifiedAt={}", url, modifiedAt) }
if (it.isSuccessful) {
it.body!!.byteStream().transferAndClose(outputStream())
modifiedAt = it.headers.getDate(HttpHeaders.LastModified)?.toInstant()?.toEpochMilli()

if (modifiedAt != null) {
preferenceService[key] = modifiedAt
preferenceService.save()
}

LOG.d { debug("{} downloaded. modifiedAt={}", url, modifiedAt) }

return true
}
}
} catch (e: Throwable) {
LOG.error("failed to download finals2000A.all", e)
}

return false
}

companion object {

const val IERSA_UPDATED_AT_KEY = "IERSA.UPDATED_AT"
const val IERSB_UPDATED_AT_KEY = "IERSB.UPDATED_AT"
const val IERSA_UPDATED_AT_KEY = "iersa.updatedAt"
const val IERSB_UPDATED_AT_KEY = "iersb.updatedAt"

private val LOG = loggerFor<IERSUpdateTask>()
}
Expand Down
7 changes: 4 additions & 3 deletions api/src/main/kotlin/nebulosa/api/atlas/LibWCSDownloadTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ class LibWCSDownloadTask(
if (response.isSuccessful) {
val newestVersion = response.body!!.string()

if (newestVersion != preferenceService.getText(VERSION_KEY) || !libraryPath.exists()) {
if (newestVersion != preferenceService[VERSION_KEY] || !libraryPath.exists()) {
LOG.info("libwcs is out of date. Downloading...")

request = Request.Builder().get().url(libraryUrl).build()

httpClient.newCall(request).execute().use {
if (it.isSuccessful) {
it.body!!.byteStream().transferAndCloseOutput(libraryPath.outputStream())
preferenceService.putText(VERSION_KEY, newestVersion)
preferenceService[VERSION_KEY] = newestVersion
preferenceService.save()
}
}
} else {
Expand All @@ -77,7 +78,7 @@ class LibWCSDownloadTask(
companion object {

const val VERSION_URL = "https://raw.githubusercontent.com/tiagohm/nebulosa.data/main/libs/wcs/VERSION.txt"
const val VERSION_KEY = "LIBWCS.VERSION"
const val VERSION_KEY = "libwcs.version"

const val LINUX_X86_64 = "linux-x86-64"
const val LINUX_AARCH_64 = "linux-aarch64"
Expand Down
9 changes: 5 additions & 4 deletions api/src/main/kotlin/nebulosa/api/atlas/SatelliteUpdateTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class SatelliteUpdateTask(

private fun isOutOfDate(): Boolean {
try {
val updatedAt = preferenceService.getLong(UPDATED_AT_KEY) ?: 0L
val updatedAt = preferenceService[UPDATED_AT_KEY]?.toLongOrNull() ?: 0L
return System.currentTimeMillis() - updatedAt >= UPDATE_INTERVAL
} catch (e: JsonMappingException) {
} catch (_: JsonMappingException) {
return true
}
}
Expand All @@ -47,7 +47,8 @@ class SatelliteUpdateTask(
LOG.info("satellites is out of date")

if (updateTLEs()) {
preferenceService.putLong(UPDATED_AT_KEY, System.currentTimeMillis())
preferenceService[UPDATED_AT_KEY] = System.currentTimeMillis()
preferenceService.save()
} else {
LOG.warn("no satellites was updated")
}
Expand Down Expand Up @@ -122,7 +123,7 @@ class SatelliteUpdateTask(
companion object {

const val UPDATE_INTERVAL = 1000L * 60 * 60 * 24 * 2 // 2 days in ms
const val UPDATED_AT_KEY = "SATELLITES.UPDATED_AT"
const val UPDATED_AT_KEY = "satellites.updatedAt"

private val LOG = loggerFor<SatelliteUpdateTask>()
}
Expand Down
8 changes: 5 additions & 3 deletions api/src/main/kotlin/nebulosa/api/atlas/SkyAtlasUpdateTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SkyAtlasUpdateTask(
httpClient.newCall(request).execute().use { response ->
if (response.isSuccessful) {
val newestVersion = response.body!!.string().trim()
val currentVersion = preferenceService.getText(VERSION_KEY)
val currentVersion = preferenceService[VERSION_KEY]

if (newestVersion != currentVersion || skyObjectEntityRepository.size == 0L) {
skyObjectEntityRepository.clear()
Expand Down Expand Up @@ -71,7 +71,9 @@ class SkyAtlasUpdateTask(
}
}

preferenceService.putText(VERSION_KEY, newestVersion)
preferenceService[VERSION_KEY] = newestVersion
preferenceService.save()

messageService.sendMessage(SkyAtlasUpdateNotificationEvent.Finished(newestVersion))

LOG.info("Sky Atlas database was updated. version={}, size={}", newestVersion, skyObjectEntityRepository.size)
Expand All @@ -85,7 +87,7 @@ class SkyAtlasUpdateTask(
companion object {

const val VERSION_URL = "https://raw.githubusercontent.com/tiagohm/nebulosa.data/main/simbad/VERSION.txt"
const val VERSION_KEY = "SKY_ATLAS.VERSION"
const val VERSION_KEY = "skyAtlas.version"

const val DATA_URL = "https://raw.githubusercontent.com/tiagohm/nebulosa.data/main/simbad/simbad.%02d.dat"
const val MAX_DATA_COUNT = 100
Expand Down
7 changes: 5 additions & 2 deletions api/src/main/kotlin/nebulosa/api/guiding/GuidingService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nebulosa.api.guiding

import com.fasterxml.jackson.databind.ObjectMapper
import nebulosa.api.message.MessageService
import nebulosa.api.preference.PreferenceService
import nebulosa.guiding.GuideStar
Expand All @@ -16,6 +17,7 @@ class GuidingService(
private val messageService: MessageService,
private val phd2Client: PHD2Client,
private val guider: Guider,
private val mapper: ObjectMapper,
) : GuiderListener {

private val guideHistory = GuideStepHistory()
Expand All @@ -35,7 +37,7 @@ class GuidingService(

phd2Client.open(host, port)
guider.registerGuiderListener(this)
settle(preferenceService.getJSON<SettleInfo>("GUIDER.SETTLE_INFO") ?: SettleInfo.EMPTY)
settle(preferenceService["GUIDER.SETTLE_INFO"]?.ifBlank { null }?.let { mapper.readValue(it, SettleInfo::class.java) } ?: SettleInfo.EMPTY)
messageService.sendMessage(GuiderMessageEvent(GUIDER_CONNECTED))
}

Expand Down Expand Up @@ -81,7 +83,8 @@ class GuidingService(
guider.settleTime = Duration.ofSeconds(settle.time)
guider.settleTimeout = Duration.ofSeconds(settle.timeout)

preferenceService.putJSON("GUIDER.SETTLE_INFO", settle)
preferenceService["GUIDER.SETTLE_INFO"] = mapper.writeValueAsString(settle)
preferenceService.save()
}

fun dither(amount: Double, raOnly: Boolean = false) {
Expand Down
Loading

0 comments on commit 37329b1

Please sign in to comment.