11use std:: sync:: atomic:: Ordering ;
2- use std:: thread;
32use std:: time:: Duration ;
3+ use std:: { io, thread} ;
44
55use hashbrown:: HashMap ;
66use http:: Uri ;
@@ -13,13 +13,15 @@ use crate::crawler::types::{
1313 Command , CommandSender , Config , LocalId , Status , StatusReceiver , WorkerId ,
1414} ;
1515use crate :: crawler:: worker:: { Worker , WorkerError } ;
16- use crate :: types:: { Cursor , MessageSender , RequestCrawlReceiver } ;
16+ use crate :: types:: { MessageSender , RequestCrawlReceiver } ;
1717
1818const CAPACITY : usize = 1024 ;
1919const SLEEP : Duration = Duration :: from_millis ( 10 ) ;
2020
2121#[ derive( Debug , Error ) ]
2222pub enum ManagerError {
23+ #[ error( "spawn error: {0}" ) ]
24+ SpawnError ( #[ from] io:: Error ) ,
2325 #[ error( "worker error: {0}" ) ]
2426 WorkerError ( #[ from] WorkerError ) ,
2527 #[ error( "rtrb error: {0}" ) ]
@@ -50,16 +52,18 @@ impl Manager {
5052 let ( status_tx, status_rx) =
5153 magnetic:: mpsc:: mpsc_queue ( DynamicBufferP2 :: new ( CAPACITY ) . unwrap ( ) ) ;
5254 let workers = ( 0 ..n_workers)
53- . map ( |worker_id| {
55+ . map ( |worker_id| -> Result < _ , ManagerError > {
5456 let message_tx = message_tx. clone ( ) ;
5557 let status_tx = status_tx. clone ( ) ;
5658 let ( command_tx, command_rx) = rtrb:: RingBuffer :: new ( CAPACITY ) ;
57- let thread_handle = thread:: spawn ( move || {
58- Worker :: new ( WorkerId ( worker_id) , message_tx, status_tx, command_rx) . run ( )
59- } ) ;
60- WorkerHandle { configs : Vec :: new ( ) , command_tx, thread_handle }
59+ let thread_handle = thread:: Builder :: new ( )
60+ . name ( format ! ( "rsky-crawl-{worker_id}" ) )
61+ . spawn ( move || {
62+ Worker :: new ( WorkerId ( worker_id) , message_tx, status_tx, command_rx) . run ( )
63+ } ) ?;
64+ Ok ( WorkerHandle { configs : Vec :: new ( ) , command_tx, thread_handle } )
6165 } )
62- . collect :: < Vec < _ > > ( ) ;
66+ . collect :: < Result < Vec < _ > , _ > > ( ) ? ;
6367 Ok ( Self {
6468 workers : workers. into_boxed_slice ( ) ,
6569 next_id : WorkerId ( 0 ) ,
@@ -73,6 +77,8 @@ impl Manager {
7377 while self . update ( ) ? {
7478 thread:: sleep ( SLEEP ) ;
7579 }
80+ tracing:: info!( "shutting down crawler" ) ;
81+ SHUTDOWN . store ( true , Ordering :: Relaxed ) ;
7682 self . shutdown ( )
7783 }
7884
@@ -88,8 +94,7 @@ impl Manager {
8894 Ok ( ( ) )
8995 }
9096
91- fn handle_status ( & mut self , status : Status ) -> Result < bool , ManagerError > {
92- match status { }
97+ fn handle_status ( & mut self , _status : Status ) -> Result < bool , ManagerError > {
9398 Ok ( true )
9499 }
95100
@@ -108,7 +113,7 @@ impl Manager {
108113 if !self . configs . contains_key ( & request_crawl. uri ) {
109114 let config = Config {
110115 uri : request_crawl. uri . clone ( ) ,
111- cursor : Cursor ( 0 ) ,
116+ hostname : request_crawl . hostname . clone ( ) ,
112117 worker_id : self . next_id ,
113118 local_id : LocalId ( self . workers [ self . next_id . 0 ] . configs . len ( ) ) ,
114119 } ;
0 commit comments