NEW: Setting up Queued job and task to support being sent a single page

This commit is contained in:
Kirk Mayo 2014-07-24 14:20:48 +12:00
parent 3b976fd408
commit 72dc652ecd
3 changed files with 52 additions and 65 deletions

View File

@ -15,7 +15,6 @@ The external links module is a task and ModelAdmin to track and to report on bro
## Features ## Features
* Add external links to broken links reports * Add external links to broken links reports
* Add a model admin for external broken links
* Add a task to track external broken links * Add a task to track external broken links
## Installation ## Installation
@ -25,8 +24,7 @@ The external links module is a task and ModelAdmin to track and to report on bro
3. Make sure the folder after being extracted is named 'externallinks' 3. Make sure the folder after being extracted is named 'externallinks'
4. Place this directory in your sites root directory. This is the one with framework and cms in it. 4. Place this directory in your sites root directory. This is the one with framework and cms in it.
5. Run in your browser - `/dev/build` to rebuild the database. 5. Run in your browser - `/dev/build` to rebuild the database.
6. You should see a new menu called *Broken Ext. Links* 6. Run the following task *http://path.to.silverstripe/dev/tasks/CheckExternalLinks* to check for broken external links
7. Run the following task *http://path.to.silverstripe/dev/tasks/CheckExternalLinks* to check for broken external links
## Dev task ## ## Dev task ##
@ -38,6 +36,3 @@ Add the following code to the mysite config to run the job every 24 hours (86400
`Config::inst()->update('CheckExternalLinks', 'QueuedJob', 86400);` `Config::inst()->update('CheckExternalLinks', 'QueuedJob', 86400);`
## TODO ##
Fix setting the class attribute of broken links to ss-broken

View File

@ -1,82 +1,69 @@
<?php <?php
/** /**
* An check external links job * A Job for running a external link check for published pages
* *
*/ */
class CheckExternalLinksJob extends AbstractQueuedJob { class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob {
public static $regenerate_time = 43200;
public function __construct() { public function __construct() {
$this->pagesToProcess = SiteTree::get(); $this->pagesToProcess = Versioned::get_by_stage('SiteTree', 'Live')->column();
$this->currentStep = 0; $this->currentStep = 0;
$this->totalSteps = count($this->pagesToProcess); $this->totalSteps = count($this->pagesToProcess);
} }
/**
* Sitemap job is going to run for a while...
*/
public function getJobType() {
return QueuedJob::QUEUED;
}
/**
* @return string
*/
public function getTitle() { public function getTitle() {
return 'Checking external links'; return 'Checking external links';
} }
/** public function getJobType() {
* Return a signature for this queued job return QueuedJob::QUEUED;
* }
* For the generate sitemap job, we only ever want one instance running, so just use the class name
*
* @return String
*/
public function getSignature() { public function getSignature() {
return md5(get_class($this)); return md5(get_class($this));
} }
/**
* Note that this is duplicated for backwards compatibility purposes...
*/
public function setup() { public function setup() {
parent::setup(); parent::setup();
increase_time_limit_to();
$restart = $this->currentStep == 0; $restart = $this->currentStep == 0;
if ($restart) { if ($restart) {
$this->pagesToProcess = SiteTree::get(); $this->pagesToProcess = Versioned::get_by_stage('SiteTree', 'Live')->column();
} }
} }
/** /**
* On any restart, make sure to check that our temporary file is being created still. * Check a individual page
*/ */
public function prepareForRestart() {
parent::prepareForRestart();
}
public function process() { public function process() {
$task = new CheckExternalLinks(); $remainingPages = $this->pagesToProcess;
$task->run(); if (!count($remainingPages)) {
$data = $this->getJobData(); $this->isComplete = true;
$completedPages = $task->getCompletedPages(); return;
$totalPages = $task->getTotalPages();
$this->addMessage("$completedPages/$totalPages pages completed");
$this->completeJob();
} }
/** // lets process our first item - note that we take it off the list of things left to do
* Outputs the completed file to the site's webroot $ID = array_shift($remainingPages);
*/
protected function completeJob() { // get the page
$this->isComplete = 1; $page = Versioned::get_by_stage('SiteTree', 'Live', 'ID = '.$ID);
$nextgeneration = new CheckExternalLinksJob();
singleton('QueuedJobService')->queueJob($nextgeneration, if (!$page || !$page->Count()) {
date('Y-m-d H:i:s', time() + self::$regenerate_time)); $this->addMessage("Page ID #$ID could not be found, skipping");
} }
$task = new CheckExternalLinks();
$task->run($page);
// and now we store the new list of remaining children
$this->pagesToProcess = $remainingPages;
$this->currentStep++;
if (!count($remainingPages)) {
$this->isComplete = true;
return;
}
}
} }

View File

@ -19,7 +19,11 @@ class CheckExternalLinks extends BuildTask {
} }
function run($request) { function run($request) {
if (isset($request->ID)) {
$pages = $request;
} else {
$pages = Versioned::get_by_stage('SiteTree', 'Live'); $pages = Versioned::get_by_stage('SiteTree', 'Live');
}
foreach ($pages as $page) { foreach ($pages as $page) {
++$this->totalPages; ++$this->totalPages;
@ -63,9 +67,10 @@ class CheckExternalLinks extends BuildTask {
$htmlValue->__call('saveHTML', array()); $htmlValue->__call('saveHTML', array());
$page->Content = $htmlValue->getContent(); $page->Content = $htmlValue->getContent();
$page->write(); $page->owner->write();
if (!$page->HasBrokenLink) { if (!$page->HasBrokenLink) {
// bypass the ORM as syncLinkTracking does not allow you // bypass the ORM as syncLinkTracking does not allow you
// to update HasBrokenLink to true // to update HasBrokenLink to true
$query = "UPDATE \"SiteTree_Live\" SET \"HasBrokenLink\" = 1 "; $query = "UPDATE \"SiteTree_Live\" SET \"HasBrokenLink\" = 1 ";