function QueueExampleForm::submitRunCron

Same name in other branches
  1. 8.x-1.x queue_example/src/Forms/QueueExampleForm.php \Drupal\queue_example\Forms\QueueExampleForm::submitRunCron()
  2. 4.0.x modules/queue_example/src/Form/QueueExampleForm.php \Drupal\queue_example\Form\QueueExampleForm::submitRunCron()

Submit function for "run cron" button.

Runs cron (to release expired claims) and reports the results.

Parameters

array $form: Form definition array.

\Drupal\Core\Form\FormStateInterface $form_state: Form state object.

File

modules/queue_example/src/Form/QueueExampleForm.php, line 390

Class

QueueExampleForm
Form with examples on how to use queue.

Namespace

Drupal\queue_example\Form

Code

public function submitRunCron(array &$form, FormStateInterface $form_state) {
    $this->cron
        ->run();
    $queue = $this->queueFactory
        ->get($form_state->getValue('queue_name'));
    // @see https://www.drupal.org/node/2705809
    if ($queue instanceof QueueGarbageCollectionInterface) {
        $queue->garbageCollection();
    }
    // There is no harm in trying to recreate existing.
    $queue->createQueue();
    $count = $queue->numberOfItems();
    $this->messenger()
        ->addMessage($this->t('Ran cron. If claimed items expired, they should be expired now. There are now @count items in the queue', [
        '@count' => $count,
    ]));
    $form_state->setRebuild();
}