2025-02-07 13:57:57 +01:00
|
|
|
import pkg/chronicles
|
|
|
|
|
import pkg/chronos
|
2025-02-07 14:51:03 +01:00
|
|
|
import pkg/questionable
|
|
|
|
|
import pkg/questionable/results
|
2025-02-07 13:57:57 +01:00
|
|
|
|
2025-02-12 13:25:37 +01:00
|
|
|
import ../services/dht
|
|
|
|
|
import ./todolist
|
2025-02-10 14:49:30 +01:00
|
|
|
import ../config
|
2025-02-10 15:34:41 +01:00
|
|
|
import ../types
|
2025-02-12 13:25:37 +01:00
|
|
|
import ../component
|
2025-02-10 15:34:41 +01:00
|
|
|
import ../state
|
|
|
|
|
import ../utils/asyncdataevent
|
2025-02-07 13:57:57 +01:00
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
|
topics = "crawler"
|
|
|
|
|
|
2025-02-10 14:49:30 +01:00
|
|
|
type Crawler* = ref object of Component
|
2025-02-12 13:25:37 +01:00
|
|
|
state: State
|
2025-02-07 13:57:57 +01:00
|
|
|
dht: Dht
|
2025-02-12 13:25:37 +01:00
|
|
|
todo: TodoList
|
2025-02-07 14:51:03 +01:00
|
|
|
|
2025-02-12 13:25:37 +01:00
|
|
|
proc raiseCheckEvent(c: Crawler, nid: Nid, success: bool): Future[?!void] {.async: (raises: []).} =
|
|
|
|
|
let event = DhtNodeCheckEventData(
|
|
|
|
|
id: nid,
|
|
|
|
|
isOk: success
|
|
|
|
|
)
|
|
|
|
|
if err =? (await c.state.events.dhtNodeCheck.fire(event)).errorOption:
|
|
|
|
|
return failure(err)
|
|
|
|
|
return success()
|
2025-02-07 15:35:40 +01:00
|
|
|
|
2025-02-12 13:25:37 +01:00
|
|
|
proc step(c: Crawler): Future[?!void] {.async: (raises: []).} =
|
|
|
|
|
without nid =? (await c.todo.pop()), err:
|
|
|
|
|
return failure(err)
|
2025-02-07 14:51:03 +01:00
|
|
|
|
2025-02-12 13:25:37 +01:00
|
|
|
without response =? await c.dht.getNeighbors(nid), err:
|
|
|
|
|
return failure(err)
|
2025-02-07 14:51:03 +01:00
|
|
|
|
2025-02-12 13:25:37 +01:00
|
|
|
if err =? (await c.raiseCheckEvent(nid, response.isResponsive)).errorOption:
|
|
|
|
|
return failure(err)
|
2025-02-07 14:51:03 +01:00
|
|
|
|
2025-02-12 13:25:37 +01:00
|
|
|
if err =? (await c.state.events.nodesFound.fire(response.nodeIds)).errorOption:
|
|
|
|
|
return failure(err)
|
2025-02-07 15:35:40 +01:00
|
|
|
|
2025-02-12 13:25:37 +01:00
|
|
|
return success()
|
2025-02-07 14:51:03 +01:00
|
|
|
|
2025-02-12 13:25:37 +01:00
|
|
|
method start*(c: Crawler): Future[?!void] {.async.} =
|
|
|
|
|
info "Starting crawler..."
|
2025-02-10 12:56:09 +01:00
|
|
|
|
2025-02-12 13:25:37 +01:00
|
|
|
proc onStep(): Future[?!void] {.async: (raises: []), gcsafe.} =
|
|
|
|
|
await c.step()
|
|
|
|
|
await c.state.whileRunning(onStep, c.state.config.stepDelayMs.milliseconds)
|
2025-02-07 14:51:03 +01:00
|
|
|
|
|
|
|
|
return success()
|
2025-02-07 13:57:57 +01:00
|
|
|
|
2025-02-10 15:34:41 +01:00
|
|
|
method stop*(c: Crawler): Future[?!void] {.async.} =
|
2025-02-10 14:49:30 +01:00
|
|
|
return success()
|
|
|
|
|
|
2025-02-07 14:51:03 +01:00
|
|
|
proc new*(
|
2025-02-07 16:19:26 +01:00
|
|
|
T: type Crawler,
|
2025-02-12 13:25:37 +01:00
|
|
|
state: State,
|
2025-02-07 16:19:26 +01:00
|
|
|
dht: Dht,
|
2025-02-12 13:25:37 +01:00
|
|
|
todo: TodoList
|
2025-02-07 14:51:03 +01:00
|
|
|
): Crawler =
|
2025-02-10 15:34:41 +01:00
|
|
|
Crawler(
|
2025-02-12 13:25:37 +01:00
|
|
|
state: state,
|
2025-02-10 15:34:41 +01:00
|
|
|
dht: dht,
|
2025-02-12 13:25:37 +01:00
|
|
|
todo: todo
|
2025-02-10 15:34:41 +01:00
|
|
|
)
|