mirror of
https://github.com/logos-storage/logos-storage-network-crawler.git
synced 2026-01-02 21:43:11 +00:00
44 lines
1.1 KiB
Nim
44 lines
1.1 KiB
Nim
import pkg/chronos
|
|
import pkg/chronicles
|
|
import pkg/questionable/results
|
|
|
|
import ./config
|
|
import ./utils/asyncdataevent
|
|
import ./types
|
|
|
|
logScope:
|
|
topics = "state"
|
|
|
|
type
|
|
OnStep* = proc(): Future[?!void] {.async: (raises: []), gcsafe.}
|
|
|
|
DhtNodeCheckEventData* = object
|
|
id*: Nid
|
|
isOk*: bool
|
|
|
|
Events* = ref object
|
|
nodesFound*: AsyncDataEvent[seq[Nid]]
|
|
newNodesDiscovered*: AsyncDataEvent[seq[Nid]]
|
|
dhtNodeCheck*: AsyncDataEvent[DhtNodeCheckEventData]
|
|
nodesExpired*: AsyncDataEvent[seq[Nid]]
|
|
|
|
ApplicationStatus* {.pure.} = enum
|
|
Stopped
|
|
Stopping
|
|
Running
|
|
|
|
State* = ref object of RootObj
|
|
status*: ApplicationStatus
|
|
config*: Config
|
|
events*: Events
|
|
|
|
method whileRunning*(s: State, step: OnStep, delay: Duration) {.async, base.} =
|
|
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()
|