Elgg\BatchUpgrader::run PHP Method

run() public method

Run single upgrade batch
public run ( ElggUpgrade $upgrade ) : void
$upgrade ElggUpgrade Upgrade to run
return void
    public function run(ElggUpgrade $upgrade)
    {
        // Upgrade also disabled data, so the compatibility is
        // preserved in case the data ever gets enabled again
        $ha = access_get_show_hidden_status();
        access_show_hidden_entities(true);
        $started = microtime(true);
        // Get the class taking care of the actual upgrading
        $batch = $upgrade->getBatch();
        if (!$batch) {
            throw new \RuntimeException(elgg_echo('admin:upgrades:error:invalid_batch', [$upgrade->title, $upgrade->guid]));
        }
        $count = $batch->countItems();
        $batch_failure_count = 0;
        $batch_success_count = 0;
        $errors = [];
        $processed = (int) $upgrade->processed;
        $offset = (int) $upgrade->offset;
        $has_errors = (bool) $upgrade->has_errors;
        while ($count > $processed && microtime(true) - $started < $this->config->get('batch_run_time_in_secs')) {
            $result = $batch->run(new Result(), $offset);
            $failure_count = $result->getFailureCount();
            $success_count = $result->getSuccessCount();
            $batch_failure_count += $failure_count;
            $batch_success_count += $success_count;
            $total = $failure_count + $success_count;
            if ($batch::INCREMENT_OFFSET) {
                // Offset needs to incremented by the total amount of processed
                // items so the upgrade we won't get stuck upgrading the same
                // items over and over.
                $offset += $total;
            } else {
                // Offset doesn't need to be incremented, so we mark only
                // the items that caused a failure.
                $offset += $failure_count;
            }
            if ($failure_count > 0) {
                $has_errors = true;
            }
            $processed += $total;
            $errors = array_merge($errors, $result->getErrors());
        }
        access_show_hidden_entities($ha);
        $upgrade->processed = $processed;
        $upgrade->offset = $offset;
        $upgrade->has_errors = $has_errors;
        $completed = $processed >= $batch->countItems();
        if ($completed) {
            // Upgrade is finished
            if ($has_errors) {
                // The upgrade was finished with errors. Reset offset
                // and errors so the upgrade can start from a scratch
                // if attempted to run again.
                $upgrade->processed = 0;
                $upgrade->offset = 0;
                $upgrade->has_errors = false;
            } else {
                // Everything has been processed without errors
                // so the upgrade can be marked as completed.
                $upgrade->setCompleted();
            }
        }
        // Give feedback to the user interface about the current batch.
        return array('errors' => $errors, 'numErrors' => $batch_failure_count, 'numSuccess' => $batch_success_count);
    }

Usage Example

Example #1
0
 public function testCanRunUnincrementedUpgrade()
 {
     $upgrade = new ElggUpgrade();
     $upgrade->setClass(TestNoIncrementBatch::class);
     $upgrade->setId("test_plugin:2016101901");
     $upgrade->title = 'test_plugin:upgrade:2016101901:title';
     $upgrade->description = 'test_plugin:upgrade:2016101901:title';
     $upgrade->save();
     $config = _elgg_services()->config;
     $upgrader = new BatchUpgrader($config);
     $result = $upgrader->run($upgrade);
     $expected = ['errors' => [0, 10, 20, 30], 'numErrors' => 40, 'numSuccess' => 60];
     $this->assertEquals($expected, $result);
 }
BatchUpgrader