Merge "Moved job running via $wgJobRunRate to a special API"
[lhc/web/wiklou.git] / includes / api / ApiRunJobs.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @author Aaron Schulz
21 */
22
23 /**
24 * This is a simple class to handle action=runjobs and is only used internally
25 *
26 * @note: this does not requre "write mode" nor tokens due to the signature check
27 *
28 * @ingroup API
29 */
30 class ApiRunJobs extends ApiBase {
31 public function execute() {
32 if ( wfReadOnly() ) {
33 $this->dieUsage( 'Wiki is in read-only mode', 'read_only', 400 );
34 }
35
36 $params = $this->extractRequestParams();
37 $squery = $this->getRequest()->getValues();
38 unset( $squery['signature'] );
39 $cSig = self::getQuerySignature( $squery );
40 $rSig = $params['signature'];
41
42 // Time-insensitive signature verification
43 if ( strlen( $rSig ) !== strlen( $cSig ) ) {
44 $verified = false;
45 } else {
46 $result = 0;
47 for ( $i = 0; $i < strlen( $cSig ); $i++ ) {
48 $result |= ord( $cSig{$i} ) ^ ord( $rSig{$i} );
49 }
50 $verified = ( $result == 0 );
51 }
52
53 if ( !$verified || $params['sigexpiry'] < time() ) {
54 $this->dieUsage( 'Invalid or stale signature provided', 'bad_signature', 401 );
55 }
56
57 // Client will usually disconnect before checking the response,
58 // but it needs to know when it is safe to disconnect. Until this
59 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
60 ignore_user_abort( true ); // jobs may take a bit of time
61 header( "HTTP/1.0 204 No Content" );
62 ob_flush();
63 flush();
64 // Once the client receives this response, it can disconnect
65
66 // Do all of the specified tasks...
67 if ( in_array( 'jobs', $params['tasks'] ) ) {
68 $this->executeJobs( $params );
69 }
70 }
71
72 /**
73 * @param array $query
74 * @return string
75 */
76 public static function getQuerySignature( array $query ) {
77 global $wgSecretKey;
78
79 ksort( $query ); // stable order
80 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $wgSecretKey );
81 }
82
83 /**
84 * Run jobs from the job queue
85 *
86 * @param array $params Request parameters
87 * @return void
88 */
89 protected function executeJobs( array $params ) {
90 $n = $params['maxjobs']; // number of jobs to run
91 if ( $n < 1 ) {
92 return;
93 }
94 try {
95 // Fallback to running the jobs here while the user waits
96 $group = JobQueueGroup::singleton();
97 do {
98 $job = $group->pop( JobQueueGroup::USE_CACHE ); // job from any queue
99 if ( $job ) {
100 $output = $job->toString() . "\n";
101 $t = - microtime( true );
102 wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
103 $success = $job->run();
104 wfProfileOut( __METHOD__ . '-' . get_class( $job ) );
105 $group->ack( $job ); // done
106 $t += microtime( true );
107 $t = round( $t * 1000 );
108 if ( $success === false ) {
109 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
110 } else {
111 $output .= "Success, Time: $t ms\n";
112 }
113 wfDebugLog( 'jobqueue', $output );
114 }
115 } while ( --$n && $job );
116 } catch ( MWException $e ) {
117 // We don't want exceptions thrown during job execution to
118 // be reported to the user since the output is already sent.
119 // Instead we just log them.
120 MWExceptionHandler::logException( $e );
121 }
122 }
123
124 public function mustBePosted() {
125 return true;
126 }
127
128 public function getAllowedParams() {
129 return array(
130 'tasks' => array(
131 ApiBase::PARAM_ISMULTI => true,
132 ApiBase::PARAM_TYPE => array( 'jobs' )
133 ),
134 'maxjobs' => array(
135 ApiBase::PARAM_TYPE => 'integer',
136 ApiBase::PARAM_DFLT => 0
137 ),
138 'signature' => array(
139 ApiBase::PROP_TYPE => 'string',
140 ),
141 'sigexpiry' => array(
142 ApiBase::PARAM_TYPE => 'integer',
143 ApiBase::PARAM_DFLT => 0 // ~epoch
144 ),
145 );
146 }
147
148 public function getParamDescription() {
149 return array(
150 'tasks' => 'List of task types to perform',
151 'maxjobs' => 'Maximum number of jobs to run',
152 'signature' => 'HMAC Signature that signs the request',
153 'sigexpiry' => 'HMAC signature expiry as a UNIX timestamp'
154 );
155 }
156
157 public function getDescription() {
158 return 'Perform periodic tasks or run jobs from the queue';
159 }
160
161 public function getExamples() {
162 return array(
163 'api.php?action=runjobs&tasks=jobs&maxjobs=3' => 'Run up to 3 jobs from the queue',
164 );
165 }
166 }