v0.3.5: update PollingAlarmReceiver.kt

This commit is contained in:
sanjeok77 committed 2026-08-25 02:11:03 +00:00
1 parent 34ba7b5a0f
commit 986e50088b
1 file changed
+25 -13
@@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import com.hotdeal.alarm.data.local.preferences.AppSettings
import com.hotdeal.alarm.domain.usecase.HotDealScraperManager
import com.hotdeal.alarm.worker.AlarmScheduler
import com.hotdeal.alarm.worker.WorkerScheduler
import dagger.hilt.android.AndroidEntryPoint
@@ -16,7 +17,9 @@ import kotlinx.coroutines.launch
import javax.inject.Inject
/**
* 주기적 알람 수신 시 백그라운드 스크래핑을 실행하고 다음 알람을 예약하는 리시버
* 주기적 알람 수신 시 백그라운드 스크래핑을 즉시 실행하고 다음 알람을 예약하는 리시버
*
* goAsync()를 사용하여 WorkManager의 Doze 지연 없이 백그라운드에서 즉시 핫딜을 수집하고 알림을 발송합니다.
*/
@AndroidEntryPoint
class PollingAlarmReceiver : BroadcastReceiver() {
@@ -26,7 +29,7 @@ class PollingAlarmReceiver : BroadcastReceiver() {
}
@Inject
lateinit var workerScheduler: WorkerScheduler
lateinit var scraperManager: HotDealScraperManager
@Inject
lateinit var alarmScheduler: AlarmScheduler
@@ -34,24 +37,33 @@ class PollingAlarmReceiver : BroadcastReceiver() {
@Inject
lateinit var appSettings: AppSettings
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private val receiverScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == AlarmScheduler.ACTION_POLL_ALARM) {
Log.d(TAG, "⏰ 폴링 알람 수신 -> 핫딜 스크래핑 1회 즉시 실행 및 다음 알람 예약")
Log.d(TAG, "⏰ [실시간 알람 수신] 백그라운드 스크래핑 즉시 시작 및 다음 알람 예약")
// 1. 핫딜 스크래핑 실행
workerScheduler.executeOnce()
val pendingResult = goAsync()
// 2. 다음 주기 알람 예약 (Self-rescheduling)
scope.launch {
receiverScope.launch {
try {
val interval = appSettings.pollingInterval.first().toLong()
alarmScheduler.scheduleAlarm(interval)
Log.d(TAG, "다음 폴링 알람 예약 완료: ${interval}분 후")
// 1. 백그라운드에서 직접 스크래핑, DB 저장, 키워드 알림 즉시 발송!
val newDeals = scraperManager.fetchAndProcessDeals()
Log.d(TAG, "⏰ [스크래핑 완료] 신규 핫딜 ${newDeals.size}개 처리됨")
} catch (e: Exception) {
Log.e(TAG, "다음 알람 예약 실패: ${e.message}")
alarmScheduler.scheduleAlarm(WorkerScheduler.DEFAULT_INTERVAL_MINUTES)
Log.e(TAG, "알람 처리 중 오류: ${e.message}", e)
} finally {
// 2. 다음 주기 알람 예약 (Self-rescheduling)
try {
val interval = appSettings.pollingInterval.first().toLong()
alarmScheduler.scheduleAlarm(interval)
Log.d(TAG, "⏰ 다음 폴링 알람 예약 완료: ${interval}분 후")
} catch (e: Exception) {
Log.e(TAG, "다음 알람 예약 실패: ${e.message}")
alarmScheduler.scheduleAlarm(WorkerScheduler.DEFAULT_INTERVAL_MINUTES)
}
pendingResult.finish()
}
}
}