Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RequeueTranscodes
0.00% covered (danger)
0.00%
0 / 79
0.00% covered (danger)
0.00%
0 / 3
930
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 processFile
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
812
1<?php
2/**
3 * Re-queue selected, or all, transcodes
4 */
5$IP = getenv( 'MW_INSTALL_PATH' );
6if ( $IP === false ) {
7    $IP = __DIR__ . '/../../..';
8}
9require_once "$IP/maintenance/Maintenance.php";
10require_once __DIR__ . "/TimedMediaMaintenance.php";
11
12use MediaWiki\TimedMediaHandler\WebVideoTranscode\WebVideoTranscode;
13
14class RequeueTranscodes extends TimedMediaMaintenance {
15
16    public function __construct() {
17        parent::__construct();
18        $this->addOption( "key", "process only the given format key or comma-delimited list", false, true );
19        $this->addOption( "error", "re-queue formats that previously failed" );
20        $this->addOption( "stalled", "re-queue formats that were started but not finished" );
21        $this->addOption( "missing", "queue formats that were never started" );
22        $this->addOption( "force", "force re-queueing of all matching transcodes" );
23        $this->addOption( "throttle", "throttle on the queue" );
24        $this->addOption( "manual-override", "override soft limits on output file size" );
25        $this->addOption( "remux", "use remuxing from another transcode where possible" );
26        $this->addOption( "remove", "remove but don't re-queue" );
27        $this->addOption( "dry-run", "don't actually remove/enqueue transcodes; for testing params" );
28        $this->addDescription( "re-queue existing and missing media transcodes." );
29    }
30
31    public function execute() {
32        $this->output( "Cleanup transcodes:\n" );
33        parent::execute();
34        $this->output( "Finished!\n" );
35    }
36
37    /**
38     * @param File $file
39     */
40    public function processFile( File $file ) {
41        $this->output( $file->getName() . "\n" );
42
43        $transcodeSet = WebVideoTranscode::enabledTranscodes();
44        $dbw = $this->getServiceContainer()->getDBLoadBalancerFactory()->getReplicaDatabase();
45        $repo = $file->repo;
46        $dryRun = $this->hasOption( 'dry-run' );
47
48        WebVideoTranscode::cleanupTranscodes( $file );
49
50        $keys = [];
51        if ( $this->hasOption( 'key' ) ) {
52            $keys = preg_split( '/\s*,\s*/', $this->getOption( 'key' ) );
53        }
54
55        $state = WebVideoTranscode::getTranscodeState( $file, $dbw );
56        $toRemove = [];
57        $toAdd = [];
58        foreach ( $state as $key => $item ) {
59            $path = WebVideoTranscode::getDerivativeFilePath( $file, $key );
60
61            if ( $keys && !in_array( $key, $keys ) ) {
62                $run = false;
63            } elseif ( $this->hasOption( 'force' ) || $this->hasOption( 'remove' ) ) {
64                $run = true;
65            } elseif ( $this->hasOption( 'error' ) && $item['time_error'] ) {
66                $run = true;
67            } elseif ( $this->hasOption( 'stalled' ) &&
68                ( $item['time_addjob'] && !$item['time_success'] && !$item['time_error'] ) ) {
69                $run = true;
70            } elseif ( $this->hasOption( 'missing' ) && $path && !$repo->fileExists( $path ) ) {
71                $run = true;
72            } elseif ( !$item['time_addjob'] ) {
73                $run = true;
74            } else {
75                $run = false;
76            }
77
78            if ( $run ) {
79                $toRemove[] = $key;
80                if ( !$this->hasOption( 'remove' ) ) {
81                    $toAdd[] = $key;
82                }
83            }
84        }
85
86        natsort( $toRemove );
87        foreach ( $toRemove as $key ) {
88            if ( $dryRun ) {
89                $this->output( ".. would remove $key\n" );
90            } else {
91                $this->output( ".. removing $key\n" );
92                WebVideoTranscode::removeTranscodes( $file, $key );
93            }
94        }
95
96        $throttle = $this->hasOption( 'throttle' );
97        natsort( $toAdd );
98        foreach ( $toAdd as $key ) {
99            if ( !WebVideoTranscode::isTranscodeEnabled( $file, $key ) ) {
100                // don't enqueue too-big files
101                $this->output( ".. skipping disabled transcode $key\n" );
102                continue;
103            }
104
105            $startTime = microtime( true );
106            $startSize = 0;
107            if ( $throttle ) {
108                $startSize = WebVideoTranscode::getQueueSize( $file, $key );
109            }
110
111            $options = [
112                'manualOverride' => $this->hasOption( 'manual-override' ),
113                'remux' => $this->hasOption( 'remux' ),
114            ];
115            if ( $dryRun ) {
116                $this->output( ".. would queue $key\n" );
117            } else {
118                $this->output( ".. queueing $key\n" );
119                WebVideoTranscode::updateJobQueue( $file, $key, $options );
120            }
121
122            while ( $throttle ) {
123                $size = WebVideoTranscode::getQueueSize( $file, $key );
124                $delta = microtime( true ) - $startTime;
125                if ( $size > $startSize && $delta < self::TIMEOUT_SEC ) {
126                    $this->output( ".. (queue $size)\n" );
127                    sleep( 1 );
128                } else {
129                    $this->output( "\n" );
130                    break;
131                }
132            }
133        }
134    }
135
136    /**
137     * If the queue counts get fouled up, go ahead and time out throttle checks
138     * after one hour.
139     */
140    private const TIMEOUT_SEC = 3600;
141}
142
143// Tells it to run the class
144$maintClass = RequeueTranscodes::class;
145require_once RUN_MAINTENANCE_IF_MAIN;