[MediaWiki-commits] [Gerrit] mediawiki...WikimediaMaintenance[master]: Create new wmfManageJobs.php for deleting queues from delete...

2017-07-28 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/368116 )

Change subject: Create new wmfManageJobs.php for deleting queues from deleted 
wikis
..


Create new wmfManageJobs.php for deleting queues from deleted wikis

Tested on the Beta Cluster:

$ mwscript eval.php --wiki aawiki
> $i = 1000; while ( $i-- ) { JobQueueGroup::singleton()->push( new NullJob( 
> Title::newMainPage(), [ 'usleep' => 100, 'lives' => 5 ] ) ); }

$ mwscript ~/wmfManageJobs.php --wiki=aawiki --target=aawiki
> null: 515 queued; 5 claimed (5 active, 0 abandoned); 0 delayed
> refreshLinksPrioritized: 0 queued; (empty or doesn't exist)
>
> Run the script again with --delete to delete these queues.

$ mwscript ~/wmfManageJobs.php --wiki=aawiki --target=aawiki --delete
> null: 241 queued; 4 claimed (4 active, 0 abandoned); 0 delayed
> refreshLinksPrioritized: 0 queued; (empty or doesn't exist)
>
> The script will now delete 241 job(s), from 2 different queue(s), for this 
> wiki: aawiki
> Abort with control-C in the next five seconds...0
> null: deleting...
> refreshLinksPrioritized: deleting...
> Done!

Bug: T171371
Change-Id: I29895db8c719abdd56ec773a79ef4287af2ac68b
---
A wmfManageJobs.php
1 file changed, 128 insertions(+), 0 deletions(-)

Approvals:
  BryanDavis: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/wmfManageJobs.php b/wmfManageJobs.php
new file mode 100644
index 000..88284b8
--- /dev/null
+++ b/wmfManageJobs.php
@@ -0,0 +1,128 @@
+http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Maintenance
+ */
+
+require_once __DIR__ . '/WikimediaMaintenance.php';
+
+/**
+ * WMF-specific version of mediawiki/maintenance/manageJobs.php.
+ *
+ * Main use case is to be able to perform actions on the job queue even for
+ * Wiki IDs that are no longer recognised by MediaWiki.
+ * It communicates directly with the job queue using the Job Config of
+ * the wiki the script was run from, under the assumption that all wikis
+ * share the same job queue configuration. (Or at least the target wiki,
+ * and the used wiki).
+ *
+ * For regular wikis that still exist and operate normally, prefer
+ * using MediaWiki core's manageJobs.php instead.
+ *
+ * Show current queues (like showJobs.php):
+ *
+ *  $ mwscript extensions/WikimediaMaintenance/wmfManageJobs.php --wiki=aawiki 
--target=testwiki
+ *
+ * Delete current queues (like manageJobs.php --delete):
+ *
+ *  $ mwscript extensions/WikimediaMaintenance/wmfManageJobs.php --wiki=aawiki 
--target=testwiki --delete
+ */
+class WmfManageJobs extends Maintenance {
+   public function __construct() {
+   parent::__construct();
+   $this->addDescription( 'Manage job queue for a particular 
wiki-id' );
+   $this->addOption( 'target', 'Which Wiki-ID to operate on', 
true, true );
+   $this->addOption( 'delete', 'Delete all queues for this wiki 
ID', false, false );
+   }
+
+   public function execute() {
+   $target = $this->getOption( 'target' );
+   $delete = $this->hasOption( 'delete' );
+
+   $group = JobQueueGroup::singleton( $target );
+
+   /**
+* Get a list of job types for this wiki
+* - Can't use $group->getQueueTypes() because that it uses 
SiteConfig
+*   to read 'wgJobClasses' from the target wiki, which may not 
exist
+* - Can't use `$this->getConfig()->get( 'JobClasses' )` 
because which
+*   jobs are recognised varies from one wiki to another (which 
extensions
+*   are installed etc, there is no "catch-all" registry)
+* - Workaround it by levering JobQueueAggregator, which tracks 
jobs
+*   in a structured globally keyed by job type first.
+*/
+   $aggregator = JobQueueAggregator::singleton();
+   $types = array_keys( $aggregator->getAllReadyWikiQueues() );
+
+   $deleteTypes = [];
+   $total = 0;
+
+   // Show current job queue status
+   // (Based on mediawiki/maintenance/showJobs.php)
+   foreach ( $types as $type ) {
+   $queue = $group->get( $type );
+   $pending = $queue->getSize();
+   $delayed = $queue->getDelayedCount();
+   $claimed = $queue->getAcquiredCount();
+   $abandoned = $queue->getAbandonedCount();
+   $subtotal = $pending + $delayed + $claimed + $abandoned;
+   if ( $subtotal ) {
+   $total += $subtotal;
+   $active = max( 0, $claimed - $abandoned );
+   $this->output(
+   "{$type}: $pending queued; " .
+  

[MediaWiki-commits] [Gerrit] mediawiki...WikimediaMaintenance[master]: Create new wmfManageJobs.php for deleting queues from delete...

2017-07-26 Thread Krinkle (Code Review)
Krinkle has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/368116 )

Change subject: Create new wmfManageJobs.php for deleting queues from deleted 
wikis
..

Create new wmfManageJobs.php for deleting queues from deleted wikis

Bug: T171371
Change-Id: I29895db8c719abdd56ec773a79ef4287af2ac68b
---
A wmfManageJobs.php
1 file changed, 119 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikimediaMaintenance 
refs/changes/16/368116/1

diff --git a/wmfManageJobs.php b/wmfManageJobs.php
new file mode 100644
index 000..8f92de0
--- /dev/null
+++ b/wmfManageJobs.php
@@ -0,0 +1,119 @@
+http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Maintenance
+ */
+
+require_once __DIR__ . '/WikimediaMaintenance.php';
+
+/**
+ * WMF-specific version of mediawiki/maintenance/manageJobs.php.
+ *
+ * Main use case is to be able to perform actions on the job queue even for
+ * Wiki IDs that are no longer recognised by MediaWiki.
+ * It communicates directly with the job queue using the Job Config of
+ * the wiki the script was run from, under the assumption that all wikis
+ * share the same job queue configuration. (Or at least the target wiki,
+ * and the used wiki).
+ *
+ * For regular wikis that still exist and operate normally, prefer
+ * using MediaWiki core's manageJobs.php instead.
+ *
+ * Usage:
+ *
+ *  $ mwscript extensions/WikimediaMaintenance/wmfManageJobs.php --wiki=aawiki 
--target=example
+ */
+class WmfManageJobs extends Maintenance {
+   public function __construct() {
+   parent::__construct();
+   $this->addDescription( 'Manage job queue for a particular 
wiki-id' );
+   $this->addOption( 'target', 'Which Wiki-ID to operate on', 
true, true );
+   $this->addOption( 'delete', 'Delete all queues for this wiki 
ID', false, false );
+   }
+
+   public function execute() {
+   $target = $this->getOption( 'target' );
+   $delete = $this->hasOption( 'delete' );
+
+   $group = JobQueueGroup::singleton( $target );
+
+   // Get a list of job types for this wiki
+   // - Can't use $group->getQueueTypes() because that it uses 
SiteConfig
+   //   to read 'wgJobClasses' from the target wiki, which may not 
exist
+   // - Can't use `$this->getConfig()->get( 'JobClasses' )` 
because which
+   //   jobs are recognised varies from one wiki to another (which 
extensions
+   //   are installed etc, there is no "catch-all" registry)
+   // - Workaround it by levering JobQueueAggregator, which tracks 
jobs
+   //   in a structured globally keyed by job type first.
+   $aggregator = JobQueueAggregator::singleton();
+   $types = array_keys( $aggregator->getAllReadyWikiQueues() );
+
+   $deleteTypes = [];
+   $total = 0;
+
+   // Show current job queue status
+   // (Based on mediawiki/maintenance/showJobs.php)
+   foreach ( $types as $type ) {
+   $queue = $group->get( $type );
+   if ( !$queue ) {
+   $this->output( "{$type}: (does not exist for 
this wiki)" );
+   continue;
+   }
+   $pending = $queue->getSize();
+   $delayed = $queue->getDelayedCount();
+   $claimed = $queue->getAcquiredCount();
+   $abandoned = $queue->getAbandonedCount();
+   $subtotal = $pending + $delayed + $claimed + $abandoned;
+   if ( $subtotal ) {
+   $active = max( 0, $claimed - $abandoned );
+   $this->output(
+   "{$type}: $pending queued; " .
+   "$claimed claimed ($active active, 
$abandoned abandoned); " .
+   "$delayed delayed\n"
+   );
+   $deleteTypes[$type] = $queue;
+   $total += $subtotal;
+   }
+   }
+
+   if ( !$deleteTypes ) {
+   $this->output( "\nNo queues found for $target.\n" );
+   return;
+   }
+
+   if ( !$delete ) {
+   $this->output( "\nRun the script again with --delete to 
delete these queues.\n" );
+   return;
+   }
+
+   $count = count( $deleteTypes );
+   $this->output( "\n\nThe script will now delete $total job(s), " 
.
+   "from $count different type(s), for this wiki: 
$target\n" );
+   $this->output( 'Abort with