2025-02-10 14:49:30 +01:00
|
|
|
import pkg/chronos
|
2025-02-11 15:33:40 +01:00
|
|
|
import pkg/chronicles
|
2025-02-10 14:49:30 +01:00
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
|
|
|
|
import ./config
|
2025-02-10 15:34:41 +01:00
|
|
|
import ./utils/asyncdataevent
|
|
|
|
|
import ./types
|
2025-02-10 14:49:30 +01:00
|
|
|
|
2025-02-11 15:33:40 +01:00
|
|
|
logScope:
|
|
|
|
|
topics = "state"
|
|
|
|
|
|
2025-02-10 14:49:30 +01:00
|
|
|
type
|
2025-02-11 16:31:23 +01:00
|
|
|
OnStep* = proc(): Future[?!void] {.async: (raises: []), gcsafe.}
|
2025-02-10 15:34:41 +01:00
|
|
|
|
|
|
|
|
DhtNodeCheckEventData* = object
|
|
|
|
|
id*: Nid
|
|
|
|
|
isOk*: bool
|
|
|
|
|
|
|
|
|
|
Events* = ref object
|
|
|
|
|
nodesFound*: AsyncDataEvent[seq[Nid]]
|
|
|
|
|
newNodesDiscovered*: AsyncDataEvent[seq[Nid]]
|
|
|
|
|
dhtNodeCheck*: AsyncDataEvent[DhtNodeCheckEventData]
|
|
|
|
|
nodesExpired*: AsyncDataEvent[seq[Nid]]
|
|
|
|
|
|
2025-02-11 15:33:40 +01:00
|
|
|
ApplicationStatus* {.pure.} = enum
|
|
|
|
|
Stopped
|
|
|
|
|
Stopping
|
|
|
|
|
Running
|
|
|
|
|
|
2025-02-11 10:54:58 +01:00
|
|
|
State* = ref object of RootObj
|
2025-02-11 15:33:40 +01:00
|
|
|
status*: ApplicationStatus
|
2025-02-10 14:49:30 +01:00
|
|
|
config*: Config
|
2025-02-10 16:24:54 +01:00
|
|
|
events*: Events
|
2025-02-10 14:49:30 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
method whileRunning*(s: State, step: OnStep, delay: Duration) {.async, base.} =
|
2025-02-11 15:33:40 +01:00
|
|
|
proc worker(): Future[void] {.async.} =
|
|
|
|
|
while s.status == ApplicationStatus.Running:
|
|
|
|
|
if err =? (await step()).errorOption:
|
|
|
|
|
error "Failure-result caught in main loop. Stopping...", err = err.msg
|
|
|
|
|
s.status = ApplicationStatus.Stopping
|
|
|
|
|
await sleepAsync(delay)
|
|
|
|
|
|
|
|
|
|
asyncSpawn worker()
|