summaryrefslogtreecommitdiff
blob: 0c4d5aec3fce3ab7e3785e0cecc8c783d76930a0 (plain)
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
<?php

declare( strict_types = 1 );

namespace MediaWiki\Extensions\Translate\Synchronization;

use Maintenance;
use MediaWiki\Extensions\Translate\Services;
use MediaWiki\Logger\LoggerFactory;
use MessageIndex;

/**
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 * @since 2020.06
 */
class CompleteExternalTranslationMaintenanceScript extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription(
			'Check and run MessageIndexRebuild and MessageGroupStats update once ' .
			'MessageUpdateJobs are done. Intended to be run periodically'
		);
		$this->requireExtension( 'Translate' );
	}

	public function execute() {
		$logger = LoggerFactory::getInstance( 'Translate.GroupSynchronization' );
		$groupSyncCache = Services::getInstance()->getGroupSynchronizationCache();
		$groupsInSync = $groupSyncCache->getGroupsInSync();
		if ( !$groupsInSync ) {
			$logger->info( 'All message groups are in sync' );
			return;
		}

		$logger->info( 'Group synchronization is in progress' );

		$groupsInProgress = [];
		$groupResponses = [];
		foreach ( $groupsInSync as $groupId ) {
			$groupResponse = $groupSyncCache->getSynchronizationStatus( $groupId );
			$groupResponses[] = $groupResponse;

			if ( $groupResponse->isDone() ) {
				$groupSyncCache->endSync( $groupId );
				continue;
			}

			if ( $groupResponse->hasTimedOut() ) {
				$remainingMessageKeys = $groupResponse->getRemainingMessages();
				$logger->warning(
					'MessageUpdateJobs timed out for group - {groupId}; ' .
					'Messages - {messages}; ' .
					'Jobs remaining - {jobRemaining}',
					[
						'groupId' => $groupId ,
						'jobRemaining' => count( $remainingMessageKeys ),
						'messages' => implode( ', ', $remainingMessageKeys )
					]
				);
				wfLogWarning( 'MessageUpdateJob timed out for group - ' . $groupId );

				$groupSyncCache->endSync( $groupId );
			} else {
				$groupsInProgress[] = $groupId;
			}
		}

		if ( !$groupsInProgress ) {
			// No groups in progress.
			$logger->info( 'All message groups are now in sync. Starting MessageIndex rebuild' );
			MessageIndex::singleton()->rebuild();
		}

		$logger->info(
			"Script completed successfully. " .
			"{inProgressGroupCount} group synchronization(s) is/are in progress",
			[
				'inProgressGroupCount' => count( $groupsInProgress )
			]
		);
	}
}