Add Job::getMetadata() and Job::setMetadata() accessors
authorAaron Schulz <aschulz@wikimedia.org>
Sat, 30 Mar 2019 15:05:31 +0000 (08:05 -0700)
committerKrinkle <krinklemail@gmail.com>
Wed, 3 Apr 2019 16:34:32 +0000 (16:34 +0000)
Change-Id: I3a97008d324f600a1c9f6005673073277ee564fa

includes/jobqueue/Job.php
includes/jobqueue/JobQueueDB.php
includes/jobqueue/JobQueueFederated.php
includes/jobqueue/JobQueueMemory.php
includes/jobqueue/JobQueueRedis.php

index 24fc473..22ff446 100644 (file)
@@ -156,6 +156,36 @@ abstract class Job implements IJobSpecification {
                return $this->params;
        }
 
+       /**
+        * @param string|null $field Metadata field or null to get all the metadata
+        * @return mixed|null Value; null if missing
+        * @since 1.33
+        */
+       public function getMetadata( $field = null ) {
+               if ( $field === null ) {
+                       return $this->metadata;
+               }
+
+               return $this->metadata[$field] ?? null;
+       }
+
+       /**
+        * @param string $field Key name to set the value for
+        * @param mixed $value The value to set the field for
+        * @return mixed|null The prior field value; null if missing
+        * @since 1.33
+        */
+       public function setMetadata( $field, $value ) {
+               $old = $this->getMetadata( $field );
+               if ( $value === null ) {
+                       unset( $this->metadata[$field] );
+               } else {
+                       $this->metadata[$field] = $value;
+               }
+
+               return $old;
+       }
+
        /**
         * @return int|null UNIX timestamp to delay running this job until, otherwise null
         * @since 1.22
index 74a6559..c52a948 100644 (file)
@@ -317,8 +317,8 @@ class JobQueueDB extends JobQueue {
                                $title = Title::makeTitle( $row->job_namespace, $row->job_title );
                                $job = Job::factory( $row->job_cmd, $title,
                                        self::extractBlob( $row->job_params ) );
-                               $job->metadata['id'] = $row->job_id;
-                               $job->metadata['timestamp'] = $row->job_timestamp;
+                               $job->setMetadata( 'id', $row->job_id );
+                               $job->setMetadata( 'timestamp', $row->job_timestamp );
                                break; // done
                        } while ( true );
 
@@ -484,7 +484,8 @@ class JobQueueDB extends JobQueue {
         * @throws MWException
         */
        protected function doAck( Job $job ) {
-               if ( !isset( $job->metadata['id'] ) ) {
+               $id = $job->getMetadata( 'id' );
+               if ( $id === null ) {
                        throw new MWException( "Job of type '{$job->getType()}' has no ID." );
                }
 
@@ -493,8 +494,11 @@ class JobQueueDB extends JobQueue {
                $scope = $this->getScopedNoTrxFlag( $dbw );
                try {
                        // Delete a row with a single DELETE without holding row locks over RTTs...
-                       $dbw->delete( 'job',
-                               [ 'job_cmd' => $this->type, 'job_id' => $job->metadata['id'] ], __METHOD__ );
+                       $dbw->delete(
+                               'job',
+                               [ 'job_cmd' => $this->type, 'job_id' => $id ],
+                               __METHOD__
+                       );
 
                        JobQueue::incrStats( 'acks', $this->type );
                } catch ( DBError $e ) {
@@ -617,8 +621,8 @@ class JobQueueDB extends JobQueue {
                                                Title::makeTitle( $row->job_namespace, $row->job_title ),
                                                strlen( $row->job_params ) ? unserialize( $row->job_params ) : []
                                        );
-                                       $job->metadata['id'] = $row->job_id;
-                                       $job->metadata['timestamp'] = $row->job_timestamp;
+                                       $job->setMetadata( 'id', $row->job_id );
+                                       $job->setMetadata( 'timestamp', $row->job_timestamp );
 
                                        return $job;
                                }
index 2025bf7..30ab7e7 100644 (file)
@@ -287,7 +287,7 @@ class JobQueueFederated extends JobQueue {
                                $job = false;
                        }
                        if ( $job ) {
-                               $job->metadata['QueuePartition'] = $partition;
+                               $job->setMetadata( 'QueuePartition', $partition );
 
                                return $job;
                        } else {
@@ -300,11 +300,12 @@ class JobQueueFederated extends JobQueue {
        }
 
        protected function doAck( Job $job ) {
-               if ( !isset( $job->metadata['QueuePartition'] ) ) {
+               $partition = $job->getMetadata( 'QueuePartition' );
+               if ( $partition === null ) {
                        throw new MWException( "The given job has no defined partition name." );
                }
 
-               $this->partitionQueues[$job->metadata['QueuePartition']]->ack( $job );
+               $this->partitionQueues[$partition]->ack( $job );
        }
 
        protected function doIsRootJobOldDuplicate( Job $job ) {
index 9b1fbdf..6c45e96 100644 (file)
@@ -132,7 +132,7 @@ class JobQueueMemory extends JobQueue {
                $job = $this->jobFromSpecInternal( $spec );
 
                end( $claimed );
-               $job->metadata['claimId'] = key( $claimed );
+               $job->setMetadata( 'claimId', key( $claimed ) );
 
                return $job;
        }
@@ -148,7 +148,7 @@ class JobQueueMemory extends JobQueue {
                }
 
                $claimed =& $this->getQueueData( 'claimed' );
-               unset( $claimed[$job->metadata['claimId']] );
+               $job->setMetadata( 'claimId', null );
        }
 
        /**
index 5e7a115..4d07a09 100644 (file)
@@ -385,11 +385,11 @@ LUA;
         * @throws JobQueueError
         */
        protected function doAck( Job $job ) {
-               if ( !isset( $job->metadata['uuid'] ) ) {
+               $uuid = $job->getMetadata( 'uuid' );
+               if ( $uuid === null ) {
                        throw new UnexpectedValueException( "Job of type '{$job->getType()}' has no UUID." );
                }
 
-               $uuid = $job->metadata['uuid'];
                $conn = $this->getConnection();
                try {
                        static $script =
@@ -643,10 +643,11 @@ LUA;
                        }
                        $title = Title::makeTitle( $item['namespace'], $item['title'] );
                        $job = Job::factory( $item['type'], $title, $item['params'] );
-                       $job->metadata['uuid'] = $item['uuid'];
-                       $job->metadata['timestamp'] = $item['timestamp'];
+                       $job->setMetadata( 'uuid', $item['uuid'] );
+                       $job->setMetadata( 'timestamp', $item['timestamp'] );
                        // Add in attempt count for debugging at showJobs.php
-                       $job->metadata['attempts'] = $conn->hGet( $this->getQueueKey( 'h-attempts' ), $uid );
+                       $job->setMetadata( 'attempts',
+                               $conn->hGet( $this->getQueueKey( 'h-attempts' ), $uid ) );
 
                        return $job;
                } catch ( RedisException $e ) {
@@ -704,8 +705,8 @@ LUA;
        protected function getJobFromFields( array $fields ) {
                $title = Title::makeTitle( $fields['namespace'], $fields['title'] );
                $job = Job::factory( $fields['type'], $title, $fields['params'] );
-               $job->metadata['uuid'] = $fields['uuid'];
-               $job->metadata['timestamp'] = $fields['timestamp'];
+               $job->setMetadata( 'uuid', $fields['uuid'] );
+               $job->setMetadata( 'timestamp', $fields['timestamp'] );
 
                return $job;
        }