/
RunBuildTaskJob.php
92 lines (80 loc) · 2.53 KB
/
RunBuildTaskJob.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Symbiote\QueuedJobs\Jobs;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataObject;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJob;
/**
* A convenience wrapper for running BuildTask implementations.
* These are usually executed via synchronous web request
* or synchronous CLI execution (under dev/tasks/*).
*
* Caution: This job can't increment steps. This is a signal
* for job health checks that a job should be considered stale
* after a (short) timeout. If you expect a build task to run
* for more than a few minutes, create it as a job with steps,
* increase timeouts, or disable health checks.
* See "Defining Jobs" in the docs for details.
*
* @author marcus@symbiote.com.au
* @license BSD License http://silverstripe.org/bsd-license/
*/
class RunBuildTaskJob extends AbstractQueuedJob
{
/**
* @param DataObject $node
*/
public function __construct($taskClass = null, $queryString = null)
{
if ($taskClass) {
$this->TaskClass = $taskClass;
}
if ($queryString) {
$this->QueryString = $queryString;
}
$this->currentStep = 0;
$this->totalSteps = 1;
}
/**
* @param string (default: Object)
*
* @return DataObject
*/
protected function getObject($name = 'SilverStripe\\Core\\Object')
{
return DataObject::get_by_id($this->TargetClass, $this->TargetID);
}
/**
* @return string
*/
public function getJobType()
{
return QueuedJob::QUEUED;
}
/**
* @return string
*/
public function getTitle()
{
$taskName = $this->QueryString ? ($this->TaskClass . '?' . $this->QueryString) : $this->TaskClass;
return _t('RunBuildTaskJob.JOB_TITLE', 'Run BuildTask {task}', ['task' => $taskName]);
}
public function process()
{
if (!is_subclass_of($this->TaskClass, BuildTask::class)) {
throw new \LogicException($this->TaskClass . ' is not a build task');
}
$task = Injector::inst()->create($this->TaskClass);
if (!$task->isEnabled()) {
throw new \LogicException($this->TaskClass . ' is not enabled');
}
$getVars = [];
parse_str($this->QueryString ?? '', $getVars);
$request = new HTTPRequest('GET', '/', $getVars);
$task->run($request);
$this->currentStep = 1;
$this->isComplete = true;
}
}