1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata project. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\NotificationBundle\Command; |
13
|
|
|
|
14
|
|
|
use Sonata\NotificationBundle\Backend\BackendInterface; |
15
|
|
|
use Sonata\NotificationBundle\Event\IterateEvent; |
16
|
|
|
use Sonata\NotificationBundle\Iterator\ErroneousMessageIterator; |
17
|
|
|
use Sonata\NotificationBundle\Model\MessageManagerInterface; |
18
|
|
|
use Sonata\NotificationBundle\Selector\ErroneousMessagesSelector; |
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
|
24
|
|
|
class RestartCommand extends ContainerAwareCommand |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function configure() |
30
|
|
|
{ |
31
|
|
|
$this->setName('sonata:notification:restart'); |
32
|
|
|
$this->setDescription('Restart messages with erroneous statuses, only for doctrine backends'); |
33
|
|
|
$this->addOption('type', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'List of messages types to restart (separate multiple types with a space)'); |
34
|
|
|
$this->addOption('max-attempts', null, InputOption::VALUE_REQUIRED, 'Maximum number of attempts', 6); |
35
|
|
|
$this->addOption('attempt-delay', null, InputOption::VALUE_OPTIONAL, 'Min seconds between two attempts', 10); |
36
|
|
|
$this->addOption('pulling', null, InputOption::VALUE_NONE, 'Run the command as an infinite pulling loop'); |
37
|
|
|
$this->addOption('pause', null, InputOption::VALUE_OPTIONAL, 'Seconds between each data pull (used only when pulling option is set)', 500000); |
38
|
|
|
$this->addOption('batch-size', null, InputOption::VALUE_OPTIONAL, 'Number of message to process on each pull (used only when pulling option is set)', 10); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$output->writeln('<info>Starting... </info>'); |
47
|
|
|
|
48
|
|
|
if (!is_numeric($input->getOption('max-attempts'))) { |
49
|
|
|
throw new \Exception('Option "max-attempts" is invalid (integer value needed).'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$pullMode = $input->getOption('pulling'); |
53
|
|
|
$manager = $this->getMessageManager(); |
54
|
|
|
|
55
|
|
|
if ($pullMode) { |
56
|
|
|
$messages = new ErroneousMessageIterator( |
57
|
|
|
$manager, |
58
|
|
|
$input->getOption('type'), |
|
|
|
|
59
|
|
|
$input->getOption('pause'), |
60
|
|
|
$input->getOption('batch-size'), |
61
|
|
|
$input->getOption('max-attempts'), |
62
|
|
|
$input->getOption('attempt-delay')); |
63
|
|
|
} else { |
64
|
|
|
$messages = $this->getErroneousMessageSelector()->getMessages($input->getOption('type'), $input->getOption('max-attempts')); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (0 == count($messages)) { |
68
|
|
|
$output->writeln('Nothing to restart, bye.'); |
69
|
|
|
|
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$eventDispatcher = $this->getContainer()->get('event_dispatcher'); |
74
|
|
|
|
75
|
|
|
foreach ($messages as $message) { |
76
|
|
|
$id = $message->getId(); |
77
|
|
|
|
78
|
|
|
$newMessage = $manager->restart($message); |
79
|
|
|
|
80
|
|
|
$this->getBackend()->publish($newMessage); |
81
|
|
|
|
82
|
|
|
$output->writeln(sprintf('Reset Message %s <info>#%d</info>, new id %d. Attempt #%d', $newMessage->getType(), $id, $newMessage->getId(), $newMessage->getRestartCount())); |
83
|
|
|
|
84
|
|
|
if ($pullMode) { |
85
|
|
|
$eventDispatcher->dispatch(IterateEvent::EVENT_NAME, new IterateEvent($messages, null, $newMessage)); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$output->writeln('<info>Done!</info>'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return the erroneous message selector service. |
94
|
|
|
* |
95
|
|
|
* @return ErroneousMessagesSelector |
96
|
|
|
*/ |
97
|
|
|
protected function getErroneousMessageSelector() |
98
|
|
|
{ |
99
|
|
|
return $this->getContainer()->get('sonata.notification.erroneous_messages_selector'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return MessageManagerInterface |
104
|
|
|
*/ |
105
|
|
|
protected function getMessageManager() |
106
|
|
|
{ |
107
|
|
|
return $this->getContainer()->get('sonata.notification.manager.message'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return BackendInterface |
112
|
|
|
*/ |
113
|
|
|
protected function getBackend() |
114
|
|
|
{ |
115
|
|
|
return $this->getContainer()->get('sonata.notification.backend'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: