diff --git a/code/jobs/CheckExternalLinksJob.php b/code/jobs/CheckExternalLinksJob.php index 56dd1e3..bb79e67 100644 --- a/code/jobs/CheckExternalLinksJob.php +++ b/code/jobs/CheckExternalLinksJob.php @@ -21,20 +21,14 @@ class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob { } /** - * Check a individual page + * Check an individual page */ public function process() { $task = new CheckExternalLinks(); - $pages = Versioned::get_by_stage('SiteTree', 'Live'); - // set the limit so each page is done individually - $task->limit = 1; - $this->totalSteps = $pages->count(); - foreach ($pages as $page) { - $this->currentStep++; - $task->run(); - } - $this->isComplete = true; - return; + $track = $task->runLinksCheck(1); + $this->currentStep = $track->CompletedPages; + $this->totalSteps = $track->TotalPages; + $this->isComplete = $track->Status === 'Completed'; } } diff --git a/code/tasks/CheckExternalLinks.php b/code/tasks/CheckExternalLinks.php index f3df02c..e9e0cc0 100644 --- a/code/tasks/CheckExternalLinks.php +++ b/code/tasks/CheckExternalLinks.php @@ -13,6 +13,16 @@ class CheckExternalLinks extends BuildTask { private $totalPages; function run($request) { + $this->runLinksCheck($this->limit); + } + + /** + * Runs the links checker and returns the track used + * + * @param int $limit Limit to number of pages to run + * @return BrokenExternalPageTrackStatus + */ + public function runLinksCheck($limit) { $track = CheckExternalLinks::getLatestTrack(); // if the script has already been started @@ -21,10 +31,10 @@ class CheckExternalLinks extends BuildTask { ->filter(array( 'TrackID' => $track->ID, 'Processed' => 0 - ))->limit($this->limit)->column('PageID'); + ))->limit($limit)->column('PageID'); $pages = Versioned::get_by_stage('SiteTree', 'Live') ->filter('ID', $batch) - ->limit($this->limit); + ->limit($limit); $this->updateJobInfo('Fetching pages to check'); if ($track->CompletedPages == $track->TotalPages) { $track->Status = 'Completed'; @@ -51,7 +61,7 @@ class CheckExternalLinks extends BuildTask { $batch = BrokenExternalPageTrack::get() ->filter(array( 'TrackID' => $track->ID - ))->limit($this->limit)->column('PageID'); + ))->limit($limit)->column('PageID'); $pages = Versioned::get_by_stage('SiteTree', 'Live') ->filter('ID', $batch); @@ -159,22 +169,15 @@ class CheckExternalLinks extends BuildTask { foreach ($rows as $row) { $row->delete(); } - } else { - // if running via the queued job module return to the queued job after each iteration - if ($this->limit == 1) { - return; - } else { - $this->updateJobInfo("Running next batch {$track->CompletedPages}/{$track->TotalPages}"); - $this->run($request); - } + return $track; } - // run this again if queued jobs exists and is a valid int - $queuedJob = Config::inst()->get('CheckExternalLinks', 'Delay'); - if (isset($queuedJob) && is_int($queuedJob) && class_exists('QueuedJobService')) { - $checkLinks = new CheckExternalLinksJob(); - singleton('QueuedJobService') - ->queueJob($checkLinks, date('Y-m-d H:i:s', time() + $queuedJob)); + // if running via the queued job module return to the queued job after each iteration + if ($limit == 1) { + return $track; + } else { + $this->updateJobInfo("Running next batch {$track->CompletedPages}/{$track->TotalPages}"); + return $this->runLinksCheck($limit); } } diff --git a/javascript/BrokenExternalLinksReport.js b/javascript/BrokenExternalLinksReport.js index 54174f4..d44f7b5 100644 --- a/javascript/BrokenExternalLinksReport.js +++ b/javascript/BrokenExternalLinksReport.js @@ -4,6 +4,8 @@ $(this).start(); }, onmatch: function() { + // poll the current job and update the front end status + $('#externalLinksReport').hide(); $(this).poll(); }, start: function() { @@ -16,15 +18,16 @@ $(this).poll(); }, poll: function() { - // poll the current job and update the front end status - $('#externalLinksReport').hide(); $.ajax({ url: "admin/externallinks/getJobStatus", async: true, success: function(data) { var obj = $.parseJSON(data); + + // No report, so let user create one if (!obj) { - setTimeout(function() { $('#externalLinksReport').poll(); }, 1000); + $('#externalLinksReport').show(); + return; } var completed = obj.Completed ? obj.Completed : 0; var total = obj.Total ? obj.Total : 0; @@ -46,7 +49,7 @@ } }, error: function(e) { - console.log(e); + if(typeof console !== 'undefined') console.log(e); } }); }