concurrent_tor 1.0.0

A comprehensive scraping runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
use crate::{
    config::WorkerConfig,
    database::{JobCache, JobStatusDb, DB},
    execution::monitor::{DequeueInfo, Event, QueueJobInfo},
    Result,
};
use anyhow::anyhow;
use async_channel::{Receiver, Sender};
use dyn_clone::DynClone;
use futures_util::TryFutureExt;
use log::{debug, error, info, warn};
use serde::de::DeserializeOwned;
use std::{fmt::Debug, hash::Hash, sync::Arc};
use strum::IntoEnumIterator;
use tokio::{sync::Mutex, task::JoinHandle};

pub trait PlatformT:
    DeserializeOwned
    + Debug
    + Clone
    + Copy
    + Hash
    + Eq
    + PartialEq
    + Send
    + Sync
    + Unpin
    + 'static
    + IntoEnumIterator
{
    fn request_from_json(&self, json: &str) -> Result<Box<dyn WorkerRequest>>;
    fn to_repr(&self) -> usize;
    fn from_repr(repr: usize) -> Self;
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SpecificWorkerType {
    Http,
    HeadedBrowser,
    HeadlessBrowser,
}

#[derive(Debug, Clone)]
pub(crate) struct ChannelGroup<C, P>
where
    P: PlatformT,
{
    pub http: C,
    pub headless_browser: C,
    pub headed_browser: C,
    pub(crate) _platform: std::marker::PhantomData<P>,
}

pub type FromJsonFn = fn(&str) -> Result<Box<dyn WorkerRequest>>;

pub type JobHash = u128;

pub trait WorkerRequest: Send + Debug + DynClone {
    fn as_any(&self) -> &dyn std::any::Any;
    fn hash(&self) -> Result<JobHash>;
    fn as_json(&self) -> String;
}
dyn_clone::clone_trait_object!(WorkerRequest);

pub trait PlatformHistory {
    fn can_request(&self, now: quanta::Instant) -> PlatformCanRequest;
    fn request_complete(&mut self, now: quanta::Instant);
    fn reset(&mut self);
}

#[derive(Debug, Eq, PartialEq)]
pub enum PlatformCanRequest {
    Ok,
    MustWait,
    MaxRequests,
}

/// NotRequested means that the job has not been processed by a worker and can be processed.
#[derive(Debug)]
pub struct NotRequested;
/// Requested means that the job has been processed by a worker and cannot be processed until
/// the Platform reinitiates the request. Reinitiating the request is dependent on the platform's
/// logic.
#[derive(Debug)]
pub struct Requested;
#[derive(Debug)]
pub struct Job<RequestStatus, P: PlatformT> {
    pub platform: P,
    pub request: Box<dyn WorkerRequest>,
    pub num_attempts: u32,
    pub max_attempts: u32,

    _status: std::marker::PhantomData<RequestStatus>,
}

unsafe impl<P> Send for Job<NotRequested, P> where P: PlatformT {}
unsafe impl<P> Send for Job<Requested, P> where P: PlatformT {}
unsafe impl<P> Sync for Job<NotRequested, P> where P: PlatformT {}
unsafe impl<P> Sync for Job<Requested, P> where P: PlatformT {}

impl<P> Job<NotRequested, P>
where
    P: PlatformT,
{
    pub fn new(
        platform: P,
        request: Box<dyn WorkerRequest>,
        num_attempts: u32,
        max_attempts: u32,
    ) -> Self {
        Job {
            platform,
            request,
            num_attempts,
            max_attempts,
            _status: std::marker::PhantomData,
        }
    }

    pub fn init(platform: P, request: Box<dyn WorkerRequest>, max_attempts: u32) -> Self {
        Job {
            platform,
            request,
            num_attempts: 0,
            max_attempts,
            _status: std::marker::PhantomData,
        }
    }

    pub fn init_from_box<T: WorkerRequest + 'static>(
        platform: P,
        request: T,
        max_attempts: u32,
    ) -> Self {
        Job {
            platform,
            request: Box::new(request),
            num_attempts: 0,
            max_attempts,
            _status: std::marker::PhantomData,
        }
    }
}

