Skip to content

Commit ff31882

Browse files
Don't run other scheduled things if flow is running
1 parent c41c7ae commit ff31882

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

src/main/kotlin/com/stefanbratanov/sofiasupermarketsapi/scheduled/ScheduledAlcoholRetriever.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ import org.springframework.stereotype.Controller
99

1010
@Log
1111
@Controller
12-
class ScheduledAlcoholRetriever(val alcoholController: AlcoholController) {
12+
class ScheduledAlcoholRetriever(
13+
val alcoholController: AlcoholController,
14+
val flowsRunner: ScheduledFlowsRunner,
15+
) {
1316

1417
@Scheduled(cron = "\${alcohol.retriever.cron}")
1518
fun retrieveAlcohol() {
1619
log.info("Scheduled to retrieve alcohol products")
20+
if (flowsRunner.isRunning()) {
21+
log.info("Skipping because the supermarkets flow is running")
22+
return
23+
}
1724
val productCriteria = ProductCriteria(null, false)
1825
alcoholController.alcohol(productCriteria, null, true)
1926
}

src/main/kotlin/com/stefanbratanov/sofiasupermarketsapi/scheduled/ScheduledFlowsRunner.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ class ScheduledFlowsRunner(val flows: Map<Supermarket, SupermarketFlow>) {
2424
flows.forEach { (_, flow) -> flow.runSafely() }
2525
isRunning.set(false)
2626
}
27+
28+
fun isRunning(): Boolean {
29+
return isRunning.get()
30+
}
2731
}

src/main/kotlin/com/stefanbratanov/sofiasupermarketsapi/scheduled/ScheduledImagesVerifier.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ class ScheduledImagesVerifier(
1515
val googleImageSearch: GoogleImageSearch,
1616
val cacheManager: CacheManager,
1717
val productImageRepository: ProductImageRepository,
18+
val flowsRunner: ScheduledFlowsRunner,
1819
) {
1920

2021
@Scheduled(cron = "\${image.verifier.cron}")
2122
@Suppress("UNCHECKED_CAST")
2223
fun verifyImages() {
2324
log.info("Scheduled to verify images")
25+
if (flowsRunner.isRunning()) {
26+
log.info("Skipping because the supermarkets flow is running")
27+
return
28+
}
2429
val productImagesCache =
2530
cacheManager.getCache("productImages")?.nativeCache as MutableMap<String?, Any?>?
2631

src/test/kotlin/com/stefanbratanov/sofiasupermarketsapi/scheduled/ScheduledAlcoholRetrieverTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import org.junit.jupiter.api.extension.ExtendWith
1414
internal class ScheduledAlcoholRetrieverTest {
1515

1616
@MockK lateinit var alcoholController: AlcoholController
17+
@MockK lateinit var flowsRunner: ScheduledFlowsRunner
1718

1819
@InjectMockKs lateinit var scheduledAlcoholRetriever: ScheduledAlcoholRetriever
1920

2021
@Test
2122
fun `retrieves alcohol`() {
23+
every { flowsRunner.isRunning() } returns false
2224
every { alcoholController.alcohol(any(), any(), any()) } returns emptyList()
2325

2426
scheduledAlcoholRetriever.retrieveAlcohol()

src/test/kotlin/com/stefanbratanov/sofiasupermarketsapi/scheduled/ScheduledImagesVerifierTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ internal class ScheduledImagesVerifierTest {
3737

3838
@MockkBean lateinit var productImageRepository: ProductImageRepository
3939

40+
@MockkBean lateinit var flowsRunner: ScheduledFlowsRunner
41+
4042
@Autowired lateinit var underTest: ScheduledImagesVerifier
4143

4244
@Autowired lateinit var cacheManager: CacheManager
@@ -49,6 +51,7 @@ internal class ScheduledImagesVerifierTest {
4951
cache["test"] = invalidUrl
5052
cache["another test"] = "https://bbc.co.uk"
5153

54+
every { flowsRunner.isRunning() } returns false
5255
every { googleImageSearch.search("test", false) } returns "https://www.telegraph.co.uk/"
5356
every { productImageRepository.findById("test") } returns
5457
Optional.of(ProductImage("test", invalidUrl))

0 commit comments

Comments
 (0)