53c9ab7ae432edb604944ebdc49e8b8b47e2d1cd
[yaffs-website] / vendor / drupal / console / src / Command / Queue / RunCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Queue\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Queue;
9
10 use Drupal\Console\Core\Command\Command;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Core\Queue\QueueWorkerManagerInterface;
15 use Drupal\Core\Queue\QueueFactory;
16
17 /**
18  * Class RunCommand
19  *
20  * @package Drupal\Console\Command\Queue
21  */
22 class RunCommand extends Command
23 {
24     /**
25      * @var QueueWorkerManagerInterface
26      */
27     protected $queueWorker;
28
29
30     /**
31      * @var QueueFactory
32      */
33     protected $queueFactory;
34
35     /**
36      * DebugCommand constructor.
37      *
38      * @param QueueWorkerManagerInterface $queueWorker
39      * @param QueueFactory                $queue
40      */
41     public function __construct(
42         QueueWorkerManagerInterface $queueWorker,
43         QueueFactory $queueFactory
44     ) {
45         $this->queueWorker = $queueWorker;
46         $this->queueFactory = $queueFactory;
47         parent::__construct();
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     protected function configure()
54     {
55         $this
56             ->setName('queue:run')
57             ->setDescription($this->trans('commands.queue.run.description'))
58             ->addArgument(
59                 'name',
60                 InputArgument::OPTIONAL,
61                 $this->trans('commands.queue.run.arguments.name')
62             )->setAliases(['qr']);
63     }
64
65     /**
66      * {@inheritdoc}
67      */
68     protected function execute(InputInterface $input, OutputInterface $output)
69     {
70         $name = $input->getArgument('name');
71
72         if (!$name) {
73             $this->getIo()->error(
74                 $this->trans('commands.queue.run.messages.missing-name')
75             );
76
77             return 1;
78         }
79
80         try {
81             $worker = $this->queueWorker->createInstance($name);
82             $queue = $this->queueFactory->get($name);
83         } catch (\Exception $e) {
84             $this->getIo()->error(
85                 sprintf(
86                     $this->trans('commands.queue.run.messages.invalid-name'),
87                     $name
88                 )
89             );
90
91             return 1;
92         }
93
94         $start = microtime(true);
95         $result = $this->runQueue($queue, $worker);
96         $time = microtime(true) - $start;
97
98         if (!empty($result['error'])) {
99             $this->getIo()->error(
100                 sprintf(
101                     $this->trans('commands.queue.run.messages.failed'),
102                     $name,
103                     $result['error']
104                 )
105             );
106
107             return 1;
108         }
109
110         $this->getIo()->success(
111             sprintf(
112                 $this->trans('commands.queue.run.success'),
113                 $name,
114                 $result['count'],
115                 $result['total'],
116                 round($time, 2)
117             )
118         );
119
120         return 0;
121     }
122
123     /**
124      * @param \Drupal\Core\Queue\QueueInterface       $queue
125      * @param \Drupal\Core\Queue\QueueWorkerInterface $worker
126      *
127      * @return array
128      */
129     private function runQueue($queue, $worker)
130     {
131         $result['count'] = 0;
132         $result['total'] = $queue->numberOfItems();
133         while ($item = $queue->claimItem()) {
134             try {
135                 $worker->processItem($item->data);
136                 $queue->deleteItem($item);
137                 $result['count']++;
138             } catch (SuspendQueueException $e) {
139                 $queue->releaseItem($item);
140                 $result['error'] = $e;
141             }
142         }
143
144         return $result;
145     }
146 }