Reset scoped session for upload jobs after deferred updates
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 17 Feb 2016 03:25:24 +0000 (19:25 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 24 Feb 2016 18:54:31 +0000 (10:54 -0800)
Bug: T126450
Change-Id: I8a6588209647252a4509078aaa7bf0cb1d9d299a

includes/jobqueue/Job.php
includes/jobqueue/JobRunner.php
includes/jobqueue/jobs/AssembleUploadChunksJob.php
includes/jobqueue/jobs/PublishStashedFileJob.php

index 2d13c7e..abab142 100644 (file)
@@ -47,6 +47,9 @@ abstract class Job implements IJobSpecification {
        /** @var string Text for error that occurred last */
        protected $error;
 
+       /** @var callable[] */
+       protected $teardownCallbacks = [];
+
        /**
         * Run the job
         * @return bool Success
@@ -279,6 +282,25 @@ abstract class Job implements IJobSpecification {
                return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
        }
 
+       /**
+        * @param callable $callback
+        * @since 1.27
+        */
+       protected function addTeardownCallback( $callback ) {
+               $this->teardownCallbacks[] = $callback;
+       }
+
+       /**
+        * Do any final cleanup after run(), deferred updates, and all DB commits happen
+        *
+        * @since 1.27
+        */
+       public function teardown() {
+               foreach ( $this->teardownCallbacks as $callback ) {
+                       call_user_func( $callback );
+               }
+       }
+
        /**
         * Insert a single job into the queue.
         * @return bool True on success
index 3919318..60372d1 100644 (file)
@@ -265,6 +265,7 @@ class JobRunner implements LoggerAwareInterface {
 
                        DeferredUpdates::doUpdates();
                        $this->commitMasterChanges( $job );
+                       $job->teardown();
                } catch ( Exception $e ) {
                        MWExceptionHandler::rollbackMasterChangesAndLog( $e );
                        $status = false;
index 4de19bc..3699afe 100644 (file)
@@ -35,6 +35,10 @@ class AssembleUploadChunksJob extends Job {
        public function run() {
                /** @noinspection PhpUnusedLocalVariableInspection */
                $scope = RequestContext::importScopedSession( $this->params['session'] );
+               $this->addTeardownCallback( function () use ( &$scope ) {
+                       ScopedCallback::consume( $scope ); // T126450
+               } );
+
                $context = RequestContext::getMain();
                $user = $context->getUser();
                try {
index 5f8af8b..caedf46 100644 (file)
@@ -37,6 +37,10 @@ class PublishStashedFileJob extends Job {
        public function run() {
                /** @noinspection PhpUnusedLocalVariableInspection */
                $scope = RequestContext::importScopedSession( $this->params['session'] );
+               $this->addTeardownCallback( function () use ( &$scope ) {
+                       ScopedCallback::consume( $scope ); // T126450
+               } );
+
                $context = RequestContext::getMain();
                $user = $context->getUser();
                try {