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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
<?php
/**
* Job queue task description base code.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup JobQueue
*/
/**
* Job queue task description interface
*
* @ingroup JobQueue
* @since 1.23
*/
interface IJobSpecification {
/**
* @return string Job type
*/
public function getType();
/**
* @return array
*/
public function getParams();
/**
* @return int|null UNIX timestamp to delay running this job until, otherwise null
*/
public function getReleaseTimestamp();
/**
* @return bool Whether only one of each identical set of jobs should be run
*/
public function ignoreDuplicates();
/**
* Subclasses may need to override this to make duplication detection work.
* The resulting map conveys everything that makes the job unique. This is
* only checked if ignoreDuplicates() returns true, meaning that duplicate
* jobs are supposed to be ignored.
*
* @return array Map of key/values
*/
public function getDeduplicationInfo();
/**
* @return Title Descriptive title (this can simply be informative)
*/
public function getTitle();
}
/**
* Job queue task description base code
*
* Example usage:
* <code>
* $job = new JobSpecification(
* 'null',
* array( 'lives' => 1, 'usleep' => 100, 'pi' => 3.141569 ),
* array( 'removeDuplicates' => 1 ),
* Title::makeTitle( NS_SPECIAL, 'nullity' )
* );
* JobQueueGroup::singleton()->push( $job )
* </code>
*
* @ingroup JobQueue
* @since 1.23
*/
class JobSpecification implements IJobSpecification {
/** @var string */
protected $type;
/** @var array Array of job parameters or false if none */
protected $params;
/** @var Title */
protected $title;
/** @var array */
protected $opts;
/**
* @param string $type
* @param array $params Map of key/values
* @param array $opts Map of key/values
* @param Title $title Optional descriptive title
*/
public function __construct(
$type, array $params, array $opts = array(), Title $title = null
) {
$this->validateParams( $params );
$this->validateParams( $opts );
$this->type = $type;
$this->params = $params;
$this->title = $title ?: Title::newMainPage();
$this->opts = $opts;
}
/**
* @param array $params
*/
protected function validateParams( array $params ) {
foreach ( $params as $p => $v ) {
if ( is_array( $v ) ) {
$this->validateParams( $v );
} elseif ( !is_scalar( $v ) && $v !== null ) {
throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
}
}
}
/**
* @return string
*/
public function getType() {
return $this->type;
}
/**
* @return Title
*/
public function getTitle() {
return $this->title;
}
/**
* @return array
*/
public function getParams() {
return $this->params;
}
/**
* @return int|null UNIX timestamp to delay running this job until, otherwise null
*/
public function getReleaseTimestamp() {
return isset( $this->params['jobReleaseTimestamp'] )
? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
: null;
}
/**
* @return bool Whether only one of each identical set of jobs should be run
*/
public function ignoreDuplicates() {
return !empty( $this->opts['removeDuplicates'] );
}
/**
* Subclasses may need to override this to make duplication detection work.
* The resulting map conveys everything that makes the job unique. This is
* only checked if ignoreDuplicates() returns true, meaning that duplicate
* jobs are supposed to be ignored.
*
* @return array Map of key/values
*/
public function getDeduplicationInfo() {
$info = array(
'type' => $this->getType(),
'namespace' => $this->getTitle()->getNamespace(),
'title' => $this->getTitle()->getDBkey(),
'params' => $this->getParams()
);
if ( is_array( $info['params'] ) ) {
// Identical jobs with different "root" jobs should count as duplicates
unset( $info['params']['rootJobSignature'] );
unset( $info['params']['rootJobTimestamp'] );
// Likewise for jobs with different delay times
unset( $info['params']['jobReleaseTimestamp'] );
}
return $info;
}
/**
* @return array Field/value map that can immediately be serialized
* @since 1.25
*/
public function toSerializableArray() {
return array(
'type' => $this->type,
'params' => $this->params,
'opts' => $this->opts,
'title' => array(
'ns' => $this->title->getNamespace(),
'key' => $this->title->getDbKey()
)
);
}
/**
* @param array $map Field/value map
* @return JobSpecification
* @since 1.25
*/
public static function newFromArray( array $map ) {
$title = Title::makeTitle( $map['title']['ns'], $map['title']['key'] );
return new self( $map['type'], $map['params'], $map['opts'], $title );
}
}
|