@@ -9,13 +9,13 @@ import javax.inject.Inject
99import javax.inject.Singleton
1010import kotlinx.coroutines.sync.Mutex
1111import kotlinx.coroutines.sync.withLock
12- import org.ole.planet.myplanet.data.DatabaseService
12+ import org.ole.planet.myplanet.repository.retry.RetryRepository
1313import org.ole.planet.myplanet.model.RealmRetryOperation
1414import org.ole.planet.myplanet.services.upload.UploadError
1515
1616@Singleton
1717class RetryQueue @Inject constructor(
18- private val databaseService : DatabaseService ,
18+ private val retryRepository : RetryRepository ,
1919 @ApplicationContext private val context : Context
2020) {
2121 companion object {
@@ -46,41 +46,16 @@ class RetryQueue @Inject constructor(
4646 return
4747 }
4848
49- val existingOperation = databaseService.withRealmAsync { realm ->
50- realm.where(RealmRetryOperation ::class .java)
51- .equalTo(" itemId" , error.itemId)
52- .equalTo(" uploadType" , uploadType)
53- .notEqualTo(" status" , RealmRetryOperation .STATUS_COMPLETED )
54- .notEqualTo(" status" , RealmRetryOperation .STATUS_ABANDONED )
55- .findFirst()
56- ?.let { realm.copyFromRealm(it) }
57- }
49+ val existingOperation = retryRepository.getExistingOperation(error.itemId, uploadType)
5850
5951 if (existingOperation != null ) {
60- databaseService.executeTransactionAsync { realm ->
61- realm.where(RealmRetryOperation ::class .java)
62- .equalTo(" id" , existingOperation.id)
63- .findFirst()?.let { op ->
64- op.attemptCount + = 1
65- op.lastAttemptTime = System .currentTimeMillis()
66- op.nextRetryTime = RealmRetryOperation .calculateNextRetryTime(op.attemptCount)
67- op.errorMessage = error.message
68- op.httpCode = error.httpCode
69-
70- if (op.attemptCount >= op.maxAttempts) {
71- op.status = RealmRetryOperation .STATUS_ABANDONED
72- Log .w(TAG , " Operation ${op.id} abandoned after ${op.maxAttempts} attempts" )
73- }
74- }
75- }
52+ retryRepository.updateAttempt(existingOperation.id, error)
7653 Log .d(TAG , " Updated existing retry operation for item ${error.itemId} " )
7754 } else {
78- databaseService.executeTransactionAsync { realm ->
79- RealmRetryOperation .createFromUploadError(
80- realm, uploadType, error, payload.toString(), endpoint,
81- httpMethod, dbId, modelClassName, userId
82- )
83- }
55+ retryRepository.enqueue(
56+ uploadType, error, payload.toString(), endpoint,
57+ httpMethod, dbId, modelClassName, userId
58+ )
8459 Log .i(TAG , " RETRY_QUEUE: Queued new operation - type=$uploadType , itemId=${error.itemId} , error=${error.message} " )
8560 }
8661 }
@@ -109,75 +84,32 @@ class RetryQueue @Inject constructor(
10984 }
11085
11186 suspend fun getPendingOperations (): List <RealmRetryOperation > {
112- return databaseService.withRealmAsync { realm ->
113- RealmRetryOperation .getPendingOperations(realm)
114- }
87+ return retryRepository.getPending()
11588 }
11689
11790 suspend fun getPendingCount (): Long {
118- return databaseService.withRealmAsync { realm ->
119- RealmRetryOperation .getFailedOperationsCount(realm)
120- }
91+ return retryRepository.getPendingCount()
12192 }
12293
12394 suspend fun markInProgress (operationId : String ) {
124- databaseService.executeTransactionAsync { realm ->
125- realm.where(RealmRetryOperation ::class .java)
126- .equalTo(" id" , operationId)
127- .findFirst()?.let { op ->
128- op.status = RealmRetryOperation .STATUS_IN_PROGRESS
129- }
130- }
95+ retryRepository.markInProgress(operationId)
13196 }
13297
13398 suspend fun markCompleted (operationId : String ) {
134- databaseService.executeTransactionAsync { realm ->
135- realm.where(RealmRetryOperation ::class .java)
136- .equalTo(" id" , operationId)
137- .findFirst()?.let { op ->
138- op.status = RealmRetryOperation .STATUS_COMPLETED
139- op.lastAttemptTime = System .currentTimeMillis()
140- }
141- }
99+ retryRepository.markCompleted(operationId)
142100 Log .d(TAG , " Marked operation $operationId as completed" )
143101 }
144102
145103 suspend fun markFailed (operationId : String , errorMessage : String? , httpCode : Int? ) {
146- databaseService.executeTransactionAsync { realm ->
147- realm.where(RealmRetryOperation ::class .java)
148- .equalTo(" id" , operationId)
149- .findFirst()?.let { op ->
150- op.attemptCount + = 1
151- op.lastAttemptTime = System .currentTimeMillis()
152- op.errorMessage = errorMessage
153- op.httpCode = httpCode
154-
155- if (op.attemptCount >= op.maxAttempts) {
156- op.status = RealmRetryOperation .STATUS_ABANDONED
157- Log .w(TAG , " Operation $operationId abandoned after ${op.maxAttempts} attempts" )
158- } else {
159- op.status = RealmRetryOperation .STATUS_PENDING
160- op.nextRetryTime = RealmRetryOperation .calculateNextRetryTime(op.attemptCount)
161- }
162- }
163- }
104+ retryRepository.markFailed(operationId, errorMessage, httpCode)
164105 }
165106
166107 suspend fun cleanup () {
167- databaseService.executeTransactionAsync { realm ->
168- RealmRetryOperation .cleanupCompletedOperations(realm)
169- }
108+ retryRepository.cleanup()
170109 }
171110
172111 suspend fun resetAllPending () {
173- databaseService.executeTransactionAsync { realm ->
174- realm.where(RealmRetryOperation ::class .java)
175- .equalTo(" status" , RealmRetryOperation .STATUS_PENDING )
176- .findAll()
177- .forEach { op ->
178- op.nextRetryTime = System .currentTimeMillis()
179- }
180- }
112+ retryRepository.resetAllPending()
181113 }
182114
183115 /* *
@@ -196,15 +128,7 @@ class RetryQueue @Inject constructor(
196128 return @withLock false
197129 }
198130
199- databaseService.executeTransactionAsync { realm ->
200- // Only delete pending and abandoned, not in_progress or completed
201- realm.where(RealmRetryOperation ::class .java)
202- .equalTo(" status" , RealmRetryOperation .STATUS_PENDING )
203- .or ()
204- .equalTo(" status" , RealmRetryOperation .STATUS_ABANDONED )
205- .findAll()
206- .deleteAllFromRealm()
207- }
131+ retryRepository.deletePendingAndAbandonedOperations()
208132 Log .i(TAG , " Queue cleared successfully" )
209133 true
210134 }
@@ -215,14 +139,6 @@ class RetryQueue @Inject constructor(
215139 * Called on app startup to recover from crashes.
216140 */
217141 suspend fun recoverStuckOperations () {
218- databaseService.executeTransactionAsync { realm ->
219- realm.where(RealmRetryOperation ::class .java)
220- .equalTo(" status" , RealmRetryOperation .STATUS_IN_PROGRESS )
221- .findAll()
222- .forEach { op ->
223- op.status = RealmRetryOperation .STATUS_PENDING
224- op.nextRetryTime = System .currentTimeMillis() + 60_000 // Retry in 1 minute
225- }
226- }
142+ retryRepository.recoverStuckOperations()
227143 }
228144}
0 commit comments