Processing a queue programmatically

Now that we have our queue worker that deletes teams (for all it knows, the teams don't even have to be without any players), we can explore how we can process this queue ourselves if we don't want the cron option. If we wanted it to be processed using a Drush command, we would not have to write that ourselves. Drush comes with one, and it would work like this:

drush queue-run team_cleaner

However, we may want to create an admin interface, a form of some kind, which allows the user to trigger the queue processing. In that case, we could do something like this:

$queue = \Drupal::queue('team_cleaner'); /** @var QueueWorkerInterface $queue_worker */ $queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance('team_cleaner'); ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.