protected function dispatchIfNotExists($id, $parameters, $domain, $locale) { if (null === $domain) { $domain = 'messages'; } $id = (string) $id; if ($this->getCatalogue()->defines($id, $domain)) { return; } $event = new MissingTranslationEvent($id, $parameters, $domain, $locale === null ? $this->getCatalogue()->getLocale() : $locale); $this->eventDispatcher->dispatch(MissingTranslationEvent::EVENT_NAME, $event); }
/** * Flushes all queue changes to the server. * * @param \SplQueue $queue */ public function flush(\SplQueue $queue) { if ($this->locked) { $this->memcache->delete('queue.manager'); $this->memcache->set('queue.manager', serialize($queue)); $this->locked = false; $this->memcache->delete('queue.manager.lock'); } else { $this->eventDispatcher->dispatch('queue.manager.flushed', new ManagerFlushedEvent(ManagerFlushedEvent::STATUS_FLUSH_FAILED_NOLOCK)); } }
/** * Clears all idle workers. */ protected function clearIdleWorkers() { /** * @var int $index * @var Process $worker */ for ($i = 0; $i < count($this->workers); $i++) { $worker = $this->workers[$i]; if (!$worker) { continue; } $processName = array_keys(array_filter($this->processList, function ($item) use($worker) { $exploded = explode(" ", $worker->getCommandLine()); $name = $exploded[count($exploded) - 2]; return $item->getName() == $name; }))[0]; if (!$worker->isRunning() && $worker->isSuccessful()) { $this->eventDispatcher->dispatch('foreman.process.finished', new ProcessFinishedEvent($processName, $this->verbose ? $worker->getOutput() : '')); if ($this->verbose) { $this->output->writeln('Output: ' . $worker->getOutput()); } unset($worker); $this->workers[$i] = null; unset($this->processList[$processName]); } elseif (!$worker->isRunning() && !$worker->isSuccessful()) { $this->eventDispatcher->dispatch('foreman.process.failed', new ProcessFailedEvent($processName, 'FAILED', $worker->getOutput())); if (!empty($worker->getOutput())) { $this->output->writeln('<error>Output: ' . $worker->getOutput() . '</error>'); } unset($worker); $this->workers[$i] = null; unset($this->processList[$processName]); } elseif ($worker->isRunning()) { try { $worker->checkTimeout(); } catch (ProcessTimedOutException $e) { $this->eventDispatcher->dispatch('foreman.process.failed', new ProcessFailedEvent($processName, 'TIMEOUT')); unset($worker); $this->workers[$i] = null; unset($this->processList[$processName]); } } } }
/** * Removes Idle workers (finished and timed out). * * @param Process[] $workers * @return int Number of cleared workers. */ public function clearIdleWorkers(array &$workers) { $cleared = 0; //Clears the current foreach ($workers as $name => &$worker) { if (!$worker->isRunning()) { $this->eventDispatcher->dispatch('queue.process_ended', new ProcessFinishedEvent($name, $worker->getOutput(), ProcessFinishedEvent::STATUS_TIMEOUT)); $worker = null; $cleared++; } else { try { $worker->checkTimeout(); } catch (ProcessTimedOutException $e) { $this->eventDispatcher->dispatch('queue.process_failed', new ProcessFinishedEvent($name, $worker->getOutput(), ProcessFinishedEvent::STATUS_TIMEOUT)); $worker = null; $cleared++; } } } $workers = array_filter($workers, function ($item) { return $item !== null; }); return $cleared; }
public function testDispatchReusedEventNested() { $nestedCall = false; $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $dispatcher->addListener('foo', function (Event $e) use($dispatcher) { $dispatcher->dispatch('bar', $e); }); $dispatcher->addListener('bar', function (Event $e) use(&$nestedCall) { $nestedCall = true; }); $this->assertFalse($nestedCall); $dispatcher->dispatch('foo'); $this->assertTrue($nestedCall); }
public function testListenerCanRemoveItselfWhenExecuted() { $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $listener1 = function ($event, $eventName, EventDispatcherInterface $dispatcher) use(&$listener1) { $dispatcher->removeListener('foo', $listener1); }; $eventDispatcher->addListener('foo', $listener1); $eventDispatcher->addListener('foo', function () { }); $eventDispatcher->dispatch('foo'); $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed'); }