/** * @param DelegatingSourceCleaner $cleaner * @param OriginInterface $origin * @param ThresholdVoterInterface $voter * * @return boolean */ public function cleanOrigin(DelegatingSourceCleaner $cleaner, OriginInterface $origin, ThresholdVoterInterface $voter) { if (null === ($expireDate = $this->getLastFullImportDate($origin))) { $this->logger->debug(sprintf('Skipping origin %s, because it has no recent imports', $origin->getTitle())); $this->eventDispatcher->dispatch(IoEvents::ORIGIN_CLEANUP_SKIP, new OriginCleanupEvent($origin, 0)); return false; } $this->eventDispatcher->dispatch(IoEvents::PRE_CLEAN_ORIGIN, new OriginEvent($origin)); $this->logger->debug(sprintf('Checking sources of %s that have not been visited since %s', $origin->getTitle(), $expireDate->format('Y-m-d H:i:s'))); // get sources that haven't been visited since $expireDate $sourceRepo = $this->sourceManager->getRepository(); $count = $sourceRepo->countByOriginAndUnvisitedSince($origin, $expireDate); // fail safe: see if percentage of sources to be removed is not too high $total = $sourceRepo->countByOrigin($origin); $max = $this->getThreshold($total); // see if threshold is reached if ($count > $max) { $message = sprintf('Stopping cleanup for origin %s, because %s of %s sources were to be deleted, %s is the maximum.', $origin->getTitle(), $count, $total, $max); if (!$voter->vote($count, $total, $max, $message)) { $this->eventDispatcher->dispatch(IoEvents::ORIGIN_CLEANUP_HALT, new OriginCleanupHaltEvent($origin, $count, $total, $max)); return false; } } $this->logger->debug(sprintf('Cleaning %d sources for origin %s', $count, $origin->getTitle())); $builder = $sourceRepo->queryByOriginAndUnvisitedSince($origin, $expireDate); $numCleaned = $cleaner->cleanByQuery($builder->getQuery()); $this->eventDispatcher->dispatch(IoEvents::POST_CLEAN_ORIGIN, new OriginCleanupEvent($origin, $numCleaned)); return $numCleaned; }
/** * @inheritdoc */ public function clean(DelegatingSourceCleaner $cleaner, ThresholdVoterInterface $voter) { $builder = $this->sourceManager->getRepository()->createQueryBuilder('s')->select('s')->innerJoin('s.origin', 'o')->where('SIZE(o.feeds) = 0'); return $cleaner->cleanByQuery($builder->getQuery()); }