Skip to content

Commit 1f63af4

Browse files
authored
[framework] feed modules are now removed if appropriate feed no longer exists (#3024)
1 parent 25651fa commit 1f63af4

File tree

4 files changed

+91
-8
lines changed

4 files changed

+91
-8
lines changed

src/Model/Feed/FeedCronModule.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Shopsys\FrameworkBundle\Component\Domain\Domain;
88
use Shopsys\FrameworkBundle\Component\Setting\Setting;
9+
use Shopsys\FrameworkBundle\Model\Feed\Exception\FeedNotFoundException;
910
use Shopsys\Plugin\Cron\IteratedCronModuleInterface;
1011
use Symfony\Bridge\Monolog\Logger;
1112

@@ -24,12 +25,14 @@ class FeedCronModule implements IteratedCronModuleInterface
2425
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
2526
* @param \Shopsys\FrameworkBundle\Component\Setting\Setting $setting
2627
* @param \Shopsys\FrameworkBundle\Model\Feed\FeedModuleRepository $feedModuleRepository
28+
* @param \Shopsys\FrameworkBundle\Model\Feed\FeedModuleFacade $feedModuleFacade
2729
*/
2830
public function __construct(
2931
protected readonly FeedFacade $feedFacade,
3032
protected readonly Domain $domain,
3133
protected readonly Setting $setting,
3234
protected readonly FeedModuleRepository $feedModuleRepository,
35+
protected readonly FeedModuleFacade $feedModuleFacade,
3336
) {
3437
}
3538

@@ -60,6 +63,10 @@ public function iterate(): bool
6063
if ($this->currentFeedExport === null) {
6164
$this->currentFeedExport = $this->createCurrentFeedExport();
6265

66+
if ($this->currentFeedExport === null) {
67+
return false;
68+
}
69+
6370
$this->logger->info(sprintf(
6471
'Started generation of feed "%s" generated on domain "%s" into "%s".',
6572
$this->currentFeedExport->getFeedInfo()->getName(),
@@ -94,6 +101,10 @@ public function iterate(): bool
94101
if ($existsNext === true) {
95102
$this->currentFeedExport = $this->createCurrentFeedExport();
96103

104+
if ($this->currentFeedExport === null) {
105+
return false;
106+
}
107+
97108
$this->logger->info(sprintf(
98109
'Started generation of feed "%s" generated on domain "%s" into "%s".',
99110
$this->currentFeedExport->getFeedInfo()->getName(),
@@ -166,6 +177,11 @@ public function wakeUp(): void
166177

167178
$lastSeekId = $this->setting->get(Setting::FEED_ITEM_ID_TO_CONTINUE);
168179
$this->currentFeedExport = $this->createCurrentFeedExport($lastSeekId);
180+
181+
if ($this->currentFeedExport === null) {
182+
return;
183+
}
184+
169185
$this->currentFeedExport->wakeUp();
170186

171187
$this->logger->info(sprintf(
@@ -178,15 +194,31 @@ public function wakeUp(): void
178194

179195
/**
180196
* @param int|null $lastSeekId
181-
* @return \Shopsys\FrameworkBundle\Model\Feed\FeedExport
197+
* @return \Shopsys\FrameworkBundle\Model\Feed\FeedExport|null
182198
*/
183-
protected function createCurrentFeedExport(?int $lastSeekId = null): FeedExport
199+
protected function createCurrentFeedExport(?int $lastSeekId = null): ?FeedExport
184200
{
185-
return $this->feedFacade->createFeedExport(
186-
$this->getFeedExportCreationDataQueue()->getCurrentFeedName(),
187-
$this->getFeedExportCreationDataQueue()->getCurrentDomain(),
188-
$lastSeekId,
189-
);
201+
try {
202+
$feedExport = $this->feedFacade->createFeedExport(
203+
$this->getFeedExportCreationDataQueue()->getCurrentFeedName(),
204+
$this->getFeedExportCreationDataQueue()->getCurrentDomain(),
205+
$lastSeekId,
206+
);
207+
} catch (FeedNotFoundException $e) {
208+
$this->logger->error($e->getMessage());
209+
210+
$this->feedModuleFacade->deleteFeedCronModulesByName($this->getFeedExportCreationDataQueue()->getCurrentFeedName());
211+
212+
$isNextFeedInQueue = $this->getFeedExportCreationDataQueue()->next();
213+
214+
if ($isNextFeedInQueue === false) {
215+
return null;
216+
}
217+
218+
return $this->createCurrentFeedExport();
219+
}
220+
221+
return $feedExport;
190222
}
191223

192224
/**

src/Model/Feed/FeedModuleFacade.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopsys\FrameworkBundle\Model\Feed;
6+
7+
use Doctrine\ORM\EntityManagerInterface;
8+
9+
class FeedModuleFacade
10+
{
11+
/**
12+
* @param \Doctrine\ORM\EntityManagerInterface $em
13+
* @param \Shopsys\FrameworkBundle\Model\Feed\FeedModuleRepository $feedModuleRepository
14+
*/
15+
public function __construct(
16+
protected readonly EntityManagerInterface $em,
17+
protected readonly FeedModuleRepository $feedModuleRepository,
18+
) {
19+
}
20+
21+
/**
22+
* @param string $feedName
23+
*/
24+
public function deleteFeedCronModulesByName(string $feedName): void
25+
{
26+
$feedModules = $this->feedModuleRepository->findFeedModulesByName($feedName);
27+
28+
foreach ($feedModules as $feedModule) {
29+
$this->em->remove($feedModule);
30+
}
31+
32+
$this->em->flush();
33+
}
34+
}

src/Model/Feed/FeedModuleRepository.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,15 @@ public function getAllScheduledFeedModules(): array
8686
{
8787
return $this->getFeedModuleRepository()->findBy(['scheduled' => true]);
8888
}
89+
90+
/**
91+
* @param string $feedName
92+
* @return \Shopsys\FrameworkBundle\Model\Feed\FeedModule[]
93+
*/
94+
public function findFeedModulesByName(string $feedName): array
95+
{
96+
return $this->getFeedModuleRepository()->findBy([
97+
'name' => $feedName,
98+
]);
99+
}
89100
}

tests/Unit/Model/Feed/FeedCronModuleTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Shopsys\FrameworkBundle\Model\Feed\FeedFacade;
1515
use Shopsys\FrameworkBundle\Model\Feed\FeedInfoInterface;
1616
use Shopsys\FrameworkBundle\Model\Feed\FeedModule;
17+
use Shopsys\FrameworkBundle\Model\Feed\FeedModuleFacade;
1718
use Shopsys\FrameworkBundle\Model\Feed\FeedModuleRepository;
1819
use Symfony\Bridge\Monolog\Logger;
1920
use Tests\FrameworkBundle\Unit\TestCase;
@@ -67,7 +68,12 @@ public function testSleepExactBetweenFeeds(): void
6768
$feedFacadeMock->expects($this->any())->method('scheduleFeedsForCurrentTime');
6869
$feedFacadeMock->expects($this->any())->method('markFeedModuleAsUnscheduled');
6970

70-
$feedCronModule = new FeedCronModule($feedFacadeMock, $domain, $settingMock, $feedModuleRepositoryMock);
71+
$feedModuleFacadeMock = $this->getMockBuilder(FeedModuleFacade::class)
72+
->disableOriginalConstructor()
73+
->setMethods(['deleteFeedCronModulesByName'])
74+
->getMock();
75+
76+
$feedCronModule = new FeedCronModule($feedFacadeMock, $domain, $settingMock, $feedModuleRepositoryMock, $feedModuleFacadeMock);
7177
$feedCronModule->setLogger($logger);
7278

7379
$feedCronModule->iterate();

0 commit comments

Comments
 (0)