You are here

public function QueueExampleForm::submitRunCron in Examples for Developers 8

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

queue_example/src/Forms/QueueExampleForm.php, line 390

Class

QueueExampleForm
Form with examples on how to use queue.

Namespace

Drupal\queue_example\Forms

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();
}