Merge pull request #3 from tractorcow/pulls/fix-tmp-loop

Make sure that process yields to queuedjobs after each page
This commit is contained in:
kmayo-ss 2014-08-04 11:26:28 +12:00
commit 5a1c636264
3 changed files with 32 additions and 32 deletions

View File

@ -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';
}
}

View File

@ -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);
}
}

View File

@ -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);
}
});
}