impl<P> Job<Requested, P>
where
    P: PlatformT,
{
    pub(crate) fn to_not_requested(self) -> Job<NotRequested, P> {
        Job {
            platform: self.platform,
            request: self.request,
            num_attempts: self.num_attempts,
            max_attempts: self.max_attempts,
            _status: std::marker::PhantomData,
        }
    }
}

impl<P> From<&Job<NotRequested, P>> for Job<Requested, P>
where
    P: PlatformT,
{
    fn from(job: &Job<NotRequested, P>) -> Self {
        Job {
            platform: job.platform,
            request: job.request.clone(),
            num_attempts: job.num_attempts + 1,
            max_attempts: job.max_attempts,
            _status: std::marker::PhantomData,
        }
    }
}

#[derive(Debug)]
pub enum WorkerAction<P: PlatformT> {
    Job(Job<NotRequested, P>),
    StopProgram,
}

#[derive(Debug)]
pub enum QueueJob<P: PlatformT> {
    New(Job<NotRequested, P>),
    Completed(Job<Requested, P>),
    /// Cancel the job and do not retry.
    Failed(Job<Requested, P>),
    Retry(Job<Requested, P>),
    SendStopProgram,
    StopProgram,
}

impl<P> QueueJob<P>
where
    P: PlatformT,
{
    pub fn inner_job_info(&self) -> Result<Option<(u128, u32, u32)>> {
        Ok(Some(match self {
            QueueJob::New(job) => (job.request.hash()?, job.num_attempts, job.max_attempts),
            QueueJob::Completed(job) => (job.request.hash()?, job.num_attempts, job.max_attempts),
            QueueJob::Failed(job) => (job.request.hash()?, job.num_attempts, job.max_attempts),
            QueueJob::Retry(job) => (job.request.hash()?, job.num_attempts, job.max_attempts),
            _ => return Ok(None),
        }))
    }
}

pub trait Scheduler<P: PlatformT>: Send + 'static {
    fn enqueue(&mut self, job: Job<NotRequested, P>);
    fn enqueue_many(&mut self, jobs: Vec<Job<NotRequested, P>>) {
        for job in jobs {
            self.enqueue(job);
        }
    }
    fn dequeue(&mut self) -> Option<Job<NotRequested, P>>;
    fn size(&self) -> usize;
}

#[derive(Debug)]
pub(crate) enum QueueJobStatus {
    WorkerCompleted { worker_id: u16 },
    NewJobArrived,
    SendStopProgram,
    StopProgram,
}

pub(crate) struct JobDistributor<S: Scheduler<P>, P: PlatformT> {
    /// Monitor
    monitor: Sender<Event<P>>,
    db: DB,
    /// Receive signal from a worker to enqueue a job.
    request_job: Receiver<QueueJobStatus>,
    /// Send a signal to dequeue a job.
    notify_new_job: Sender<QueueJobStatus>,
    /// Receive job from a platform only.
    queue_job: Receiver<QueueJob<P>>,
    /// Send job to a worker only. HashMap because workers are Http or Browser based, not generic.
    dequeue_job: Vec<Sender<WorkerAction<P>>>,
    /// Used only to send stop signal to http workers.
    worker_rxs: ChannelGroup<Sender<WorkerAction<P>>, P>,
    scheduler: Arc<Mutex<S>>,
    worker_config: WorkerConfig,
}

