Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
RebuildMessageIndexJob | |
0.00% |
0 / 22 |
|
0.00% |
0 / 5 |
56 | |
0.00% |
0 / 1 |
newJob | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
allowRetries | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDeduplicationInfo | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\MessageLoading; |
5 | |
6 | use GenericParameterJob; |
7 | use MediaWiki\Extension\Translate\Jobs\GenericTranslateJob; |
8 | use MediaWiki\Extension\Translate\MessageGroupProcessing\MessageGroups; |
9 | use MediaWiki\Extension\Translate\Services; |
10 | use MediaWiki\MediaWikiServices; |
11 | |
12 | /** |
13 | * Job for rebuilding message index. |
14 | * @author Niklas Laxström |
15 | * @copyright Copyright © 2011-2013, Niklas Laxström |
16 | * @license GPL-2.0-or-later |
17 | * @ingroup JobQueue |
18 | */ |
19 | class RebuildMessageIndexJob extends GenericTranslateJob implements GenericParameterJob { |
20 | public static function newJob( ?string $caller = null ): self { |
21 | $timestamp = microtime( true ); |
22 | return new self( [ |
23 | 'timestamp' => $timestamp, |
24 | 'caller' => $caller ?? wfGetCaller(), |
25 | ] ); |
26 | } |
27 | |
28 | /** @inheritDoc */ |
29 | public function __construct( $params = [] ) { |
30 | parent::__construct( 'RebuildMessageIndexJob', $params ); |
31 | $this->removeDuplicates = true; |
32 | } |
33 | |
34 | public function run(): bool { |
35 | $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
36 | if ( !$lb->waitForReplication() ) { |
37 | $this->logWarning( 'Continuing despite replication lag' ); |
38 | } |
39 | |
40 | // Make sure we have latest version of message groups from global cache. |
41 | // This should be pretty fast, just a few cache fetches with some post processing. |
42 | MessageGroups::singleton()->clearProcessCache(); |
43 | |
44 | // BC for existing jobs which may not have this parameter set |
45 | $timestamp = $this->getParams()['timestamp'] ?? microtime( true ); |
46 | |
47 | try { |
48 | Services::getInstance()->getMessageIndex()->rebuild( $timestamp ); |
49 | } catch ( MessageIndexException $e ) { |
50 | // Currently there is just one type of exception: lock wait time exceeded. |
51 | // Assuming no bugs, this is a transient issue and retry will solve it. |
52 | $this->logWarning( $e->getMessage() ); |
53 | // Try again later. See ::allowRetries |
54 | return false; |
55 | } |
56 | |
57 | return true; |
58 | } |
59 | |
60 | public function allowRetries(): bool { |
61 | // This is the default, but added for explicitness and clarity |
62 | return true; |
63 | } |
64 | |
65 | public function getDeduplicationInfo(): array { |
66 | $info = parent::getDeduplicationInfo(); |
67 | // The timestamp is different for every job, so ignore it. The worst that can |
68 | // happen is that the front cache is not cleared until a future job is created. |
69 | // There is a check in MessageIndex to spawn a new job if timestamp is smaller |
70 | // than expected. |
71 | // |
72 | // Ideally we would take the latest timestamp, but it seems that the job queue |
73 | // just prevents insertion of duplicate jobs instead. |
74 | unset( $info['params']['timestamp'] ); |
75 | unset( $info['params']['caller'] ); |
76 | |
77 | return $info; |
78 | } |
79 | } |
80 | |
81 | class_alias( RebuildMessageIndexJob::class, 'MessageIndexRebuildJob' ); |