1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Tasks; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use SilverStripe\Core\ClassInfo; |
7
|
|
|
use SilverStripe\Dev\BuildTask; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
9
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A task that can be used to create a queued job. |
13
|
|
|
* |
14
|
|
|
* Useful to hook a queued job in to place that needs to exist if it doesn't already. |
15
|
|
|
* |
16
|
|
|
* If no name is given, it creates a demo dummy job to help test that things |
17
|
|
|
* are set up and working |
18
|
|
|
* |
19
|
|
|
* @author Marcus Nyeholt <[email protected]> |
20
|
|
|
* @license BSD http://silverstripe.org/bsd-license/ |
21
|
|
|
*/ |
22
|
|
|
class CreateQueuedJobTask extends BuildTask |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private static $segment = 'CreateQueuedJobTask'; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getDescription() |
34
|
|
|
{ |
35
|
|
|
return _t( |
36
|
|
|
__CLASS__ . '.Description', |
37
|
|
|
'A task used to create a queued job. Pass the queued job class name as the "name" parameter, ' |
38
|
|
|
. 'pass an optional "start" parameter (parseable by strtotime) to set a start time for the job.' |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param HTTPRequest $request |
44
|
|
|
*/ |
45
|
|
|
public function run($request) |
46
|
|
|
{ |
47
|
|
|
if (isset($request['name']) && ClassInfo::exists($request['name'])) { |
48
|
|
|
$clz = $request['name']; |
49
|
|
|
$job = new $clz(); |
50
|
|
|
} else { |
51
|
|
|
$job = new DummyQueuedJob(mt_rand(10, 100)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (isset($request['start'])) { |
55
|
|
|
$start = strtotime($request['start']); |
56
|
|
|
$now = DBDatetime::now()->getTimestamp(); |
57
|
|
|
if ($start >= $now) { |
58
|
|
|
$friendlyStart = DBDatetime::create()->setValue($start)->Rfc2822(); |
59
|
|
|
echo "Job " . $request['name'] . " queued to start at: <b>" . $friendlyStart . "</b>"; |
60
|
|
|
QueuedJobService::singleton()->queueJob($job, $start); |
61
|
|
|
} else { |
62
|
|
|
echo "'start' parameter must be a date/time in the future, parseable with strtotime"; |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
echo "Job Queued"; |
66
|
|
|
QueuedJobService::singleton()->queueJob($job); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|