2025-02-07 16:48:47 +01:00
|
|
|
import pkg/chronicles
|
|
|
|
|
import pkg/chronos
|
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
import ./nodestore
|
2025-02-10 14:49:30 +01:00
|
|
|
import ../component
|
2025-02-10 15:34:41 +01:00
|
|
|
import ../state
|
2025-02-11 16:31:23 +01:00
|
|
|
import ../types
|
|
|
|
|
import ../utils/asyncdataevent
|
2025-02-07 16:48:47 +01:00
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
|
topics = "timetracker"
|
|
|
|
|
|
2025-02-10 14:49:30 +01:00
|
|
|
type TimeTracker* = ref object of Component
|
2025-02-11 16:31:23 +01:00
|
|
|
state: State
|
|
|
|
|
nodestore: NodeStore
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
proc step(t: TimeTracker): Future[?!void] {.async: (raises: []).} =
|
|
|
|
|
let expiry =
|
|
|
|
|
(Moment.now().epochSeconds - (t.state.config.revisitDelayMins * 60)).uint64
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
var expired = newSeq[Nid]()
|
|
|
|
|
proc checkNode(item: NodeEntry): Future[?!void] {.async: (raises: []), gcsafe.} =
|
|
|
|
|
if item.lastVisit < expiry:
|
|
|
|
|
expired.add(item.id)
|
|
|
|
|
return success()
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
?await t.nodestore.iterateAll(checkNode)
|
|
|
|
|
?await t.state.events.nodesExpired.fire(expired)
|
|
|
|
|
return success()
|
2025-02-10 12:56:09 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
method start*(t: TimeTracker): Future[?!void] {.async.} =
|
|
|
|
|
info "Starting timetracker..."
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
proc onStep(): Future[?!void] {.async: (raises: []), gcsafe.} =
|
|
|
|
|
await t.step()
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
var delay = t.state.config.revisitDelayMins div 100
|
|
|
|
|
if delay < 1:
|
|
|
|
|
delay = 1
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
await t.state.whileRunning(onStep, delay.minutes)
|
2025-02-07 16:48:47 +01:00
|
|
|
return success()
|
|
|
|
|
|
2025-02-10 15:34:41 +01:00
|
|
|
method stop*(t: TimeTracker): Future[?!void] {.async.} =
|
2025-02-10 14:49:30 +01:00
|
|
|
return success()
|
|
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
proc new*(T: type TimeTracker, state: State, nodestore: NodeStore): TimeTracker =
|
|
|
|
|
TimeTracker(state: state, nodestore: nodestore)
|