Inject services on JobClasses

Change-Id: Ic0e965dca31b41ab76e59a3d2672069464418de8
This commit is contained in:
Fomafix 2023-10-15 12:18:57 +00:00
parent 627a4bf0ef
commit efa6c4f0ce
3 changed files with 23 additions and 7 deletions

View file

@ -63,7 +63,12 @@
} }
}, },
"JobClasses": { "JobClasses": {
"InitImageDataJob": "PageImages\\Job\\InitImageDataJob" "InitImageDataJob": {
"class": "PageImages\\Job\\InitImageDataJob",
"services": [
"DBLoadBalancerFactory"
]
}
}, },
"config": { "config": {
"PageImagesScores": { "PageImagesScores": {

View file

@ -3,31 +3,38 @@
namespace PageImages\Job; namespace PageImages\Job;
use Job; use Job;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title; use MediaWiki\Title\Title;
use MWExceptionHandler; use MWExceptionHandler;
use RefreshLinks; use RefreshLinks;
use Wikimedia\Rdbms\LBFactory;
class InitImageDataJob extends Job { class InitImageDataJob extends Job {
/** @var LBFactory */
private $lbFactory;
/** /**
* @param Title $title Title object associated with this job * @param Title $title Title object associated with this job
* @param array $params Parameters to the job, containing an array of * @param array $params Parameters to the job, containing an array of
* page ids representing which pages to process * page ids representing which pages to process
* @param LBFactory $lbFactory
*/ */
public function __construct( Title $title, array $params ) { public function __construct(
Title $title,
array $params,
LBFactory $lbFactory
) {
parent::__construct( 'InitImageDataJob', $title, $params ); parent::__construct( 'InitImageDataJob', $title, $params );
$this->lbFactory = $lbFactory;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function run() { public function run() {
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
foreach ( $this->params['page_ids'] as $id ) { foreach ( $this->params['page_ids'] as $id ) {
try { try {
RefreshLinks::fixLinksFromArticle( $id ); RefreshLinks::fixLinksFromArticle( $id );
$lbFactory->waitForReplication(); $this->lbFactory->waitForReplication();
} catch ( \Exception $e ) { } catch ( \Exception $e ) {
// There are some broken pages out there that just don't parse. // There are some broken pages out there that just don't parse.
// Log it and keep on trucking. // Log it and keep on trucking.

View file

@ -81,7 +81,11 @@ class InitImageData extends Maintenance {
foreach ( $res as $row ) { foreach ( $res as $row ) {
$pageIds[] = $row->page_id; $pageIds[] = $row->page_id;
} }
$job = new InitImageDataJob( Title::newMainPage(), [ 'page_ids' => $pageIds ] ); $job = new InitImageDataJob(
Title::newMainPage(),
[ 'page_ids' => $pageIds ],
$this->getServiceContainer()->getDBLoadBalancerFactory()
);
if ( $queue === null ) { if ( $queue === null ) {
$job->run(); $job->run();
} else { } else {