-
Notifications
You must be signed in to change notification settings - Fork 0
[Feature] Bloomed & RAIN_FORECAST_ALERT Push Notification #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fc777a7
feat: push notification 2
char-yb add9800
Merge branch 'develop' into feature/push-noitification-2
char-yb e70ecd1
[Refactor&Feature] Refactor notification schedulers and support dual โฆ
char-yb 367186e
[Refactor&Feature] Add RAIN_FORECAST_ALERT and orchestrate 18:00 notiโฆ
char-yb 1ce922c
[Feature] Add BLOOMED_SPOT_ALERT push for 3km nearby users on BLOOMEDโฆ
char-yb d4c98d0
[Refactor] Optimize notification pipeline bottlenecks with fail-open โฆ
char-yb 3a4c8f7
fix: timezone, require
char-yb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
pida-core/core-api/src/main/kotlin/com/pida/scheduler/BloomedNotificationScheduler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.pida.scheduler | ||
|
|
||
| import com.pida.notification.bloomed.BloomedNotificationService | ||
| import com.pida.support.extension.logger | ||
| import org.springframework.scheduling.annotation.Scheduled | ||
| import org.springframework.stereotype.Component | ||
|
|
||
| /** | ||
| * ๋ง๊ฐํ์ด์ ์๋ฆผ ์ค์ผ์ค๋ฌ | ||
| * | ||
| * ๋งค์ผ ์ค์ 9์์ ์คํํ์ฌ BLOOMED ๋น์จ 80% ์ด์ ์ง์ญ ์๋ฆผ์ ๋ฐ์กํฉ๋๋ค. | ||
| * ํ๋ฃจ 1ํ๋ง ์คํ๋๋๋ก ์ค๋ณต ์คํ์ ๋ฐฉ์งํฉ๋๋ค. | ||
| */ | ||
| @Component | ||
| class BloomedNotificationScheduler( | ||
| private val bloomedNotificationService: BloomedNotificationService, | ||
| ) : DailyNotificationScheduler() { | ||
| private val logger by logger() | ||
|
|
||
| @Scheduled(cron = "0 0 9 * * *") | ||
| fun executeBloomedNotification() = | ||
| executeOncePerDay( | ||
| logger = logger, | ||
| failureMessage = "Failed to execute bloomed notification scheduler", | ||
| ) { | ||
| bloomedNotificationService.sendBloomedNotifications() | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
pida-core/core-api/src/main/kotlin/com/pida/scheduler/EveningNotificationScheduler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package com.pida.scheduler | ||
|
|
||
| import com.pida.notification.rain.RainForecastNotificationService | ||
| import com.pida.support.extension.logger | ||
| import org.springframework.scheduling.annotation.Scheduled | ||
| import org.springframework.stereotype.Component | ||
| import java.time.DayOfWeek | ||
| import java.time.LocalDate | ||
|
|
||
| /** | ||
| * ์ ๋ ํธ์ ์๋ฆผ ์ค์ผ์คํธ๋ ์ดํฐ | ||
| * | ||
| * ๋งค์ผ 18:00์ ์คํ๋๋ฉฐ ๋ถ์ฐ ๋ฝ ์๋์์ ์คํ ์์๋ฅผ ๊ณ ์ ํฉ๋๋ค. | ||
| */ | ||
| @Component | ||
| class EveningNotificationScheduler( | ||
| private val notificationExecutionLock: NotificationExecutionLock, | ||
| private val weekdayNotificationScheduler: WeekdayNotificationScheduler, | ||
| private val rainForecastNotificationService: RainForecastNotificationService, | ||
| ) { | ||
| private val logger by logger() | ||
|
|
||
| @Scheduled(cron = "0 0 18 * * *") | ||
| fun executeEveningNotifications() { | ||
| val today = LocalDate.now() | ||
| val result = | ||
| notificationExecutionLock.runWithEveningLock(date = today) { | ||
| executeInOrder(today) | ||
| } | ||
|
|
||
| when (result) { | ||
| NotificationExecutionLock.ExecutionResult.ACQUIRED -> { | ||
| logger.info("Evening notification orchestration completed with distributed lock: date=$today") | ||
| } | ||
|
|
||
| NotificationExecutionLock.ExecutionResult.SKIPPED_BY_CONTENTION -> { | ||
| logger.info("Skipped evening notification orchestration due to lock contention: date=$today") | ||
| } | ||
|
|
||
| NotificationExecutionLock.ExecutionResult.FAIL_OPEN -> { | ||
| logger.warn("Evening notification orchestration executed in fail-open mode: date=$today") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun executeInOrder(today: LocalDate) { | ||
| if (isWeekday(today.dayOfWeek)) { | ||
| weekdayNotificationScheduler.executeWeekdayNotification(today) | ||
| } else { | ||
| logger.info("Skipping weekday notification flow on weekend: ${today.dayOfWeek}") | ||
| } | ||
|
|
||
| runSchedulerSafely( | ||
| logger = logger, | ||
| failureMessage = "Failed to execute rain forecast notification scheduler", | ||
| ) { | ||
| rainForecastNotificationService.sendRainForecastNotifications() | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private fun isWeekday(dayOfWeek: DayOfWeek): Boolean = | ||
| dayOfWeek in | ||
| setOf( | ||
| DayOfWeek.MONDAY, | ||
| DayOfWeek.TUESDAY, | ||
| DayOfWeek.WEDNESDAY, | ||
| DayOfWeek.THURSDAY, | ||
| DayOfWeek.FRIDAY, | ||
| ) | ||
| } | ||
89 changes: 89 additions & 0 deletions
89
pida-core/core-api/src/main/kotlin/com/pida/scheduler/NotificationExecutionLock.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package com.pida.scheduler | ||
|
|
||
| import com.pida.support.extension.logger | ||
| import org.redisson.api.RedissonClient | ||
| import org.springframework.beans.factory.annotation.Qualifier | ||
| import org.springframework.stereotype.Component | ||
| import java.time.LocalDate | ||
| import java.time.format.DateTimeFormatter | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| /** | ||
| * ์ ๋ ์๋ฆผ ์ค์ผ์ค๋ฌ ์คํ ๊ฒฝํฉ ๋ฐฉ์ง๋ฅผ ์ํ ๋ถ์ฐ ๋ฝ | ||
| */ | ||
| @Component | ||
| class NotificationExecutionLock( | ||
| @param:Qualifier("coreRedissonClient") | ||
| private val redissonClient: RedissonClient, | ||
| ) { | ||
| private val logger by logger() | ||
|
|
||
| companion object { | ||
| private val DATE_FORMATTER: DateTimeFormatter = DateTimeFormatter.BASIC_ISO_DATE | ||
| private const val LOCK_KEY_PREFIX = "notification:evening" | ||
| private const val WAIT_SECONDS = 1L | ||
| private const val LEASE_SECONDS = 3600L | ||
| } | ||
|
|
||
| enum class ExecutionResult { | ||
| ACQUIRED, | ||
| SKIPPED_BY_CONTENTION, | ||
| FAIL_OPEN, | ||
| } | ||
|
|
||
| fun runWithEveningLock( | ||
| date: LocalDate = LocalDate.now(), | ||
| action: () -> Unit, | ||
| ): ExecutionResult { | ||
| val lockKey = "$LOCK_KEY_PREFIX:${date.format(DATE_FORMATTER)}" | ||
|
|
||
| val lock = | ||
| try { | ||
| redissonClient.getLock(lockKey) | ||
| } catch (error: RuntimeException) { | ||
| logger.error( | ||
| "Failed to create evening notification lock. Executing action with fail-open: key=$lockKey, date=$date, errorType=${error::class.simpleName}", | ||
| error, | ||
| ) | ||
| action() | ||
| return ExecutionResult.FAIL_OPEN | ||
| } | ||
|
|
||
| val acquired = | ||
| try { | ||
| lock.tryLock(WAIT_SECONDS, LEASE_SECONDS, TimeUnit.SECONDS) | ||
| } catch (error: InterruptedException) { | ||
| Thread.currentThread().interrupt() | ||
| logger.warn("Interrupted while acquiring evening notification lock: $lockKey", error) | ||
| return ExecutionResult.SKIPPED_BY_CONTENTION | ||
| } catch (error: RuntimeException) { | ||
| logger.error( | ||
| "Failed to acquire evening notification lock. Executing action with fail-open: key=$lockKey, date=$date, errorType=${error::class.simpleName}", | ||
| error, | ||
| ) | ||
| action() | ||
| return ExecutionResult.FAIL_OPEN | ||
| } | ||
|
|
||
| if (!acquired) { | ||
| logger.info("Skipped evening notifications due to lock contention: $lockKey") | ||
| return ExecutionResult.SKIPPED_BY_CONTENTION | ||
| } | ||
|
|
||
| return try { | ||
| action() | ||
| ExecutionResult.ACQUIRED | ||
| } finally { | ||
| runCatching { | ||
| if (lock.isHeldByCurrentThread) { | ||
| lock.unlock() | ||
| } | ||
| }.onFailure { error -> | ||
| logger.warn( | ||
| "Failed to release evening notification lock: $lockKey, errorType=${error::class.simpleName}", | ||
| error, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restrict manual trigger endpoints to non-production/admin-only access.
Lines 61-137 add powerful POST endpoints that can trigger bulk notifications. If exposed in production without strict guardrails, this is abuse-prone and can cause user-impacting spam/cost incidents.
๐ Suggested hardening
๐ค Prompt for AI Agents