Skip to content

Commit

Permalink
Added support for queued jobs module
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Guinn committed May 9, 2016
1 parent 6cd283b commit cc5e938
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 7 deletions.
55 changes: 49 additions & 6 deletions code/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function process(SS_HTTPRequest $req)
if (!$id) {
$this->httpError(404);
}

/** @var DownloadTempFile $file */
$file = DownloadTempFile::get()->byID($id);
if (!$file || !$file->ID) {
$this->httpError(404);
Expand All @@ -75,7 +77,7 @@ public function process(SS_HTTPRequest $req)
// in the background and in the meantime we refreshed or something
// so just send them the file.
$this->addToLog(Session::get('DownloadableProcessingOrderID'), $file->SourceFiles(), $file);
return $this->sendTempFile($file);
return $this->displayDownloadPage($file);
break;

case DownloadTempFile::ACTIVE:
Expand All @@ -89,10 +91,14 @@ public function process(SS_HTTPRequest $req)
// otherwise fall through and restart processing

case DownloadTempFile::PENDING:
ini_set('max_execution_time', 0);
$file->process();
$this->addToLog(Session::get('DownloadableProcessingOrderID'), $file->SourceFiles(), $file);
return $this->sendTempFile($file);
if (!interface_exists('QueuedJob')) {
ini_set('max_execution_time', 0);
$file->process();
$this->addToLog(Session::get('DownloadableProcessingOrderID'), $file->SourceFiles(), $file);
return $this->sendTempFile($file);
} else {
return $this->displayCrunchingPage($file);
}
}
}

Expand Down Expand Up @@ -202,7 +208,7 @@ protected function initiateOfflineProcessing(array $files, $orderID)
if ($existingFile && $existingFile->exists()) {
if ($existingFile->ProcessingState === DownloadTempFile::COMPLETE) {
$this->addToLog($orderID, $files, $existingFile);
return $this->sendTempFile($existingFile);
return $this->displayDownloadPage($existingFile);
} else {
return $this->displayCrunchingPage($existingFile);
}
Expand All @@ -226,6 +232,12 @@ protected function initiateOfflineProcessing(array $files, $orderID)
$dl->updateFileKey();
$dl->write();

// If we've got a worker queue, use that
if (interface_exists('QueuedJob')) {
$job = new FilePrepQueuedJob($dl);
$job->triggerProcessing();
}

// Display the "crunching" page so the user isn't left wondering what's going on
return $this->displayCrunchingPage($dl);
}
Expand Down Expand Up @@ -261,6 +273,37 @@ protected function displayCrunchingPage(DownloadTempFile $dl)
}


/**
* @param DownloadTempFile $dl
* @return HTMLText
*/
protected function displayDownloadPage(DownloadTempFile $dl)
{
$downloadPage = Config::inst()->get('Downloadable', 'download_page');
if ($downloadPage) {
$downloadPage = SiteTree::get_by_link($downloadPage);
}

if (!$downloadPage || !$downloadPage->exists()) {
return $this->sendTempFile($dl);
} else {
$dl->LastUsedAt = date('Y-m-d H:i:s');
$dl->write();
}

// Just in case
$this->dataRecord = $downloadPage;

// Add a meta tag that will refresh with the request that actually does the processing
// In the future this could be wrapped in a <noscript> and we could do some better ajax
// work to make this more userfriendly (such as a progress bar for multiple files, etc)
Requirements::insertHeadTags('<meta http-equiv="refresh" content="1; url=' . $dl->Link() . '">');

// And....render
return $this->customise($downloadPage)->renderWith(array('DownloadPage', 'Page', 'Page'));
}


/**
* @param int|Order $orderID
* @param array|File $files
Expand Down
3 changes: 3 additions & 0 deletions code/Downloadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Downloadable extends DataExtension
/** @var string - url segment/path of a page to display instead of the default while copying/zipping files */
private static $crunching_page = '';

/** @var string - url segment/path of a page to display while downloading */
private static $download_page = '';

/** @var int - how many minutes before crunching is restarted on a zip file */
private static $crunching_zombie_window = 5;

Expand Down
65 changes: 65 additions & 0 deletions code/FilePrepQueuedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* If the queued jobs module is installed, this will be used for offline processing of Zip files.
*
* @author Mark Guinn <mark@adaircreative.com>
* @date 05.09.2016
* @package shop_downloadable
*/
if (!interface_exists('QueuedJob')) {
return;
}

class FilePrepQueuedJob extends AbstractQueuedJob implements QueuedJob
{
/**
* The QueuedJob queue to use when processing updates
* @config
* @var int
*/
private static $reindex_queue = 2; // QueuedJob::QUEUED;


/**
* @param DownloadTempFile $object
*/
public function __construct($object)
{
$this->setObject($object);
}


/**
* Helper method
*/
public function triggerProcessing()
{
singleton('QueuedJobService')->queueJob($this);
}


/**
* @return string
*/
public function getTitle()
{
/** @var DownloadTempFile $obj */
$obj = $this->getObject();
return "Prep File For Download: " . ($obj ? $obj->getFriendlyName() : '???');
}


/**
* Reprocess any needed fields
*/
public function process()
{
ini_set('memory_limit', '1G');

/** @var DownloadTempFile $obj */
$obj = $this->getObject();
$obj->process();
$this->isComplete = true;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
},
"require": {
"silverstripe/framework": "~3.1",
"burnbright/silverstripe-shop": "*"
"silvershop/core": "~1.2 || ~2.0"
}
}

0 comments on commit cc5e938

Please sign in to comment.