1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Model; |
4
|
|
|
|
5
|
|
|
abstract class Worker |
6
|
|
|
{ |
7
|
|
|
protected $jobManager; |
8
|
|
|
protected $jobClass; |
9
|
|
|
protected $job; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
10 |
|
public function getJobClass() |
15
|
|
|
{ |
16
|
10 |
|
return $this->jobClass; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $jobClass |
21
|
|
|
*/ |
22
|
9 |
|
public function setJobClass($jobClass) |
23
|
|
|
{ |
24
|
9 |
|
$this->jobClass = $jobClass; |
25
|
9 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param JobManagerInterface $jobManager |
29
|
|
|
*/ |
30
|
19 |
|
public function setJobManager(JobManagerInterface $jobManager) |
31
|
|
|
{ |
32
|
19 |
|
$this->jobManager = $jobManager; |
33
|
19 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return |
37
|
|
|
*/ |
38
|
42 |
|
public function getJobManager() |
39
|
|
|
{ |
40
|
42 |
|
return $this->jobManager; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param int|null $time |
45
|
|
|
* @param bool $batch |
46
|
|
|
* @param int|null $priority |
47
|
|
|
*/ |
48
|
14 |
|
public function at($time = null, $batch = false, $priority = null) |
49
|
|
|
{ |
50
|
14 |
|
if (null === $time) { |
51
|
|
|
$time = time(); |
52
|
|
|
} |
53
|
|
|
|
54
|
14 |
|
if ($time) { |
55
|
14 |
|
$dateTime = new \DateTime(); |
56
|
14 |
|
$dateTime->setTimestamp($time); |
57
|
14 |
|
} else { |
58
|
|
|
$dateTime = null; |
59
|
|
|
} |
60
|
|
|
|
61
|
14 |
|
return new $this->jobClass($this, $batch, $priority, $dateTime); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param int $delay Amount of time to delay |
66
|
|
|
* @param int|null $priority |
67
|
|
|
*/ |
68
|
11 |
|
public function later($delay = 0, $priority = null) |
69
|
|
|
{ |
70
|
11 |
|
return $this->batchOrLaterDelay($delay, false, $priority); |
71
|
|
|
} |
72
|
|
|
|
73
|
12 |
|
public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null) |
74
|
|
|
{ |
75
|
12 |
|
$job = $this->at(time() + $delay, $batch, $priority); |
76
|
12 |
|
$job->setDelay($delay); |
77
|
|
|
|
78
|
12 |
|
return $job; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int $delay Amount of time to delay |
83
|
|
|
* @param int|null $priority |
84
|
|
|
*/ |
85
|
3 |
|
public function batchLater($delay = 0, $priority = null) |
86
|
|
|
{ |
87
|
3 |
|
return $this->batchOrLaterDelay($delay, true, $priority); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int|null $time |
92
|
|
|
* @param int|null $priority |
93
|
|
|
*/ |
94
|
1 |
|
public function batchAt($time = null, $priority = null) |
95
|
|
|
{ |
96
|
1 |
|
return $this->at($time, true, $priority); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
abstract public function getName(); |
100
|
|
|
} |
101
|
|
|
|