impl<S, P> JobDistributor<S, P>
where
    S: Scheduler<P>,
    P: PlatformT,
{
    // TODO: Fix target circulation
    // TODO: Check if there are duplicate jobs in the queue - remove if so
    pub fn new(
        monitor: Sender<Event<P>>,
        db: DB,
        request_job: Receiver<QueueJobStatus>,
        notify_new_job: Sender<QueueJobStatus>,
        queue_job: Receiver<QueueJob<P>>,
        dequeue_job: Vec<Sender<WorkerAction<P>>>,
        worker_rxs: ChannelGroup<Sender<WorkerAction<P>>, P>,
        scheduler: S,
        worker_config: WorkerConfig,
    ) -> Self {
        JobDistributor {
            monitor,
            db,
            request_job,
            notify_new_job,
            queue_job,
            dequeue_job,
            worker_rxs,
            scheduler: Arc::new(Mutex::new(scheduler)),
            worker_config,
        }
    }

    pub(super) fn start(self) -> [JoinHandle<Result<()>>; 2] {
        let scheduler = self.scheduler.clone();
        let monitor_clone = self.monitor.clone();
        let enqueue_thread = tokio::task::spawn(async move {
            match Self::loop_enqueue(
                monitor_clone,
                self.db,
                self.queue_job,
                self.notify_new_job,
                scheduler,
            )
            .await
            {
                Ok(_) => info!("Enqueue loop stopped"),
                Err(e) => panic!("Enqueue loop stopped with error: {:?}", e),
            }
            Ok(())
        });
        let dequeue_thread = tokio::task::spawn(async move {
            match Self::loop_dequeue(
                self.monitor,
                self.request_job,
                self.dequeue_job,
                self.worker_rxs,
                self.scheduler,
                self.worker_config,
            )
            .await
            {
                Ok(_) => info!("Dequeue loop stopped"),
                Err(e) => panic!("Dequeue loop stopped with error: {:?}", e),
            }
            Ok(())
        });
        [enqueue_thread, dequeue_thread]
    }

    async fn loop_enqueue(
        monitor: Sender<Event<P>>,
        mut db: DB,
        queue_job: Receiver<QueueJob<P>>,
        notify_new_job: Sender<QueueJobStatus>,
        scheduler: Arc<Mutex<S>>,
    ) -> Result<()> {
        info!("Starting enqueue loop");

        // initialize the scheduler with what is in the database
        let jobs = JobCache::<P>::fetch_all_active(&mut db).await?;
        info!("Fetched {} active jobs from the database", jobs.len());
        for job in jobs {
            let request = job.platform.request_from_json(&job.request)?;
            let job = Job::new(
                job.platform,
                request,
                job.num_attempts as u32,
                job.max_attempts as u32,
            );
            monitor
                .send(Event::NewJob(QueueJobInfo::new(
                    job.platform,
                    quanta::Instant::now(),
                    job.request.hash()?,
                    JobStatusDb::Active,
                    job.num_attempts,
                    job.max_attempts,
                )))
                .await?;
            scheduler.lock().await.enqueue(job);
            notify_new_job
                .send(QueueJobStatus::NewJobArrived)
                .map_err(|e| {
                    anyhow!(
                        "Failed to send new job arrived signal in loop enqueue: {:?}",
                        e
                    )
                })
                .await?;

            // TODO: Handle duplicate jobs in the queue
        }
        let mut shutting_down = false;

        loop {
            let job = queue_job.recv().await?;
            match job {
                QueueJob::New(job) => {
                    let job_hash = job.request.hash()?;
                    match JobCache::insert_new(
                        &mut db,
                        job.num_attempts as i32,
                        job.max_attempts as i32,
                        job_hash,
                        job.platform.clone(),
                        &job.request.as_json(),
                    )
                    .await
                    {
                        Ok(_) => {}
                        Err(e) => {
                            error!("Failed to insert new job into database: {:?}", e);
                            continue;
                        }
                    }
                    if !shutting_down {
                        // TODO: Handle duplicate jobs in the queue
                        monitor
                            .send(Event::NewJob(QueueJobInfo::new(
                                job.platform,
                                quanta::Instant::now(),
                                job_hash,
                                JobStatusDb::Active, // behaviour of [insert_new]
                                job.num_attempts,
                                job.max_attempts,
                            )))
                            .await?;
                        scheduler.lock().await.enqueue(job);
                        notify_new_job
                            .send(QueueJobStatus::NewJobArrived)
                            .map_err(|e| {
                                anyhow!(
                                    "Failed to send new job arrived signal in loop enqueue: {:?}",
                                    e
                                )
                            })
                            .await?;
                    }
                }
                QueueJob::Completed(job) => {
                    let job_hash = job.request.hash()?;
                    JobCache::<P>::update_status_by_hash(
                        &mut db,
                        job_hash,
                        JobStatusDb::Completed,
                        job.num_attempts as i32,
                    )
                    .await?;
                    monitor
                        .send(Event::CompletedJob(QueueJobInfo::new(
                            job.platform,
                            quanta::Instant::now(),
                            job_hash,
                            JobStatusDb::Completed, // behaviour of [update_status_by_hash] in this context
                            job.num_attempts,
                            job.num_attempts,
                        )))
                        .await?;
                }
                QueueJob::Failed(job) => {
                    let job_hash = job.request.hash()?;
                    JobCache::<P>::update_status_by_hash(
                        &mut db,
                        job_hash,
                        JobStatusDb::Failed,
                        job.num_attempts as i32,
                    )
                    .await?;
                    monitor
                        .send(Event::FailedJob(QueueJobInfo::new(
                            job.platform,
                            quanta::Instant::now(),
                            job_hash,
                            JobStatusDb::Failed, // behaviour of [update_status_by_hash] in this context
                            job.num_attempts,
                            job.max_attempts,
                        )))
                        .await?;
                }
                QueueJob::Retry(job) => {
                    let job_hash = job.request.hash()?;
                    JobCache::<P>::update_status_by_hash(
                        &mut db,
                        job.request.hash()?,
                        JobStatusDb::Active,
                        job.num_attempts as i32,
                    )
                    .await?;
                    if !shutting_down {
                        if job.num_attempts >= job.max_attempts {
                            warn!(
                                "Job with hash {} has reached max attempts. Not retrying and setting \
                                status to failed.",
                                job_hash
                            );
                            monitor
                                .send(Event::MaxAttemptsReached(QueueJobInfo::new(
                                    job.platform,
                                    quanta::Instant::now(),
                                    job_hash,
                                    JobStatusDb::Active, // behaviour of [update_status_by_hash] in this context
                                    job.num_attempts,
                                    job.max_attempts,
                                )))
                                .await?;
                            JobCache::<P>::update_status_by_hash(
                                &mut db,
                                job_hash,
                                JobStatusDb::Failed,
                                job.num_attempts as i32,
                            )
                            .await?;
                        } else {
                            monitor
                                .send(Event::RetryJob(QueueJobInfo::new(
                                    job.platform,
                                    quanta::Instant::now(),
                                    job_hash,
                                    JobStatusDb::Active, // behaviour of [update_status_by_hash] in this context
                                    job.num_attempts,
                                    job.max_attempts,
                                )))
                                .await?;
                            scheduler.lock().await.enqueue(job.to_not_requested());
                            notify_new_job
                                .send(QueueJobStatus::NewJobArrived)
                                .map_err(|e| {
                                    anyhow!(
                                        "Failed to send new job arrived signal in loop enqueue: {:?}",
                                        e
                                    )
                                })
                                .await?;
                        }
                    }
                }
                QueueJob::SendStopProgram => {
                    info!("Sending stop signal to dequeue loop");
                    shutting_down = true;
                    notify_new_job
                        .send(QueueJobStatus::SendStopProgram)
                        .map_err(|e| {
                            anyhow!(
                                "Failed to send preliminary stop program signal in loop enqueue: {:?}",
                                e
                            )
                        })
                        .await?;
                }
                QueueJob::StopProgram => {
                    info!("Stopping enqueue loop");
                    notify_new_job
                        .send(QueueJobStatus::StopProgram)
                        .map_err(|e| {
                            anyhow!(
                                "Failed to send stop program signal in loop enqueue: {:?}",
                                e
                            )
                        })
                        .await?;
                    break;
                }
            }
        }
        info!("Stopping enqueue loop");
        Ok(())
    }

    async fn loop_dequeue(
        monitor: Sender<Event<P>>,
        request_job: Receiver<QueueJobStatus>,
        dequeue_job: Vec<Sender<WorkerAction<P>>>,
        worker_rxs: ChannelGroup<Sender<WorkerAction<P>>, P>,
        scheduler: Arc<Mutex<S>>,
        worker_config: WorkerConfig,
    ) -> Result<()> {
        debug!("Starting dequeue loop");
        // Negative circulation means that we are waiting for jobs to arrive.
        // Zero circulation means that we are in equilibrium and all workers are busy.
        // Positive circulation means that we are waiting for workers to complete jobs in the channel.
        let mut current_circulation = -(worker_config.target_circulation as i32);
        let mut shutting_down = false;
        loop {
            let status = request_job.recv().await?;
            match status {
                QueueJobStatus::NewJobArrived => {
                    if !shutting_down {
                        Self::balance_circulation(
                            worker_config.target_circulation as i32,
                            &mut current_circulation,
                            &scheduler,
                            &worker_rxs,
                            &monitor,
                            &dequeue_job,
                        )
                        .await?;
                    }
                }
                QueueJobStatus::WorkerCompleted {
                    worker_id: _worker_id,
                } => {
                    current_circulation -= 1;
                    if !shutting_down {
                        Self::balance_circulation(
                            worker_config.target_circulation as i32,
                            &mut current_circulation,
                            &scheduler,
                            &worker_rxs,
                            &monitor,
                            &dequeue_job,
                        )
                        .await?;
                    }
                }
                QueueJobStatus::SendStopProgram => {
                    info!("Sending stop signal to http workers");
                    for _http_platform in 0..worker_config.http_workers {
                        worker_rxs
                            .http
                            .send(WorkerAction::StopProgram)
                            .map_err(|e| {
                                anyhow!("Failed to send stop program to http workers: {:?}", e)
                            })
                            .await?;
                    }
                    info!("Sending stop signal to headless browser workers");
                    for _browser_platform in 0..worker_config.headless_browser_workers {
                        worker_rxs
                            .headless_browser
                            .send(WorkerAction::StopProgram)
                            .map_err(|e| {
                                anyhow!("Failed to send stop program to browser workers: {:?}", e)
                            })
                            .await?;
                    }
                    info!("Sending stop signal to headed browser workers");
                    for _browser_platform in 0..worker_config.headed_browser_workers {
                        worker_rxs
                            .headed_browser
                            .send(WorkerAction::StopProgram)
                            .map_err(|e| {
                                anyhow!("Failed to send stop program to browser workers: {:?}", e)
                            })
                            .await?;
                    }
                    info!("Sent all stop signals to workers");
                    shutting_down = true;
                }
                QueueJobStatus::StopProgram => {
                    break;
                }
            }
        }
        info!("Stopping dequeue loop");
        Ok(())
    }

    async fn balance_circulation(
        target_circulation: i32,
        circulation: &mut i32,
        scheduler: &Arc<Mutex<S>>,
        worker_rxs: &ChannelGroup<Sender<WorkerAction<P>>, P>,
        monitor: &Sender<Event<P>>,
        dequeue_job: &Vec<Sender<WorkerAction<P>>>,
    ) -> Result<()> {
        monitor
            .send(Event::BalanceCirculation(DequeueInfo::new(
                *circulation,
                scheduler.lock().await.size(),
                worker_rxs.http.len(),
                worker_rxs.headless_browser.len(),
                worker_rxs.headed_browser.len(),
                quanta::Instant::now(),
            )))
            .await?;
        if *circulation < target_circulation {
            if let Some(job) = scheduler.lock().await.dequeue() {
                let sender = &dequeue_job[job.platform.to_repr()];
                sender
                    .send(WorkerAction::Job(job))
                    .map_err(|e| anyhow!("Failed to send job to worker in dequeue loop: {:?}", e))
                    .await?;
                *circulation += 1;
            }
        }
        Ok(())
    }
}

pub struct SimpleScheduler<P: PlatformT> {
    queue: Vec<Job<NotRequested, P>>,
}

impl<P> SimpleScheduler<P>
where
    P: PlatformT,
{
    pub fn new() -> Self {
        SimpleScheduler { queue: Vec::new() }
    }
}

impl<P> Scheduler<P> for SimpleScheduler<P>
where
    P: PlatformT,
{
    fn enqueue(&mut self, job: Job<NotRequested, P>) {
        self.queue.push(job);
    }
    fn enqueue_many(&mut self, jobs: Vec<Job<NotRequested, P>>) {
        self.queue.extend(jobs);
    }
    fn dequeue(&mut self) -> Option<Job<NotRequested, P>> {
        self.queue.pop()
    }
    fn size(&self) -> usize {
        self.queue.len()
    }
}