Skip to content

Commit f00a854

Browse files
author
Nathan Nguyen
committed
Allow running course in parallel
1 parent 4390c95 commit f00a854

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

classes/robot/crawler.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,19 +532,34 @@ public function process_queue($verbose = false) {
532532

533533
// Get an instance of the currently configured lock_factory.
534534
$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+
}
536552
// While we are not exceeding the maxcron time, and the queue is not empty.
537553
while (time() < $cronstop) {
538554
if (empty($nodes)) {
539555
// Grab a list of items from the front of the queue. We need the first 1000
540556
// in case other workers are already locked and processing items at the front of the queue.
541557
// We try each queue item until we find an available one.
542558
if ($config->coursemode == 1 || $config->uselogs == 1) {
543-
if (empty($allowedcourses)) {
559+
if (!$courseid) {
544560
return true;
545561
}
546-
// Only process first course in the queue.
547-
$courseid = reset($allowedcourses);
562+
548563
$params = ['courseid' => $courseid];
549564
$sql = "SELECT *
550565
FROM {tool_crawler_url}
@@ -556,6 +571,9 @@ public function process_queue($verbose = false) {
556571
id ASC";
557572
$nodes = $DB->get_records_sql($sql, $params, 0, 1000);
558573
if (empty($nodes)) {
574+
if ($courselock) {
575+
$courselock->release();
576+
}
559577
\tool_crawler\helper::finish_course_crawling($courseid);
560578
return true;
561579
}
@@ -600,11 +618,17 @@ public function process_queue($verbose = false) {
600618
try {
601619
$this->crawl($node, $verbose);
602620
} catch (\Throwable $e) {
621+
if ($courselock) {
622+
$courselock->release();
623+
}
603624
throw $e;
604625
} finally {
605626
$lock->release();
606627
}
607628
}
629+
if ($courselock) {
630+
$courselock->release();
631+
}
608632
set_config('crawltick', time(), 'tool_crawler');
609633
return false;
610634
}

0 commit comments

Comments
 (0)