1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\StaticPublishQueue\Task; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
use SilverStripe\Dev\BuildTask; |
9
|
|
|
use SilverStripe\ORM\DataList; |
10
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
11
|
|
|
use SilverStripe\StaticPublishQueue\Job\StaticCacheFullBuildJob; |
12
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
13
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
14
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
15
|
|
|
|
16
|
|
|
class StaticCacheFullBuildTask extends BuildTask |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Queue up a StaticCacheFullBuildJob |
20
|
|
|
* Check for startAfter param and do some sanity checking |
21
|
|
|
* |
22
|
|
|
* @param HTTPRequest $request |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
|
|
public function run($request) |
26
|
|
|
{ |
27
|
|
|
$job = Injector::inst()->create(StaticCacheFullBuildJob::class); |
28
|
|
|
$signature = $job->getSignature(); |
29
|
|
|
|
30
|
|
|
// see if we already have this job in a queue |
31
|
|
|
$filter = [ |
32
|
|
|
'Signature' => $signature, |
33
|
|
|
'JobStatus' => [ |
34
|
|
|
QueuedJob::STATUS_NEW, |
35
|
|
|
QueuedJob::STATUS_INIT, |
36
|
|
|
], |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$existing = DataList::create(QueuedJobDescriptor::class)->filter($filter)->first(); |
40
|
|
|
|
41
|
|
|
if ($existing && $existing->exists()) { |
42
|
|
|
$this->log(sprintf( |
43
|
|
|
'There is already a %s in the queue, added %s %s', |
44
|
|
|
StaticCacheFullBuildJob::class, |
45
|
|
|
$existing->Created, |
46
|
|
|
$existing->StartAfter ? 'and set to start after ' . $existing->StartAfter : '' |
47
|
|
|
)); |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($request->getVar('startAfter')) { |
52
|
|
|
$now = DBDatetime::now(); |
53
|
|
|
$today = $now->Date(); |
54
|
|
|
$startTime = $request->getVar('startAfter'); |
55
|
|
|
|
56
|
|
|
// move to tomorrow if the starttime has passed today |
57
|
|
|
if ($now->Time24() > $startTime) { |
58
|
|
|
$timestamp = strtotime($today . ' ' . $startTime . ' +1 day'); |
59
|
|
|
$dayWord = 'tomorrow'; |
60
|
|
|
} else { |
61
|
|
|
$timestamp = strtotime($today . ' ' . $startTime); |
62
|
|
|
$dayWord = 'today'; |
63
|
|
|
} |
64
|
|
|
$startAfter = (new \DateTime())->setTimestamp($timestamp); |
65
|
|
|
$thisTimeTomorrow = (new \DateTime())->setTimestamp(strtotime($now . ' +1 day'))->getTimestamp(); |
66
|
|
|
|
67
|
|
|
// sanity check that we are in the next 24 hours - prevents some weird stuff sneaking through |
68
|
|
|
if ($startAfter->getTimestamp() > $thisTimeTomorrow || $startAfter->getTimestamp() < $now->getTimestamp()) { |
69
|
|
|
$this->log('Invalid startAfter parameter passed. Please ensure the time format is HHmm e.g. 1300'); |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->log(sprintf( |
74
|
|
|
'%s queued for %s %s.', |
75
|
|
|
StaticCacheFullBuildJob::class, |
76
|
|
|
$startAfter->format('H:m'), |
77
|
|
|
$dayWord |
78
|
|
|
)); |
79
|
|
|
} else { |
80
|
|
|
$startAfter = null; |
81
|
|
|
$this->log(StaticCacheFullBuildJob::class . ' added to the queue for immediate processing'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$job->setJobData(0, 0, false, new \stdClass(), [ |
85
|
|
|
'Building static cache for full site', |
86
|
|
|
]); |
87
|
|
|
QueuedJobService::singleton()->queueJob($job, $startAfter ? $startAfter->format('Y-m-d H:i:s') : null); |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function log($message) |
93
|
|
|
{ |
94
|
|
|
$newLine = Director::is_cli() ? PHP_EOL : '<br>'; |
95
|
|
|
echo $message . $newLine; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|