90 lines
2.4 KiB
Nim
Raw Normal View History

2025-02-05 16:06:04 +01:00
import std/os
import pkg/chronicles
import pkg/chronos
import pkg/questionable
import pkg/questionable/results
2025-02-05 16:35:02 +01:00
import pkg/metrics
2025-02-05 16:06:04 +01:00
import ./config
2025-02-10 13:54:59 +01:00
import ./utils/logging
2025-02-10 15:34:41 +01:00
import ./utils/asyncdataevent
2025-02-10 14:49:30 +01:00
import ./installer
import ./state
import ./component
2025-02-10 15:34:41 +01:00
import ./types
2025-02-05 16:06:04 +01:00
2025-02-11 15:33:40 +01:00
type Application* = ref object
state: State
2025-02-12 14:12:21 +01:00
components: seq[Component]
2025-02-05 16:06:04 +01:00
2025-06-02 16:16:41 +02:00
proc initializeApp(
app: Application, config: Config
): Future[?!void] {.async: (raises: [CancelledError]).} =
2025-02-12 14:12:21 +01:00
app.state = State(
2025-02-11 15:33:40 +01:00
status: ApplicationStatus.Running,
config: config,
2025-02-10 15:34:41 +01:00
events: Events(
nodesFound: newAsyncDataEvent[seq[Nid]](),
newNodesDiscovered: newAsyncDataEvent[seq[Nid]](),
dhtNodeCheck: newAsyncDataEvent[DhtNodeCheckEventData](),
nodesToRevisit: newAsyncDataEvent[seq[Nid]](),
nodesDeleted: newAsyncDataEvent[seq[Nid]](),
2025-02-10 15:34:41 +01:00
),
2025-02-10 14:49:30 +01:00
)
2025-02-12 14:12:21 +01:00
without components =? (await createComponents(app.state)), err:
2025-02-10 16:24:54 +01:00
error "Failed to create componenents", err = err.msg
return failure(err)
2025-02-12 14:12:21 +01:00
app.components = components
2025-02-10 16:24:54 +01:00
for c in components:
if err =? (await c.awake()).errorOption:
error "Failed during component awake", err = err.msg
return failure(err)
2025-02-10 14:49:30 +01:00
for c in components:
2025-02-10 16:24:54 +01:00
if err =? (await c.start()).errorOption:
error "Failed during component start", err = err.msg
return failure(err)
2025-02-10 14:49:30 +01:00
2025-02-05 16:35:02 +01:00
return success()
2025-06-02 14:30:28 +02:00
proc stopComponents(app: Application) {.async: (raises: [CancelledError]).} =
2025-02-12 14:12:21 +01:00
for c in app.components:
if err =? (await c.stop()).errorOption:
error "Failed to stop component", err = err.msg
2025-02-05 16:35:02 +01:00
proc stop*(app: Application) =
2025-02-11 15:33:40 +01:00
app.state.status = ApplicationStatus.Stopping
2025-02-05 16:06:04 +01:00
proc run*(app: Application) =
2025-02-11 15:33:40 +01:00
let config = parseConfig()
info "Loaded configuration", config = $config
2025-02-05 16:06:04 +01:00
# Configure loglevel
2025-02-11 15:33:40 +01:00
updateLogLevel(config.logLevel)
2025-02-05 16:06:04 +01:00
# Ensure datadir path exists:
2025-02-11 15:33:40 +01:00
if not existsDir(config.dataDir):
createDir(config.dataDir)
2025-02-05 16:06:04 +01:00
info "Metrics endpoint initialized"
info "Starting application"
2025-02-11 15:33:40 +01:00
if err =? (waitFor app.initializeApp(config)).errorOption:
app.state.status = ApplicationStatus.Stopping
2025-02-05 16:06:04 +01:00
error "Failed to start application", err = err.msg
2025-02-05 16:35:02 +01:00
return
2025-02-05 16:06:04 +01:00
2025-02-11 15:33:40 +01:00
while app.state.status == ApplicationStatus.Running:
2025-02-05 16:06:04 +01:00
try:
chronos.poll()
except Exception as exc:
error "Unhandled exception", msg = exc.msg
quit QuitFailure
2025-02-12 14:12:21 +01:00
notice "Application stopping..."
waitFor app.stopComponents()
notice "Application stopped"