Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RunSingleJobStdin
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Run a single job from a definition passed from stdin
4 *
5 * Take a jobqueue-style job definition encoded as JSON as an input
6 * via stdin and execute it using a jobexecutor in the same style as
7 * rpc/RunSingleJob.php does on jobrunner hosts. This script can be
8 * used to run any one-off job and might be useful for testing the
9 * execution or rerunning of single jobs but is primarily designed for
10 * use with mercurius in videoscaling tasks.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @ingroup Maintenance
29 * @author Hugh Nowlan <hnowlan@wikimedia.org>
30 */
31
32namespace MediaWiki\Extensions\EventBus\Maintenance;
33
34// @codeCoverageIgnoreStart
35$IP = getenv( 'MW_INSTALL_PATH' );
36if ( $IP === false ) {
37    $IP = __DIR__ . '/../../..';
38}
39require_once "$IP/maintenance/Maintenance.php";
40// @codeCoverageIgnoreEnd
41
42use Exception;
43use MediaWiki\Extension\EventBus\JobExecutor;
44use MediaWiki\Maintenance\Maintenance;
45
46class RunSingleJobStdin extends Maintenance {
47
48    public function __construct() {
49        parent::__construct();
50        $this->addDescription( 'Run a single job from stdin' );
51    }
52
53    public function execute() {
54        $input = file_get_contents( "php://stdin" );
55        if ( $input === '' ) {
56            $this->fatalError( 'No event received.' );
57        }
58
59        $event_data = json_decode( $input, true );
60
61        if ( !isset( $event_data['database'] ) ) {
62            $this->fatalError( 'Invalid event received - database field is missing!' );
63        }
64
65        $executor = new JobExecutor();
66        try {
67            $response = $executor->execute( $event_data );
68            if ( $response['status'] === true ) {
69                $this->output( "Job executed successfully: {$response['message']}\n" );
70            } else {
71                if ( $response['readonly'] ) {
72                    // TODO - T204154
73                    // if we detect that the DB is in read-only mode, we delay the return of the
74                    // response to keep request rate low
75                    $this->output( "Sleeping due to readonly response from executor" );
76                    sleep( rand( 40, 45 ) );
77                }
78                $this->fatalError( "Failed to execute job on {$event_data['database']}{$response['message']}" );
79            }
80        } catch ( Exception $e ) {
81            $this->fatalError( "Caught exception when executing event for {$event_data['database']}" .
82                get_class( $e ) . "{$e->getMessage()}\n" . $e->getTraceAsString() );
83        }
84    }
85}
86
87// @codeCoverageIgnoreStart
88$maintClass = RunSingleJobStdin::class;
89require_once RUN_MAINTENANCE_IF_MAIN;
90// @codeCoverageIgnoreEnd