@@ -532,19 +532,34 @@ public function process_queue($verbose = false) {
532
532
533
533
// Get an instance of the currently configured lock_factory.
534
534
$ lockfactory = \core \lock \lock_config::get_lock_factory ('tool_crawler_process_queue ' );
535
-
535
+ if ($ config ->coursemode == 1 || $ config ->uselogs == 1 ) {
536
+ if (empty ($ allowedcourses )) {
537
+ return true ;
538
+ }
539
+ $ courselockfactory = \core \lock \lock_config::get_lock_factory ('tool_crawler_process_course_queue ' );
540
+ $ courseid = 0 ;
541
+ foreach ($ allowedcourses as $ id ) {
542
+ if ($ courselock = $ courselockfactory ->get_lock ($ id , 0 )) {
543
+ $ courseid = $ id ;
544
+ break ;
545
+ }
546
+ }
547
+ // No unlocked course.
548
+ if (!$ courseid ) {
549
+ $ courseid = reset ($ allowedcourses );
550
+ }
551
+ }
536
552
// While we are not exceeding the maxcron time, and the queue is not empty.
537
553
while (time () < $ cronstop ) {
538
554
if (empty ($ nodes )) {
539
555
// Grab a list of items from the front of the queue. We need the first 1000
540
556
// in case other workers are already locked and processing items at the front of the queue.
541
557
// We try each queue item until we find an available one.
542
558
if ($ config ->coursemode == 1 || $ config ->uselogs == 1 ) {
543
- if (empty ( $ allowedcourses ) ) {
559
+ if (! $ courseid ) {
544
560
return true ;
545
561
}
546
- // Only process first course in the queue.
547
- $ courseid = reset ($ allowedcourses );
562
+
548
563
$ params = ['courseid ' => $ courseid ];
549
564
$ sql = "SELECT *
550
565
FROM {tool_crawler_url}
@@ -556,6 +571,9 @@ public function process_queue($verbose = false) {
556
571
id ASC " ;
557
572
$ nodes = $ DB ->get_records_sql ($ sql , $ params , 0 , 1000 );
558
573
if (empty ($ nodes )) {
574
+ if ($ courselock ) {
575
+ $ courselock ->release ();
576
+ }
559
577
\tool_crawler \helper::finish_course_crawling ($ courseid );
560
578
return true ;
561
579
}
@@ -600,11 +618,17 @@ public function process_queue($verbose = false) {
600
618
try {
601
619
$ this ->crawl ($ node , $ verbose );
602
620
} catch (\Throwable $ e ) {
621
+ if ($ courselock ) {
622
+ $ courselock ->release ();
623
+ }
603
624
throw $ e ;
604
625
} finally {
605
626
$ lock ->release ();
606
627
}
607
628
}
629
+ if ($ courselock ) {
630
+ $ courselock ->release ();
631
+ }
608
632
set_config ('crawltick ' , time (), 'tool_crawler ' );
609
633
return false ;
610
634
}
0 commit comments