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
* Add external links to broken links reports
* Add a model admin for external broken links
* Add a task to track external broken links
## 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'
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.
6. You should see a new menu called *Broken Ext. Links*
7. Run the following task *http://path.to.silverstripe/dev/tasks/CheckExternalLinks* to check for broken external links
6. Run the following task *http://path.to.silverstripe/dev/tasks/CheckExternalLinks* to check for broken external links
## 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);`
## TODO ##
Fix setting the class attribute of broken links to ss-broken

View File

@ -1,82 +1,69 @@
<?php
/**
* An check external links job
* A Job for running a external link check for published pages
*
*/
class CheckExternalLinksJob extends AbstractQueuedJob {
public static $regenerate_time = 43200;
class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob {
public function __construct() {
$this->pagesToProcess = SiteTree::get();
$this->pagesToProcess = Versioned::get_by_stage('SiteTree', 'Live')->column();
$this->currentStep = 0;
$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() {
return 'Checking external links';
}
/**
* Return a signature for this queued job
*
* For the generate sitemap job, we only ever want one instance running, so just use the class name
*
* @return String
*/
public function getJobType() {
return QueuedJob::QUEUED;
}
public function getSignature() {
return md5(get_class($this));
}
/**
* Note that this is duplicated for backwards compatibility purposes...
*/
public function setup() {
parent::setup();
increase_time_limit_to();
$restart = $this->currentStep == 0;
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() {
$task = new CheckExternalLinks();
$task->run();
$data = $this->getJobData();
$completedPages = $task->getCompletedPages();
$totalPages = $task->getTotalPages();
$this->addMessage("$completedPages/$totalPages pages completed");
$this->completeJob();
$remainingPages = $this->pagesToProcess;
if (!count($remainingPages)) {
$this->isComplete = true;
return;
}
/**
* Outputs the completed file to the site's webroot
*/
protected function completeJob() {
$this->isComplete = 1;
$nextgeneration = new CheckExternalLinksJob();
singleton('QueuedJobService')->queueJob($nextgeneration,
date('Y-m-d H:i:s', time() + self::$regenerate_time));
// lets process our first item - note that we take it off the list of things left to do
$ID = array_shift($remainingPages);
// get the page
$page = Versioned::get_by_stage('SiteTree', 'Live', 'ID = '.$ID);
if (!$page || !$page->Count()) {
$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) {
if (isset($request->ID)) {
$pages = $request;
} else {
$pages = Versioned::get_by_stage('SiteTree', 'Live');
}
foreach ($pages as $page) {
++$this->totalPages;
@ -63,9 +67,10 @@ class CheckExternalLinks extends BuildTask {
$htmlValue->__call('saveHTML', array());
$page->Content = $htmlValue->getContent();
$page->write();
$page->owner->write();
if (!$page->HasBrokenLink) {
// bypass the ORM as syncLinkTracking does not allow you
// to update HasBrokenLink to true
$query = "UPDATE \"SiteTree_Live\" SET \"HasBrokenLink\" = 1 ";