mirror of
https://github.com/logos-storage/logos-storage-network-crawler.git
synced 2026-01-02 13:33:08 +00:00
81 lines
2.1 KiB
Nim
81 lines
2.1 KiB
Nim
import std/os
|
|
import pkg/chronicles
|
|
import pkg/chronos
|
|
import pkg/questionable
|
|
import pkg/questionable/results
|
|
|
|
import pkg/metrics
|
|
|
|
import ./config
|
|
import ./utils/logging
|
|
import ./utils/asyncdataevent
|
|
import ./installer
|
|
import ./state
|
|
import ./component
|
|
import ./types
|
|
|
|
type Application* = ref object
|
|
state: State
|
|
components: seq[Component]
|
|
|
|
proc initializeApp(app: Application, config: Config): Future[?!void] {.async.} =
|
|
app.state = State(
|
|
status: ApplicationStatus.Running,
|
|
config: config,
|
|
events: Events(
|
|
nodesFound: newAsyncDataEvent[seq[Nid]](),
|
|
newNodesDiscovered: newAsyncDataEvent[seq[Nid]](),
|
|
dhtNodeCheck: newAsyncDataEvent[DhtNodeCheckEventData](),
|
|
nodesExpired: newAsyncDataEvent[seq[Nid]](),
|
|
),
|
|
)
|
|
|
|
without components =? (await createComponents(app.state)), err:
|
|
error "Failed to create componenents", err = err.msg
|
|
return failure(err)
|
|
app.components = components
|
|
|
|
for c in components:
|
|
if err =? (await c.start()).errorOption:
|
|
error "Failed to start component", err = err.msg
|
|
|
|
return success()
|
|
|
|
proc stopComponents(app: Application) {.async.} =
|
|
for c in app.components:
|
|
if err =? (await c.stop()).errorOption:
|
|
error "Failed to stop component", err = err.msg
|
|
|
|
proc stop*(app: Application) =
|
|
app.state.status = ApplicationStatus.Stopping
|
|
|
|
proc run*(app: Application) =
|
|
let config = parseConfig()
|
|
info "Loaded configuration", config = $config
|
|
|
|
# Configure loglevel
|
|
updateLogLevel(config.logLevel)
|
|
|
|
# Ensure datadir path exists:
|
|
if not existsDir(config.dataDir):
|
|
createDir(config.dataDir)
|
|
|
|
info "Metrics endpoint initialized"
|
|
|
|
info "Starting application"
|
|
if err =? (waitFor app.initializeApp(config)).errorOption:
|
|
app.state.status = ApplicationStatus.Stopping
|
|
error "Failed to start application", err = err.msg
|
|
return
|
|
|
|
while app.state.status == ApplicationStatus.Running:
|
|
try:
|
|
chronos.poll()
|
|
except Exception as exc:
|
|
error "Unhandled exception", msg = exc.msg
|
|
quit QuitFailure
|
|
|
|
notice "Application stopping..."
|
|
waitFor app.stopComponents()
|
|
notice "Application stopped"
|