Skip to content

[messages] add some logging to the worker #202

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import androidx.work.WorkManager
import androidx.work.WorkRequest
import androidx.work.WorkerParameters
import me.capcom.smsgateway.R
import me.capcom.smsgateway.modules.logs.LogsService
import me.capcom.smsgateway.modules.logs.db.LogEntry
import me.capcom.smsgateway.modules.messages.MessagesService
import me.capcom.smsgateway.modules.notifications.NotificationsService
import org.koin.core.component.KoinComponent
Expand All @@ -20,19 +22,43 @@ import java.util.concurrent.TimeUnit
class SendMessagesWorker(appContext: Context, params: WorkerParameters) :
CoroutineWorker(appContext, params), KoinComponent {

private val logsService: LogsService by inject()
private val messagesSvc: MessagesService by inject()
private val notificationsSvc: NotificationsService by inject()

override suspend fun doWork(): Result {
logsService.insert(
LogEntry.Priority.DEBUG,
NAME,
"SendMessagesWorker started"
)

return try {
while (messagesSvc.sendPendingMessages()) {
// why not to `Result.retry()`?
logsService.insert(
LogEntry.Priority.DEBUG,
NAME,
"SendMessagesWorker started next iteration"
)
}

logsService.insert(
LogEntry.Priority.DEBUG,
NAME,
"SendMessagesWorker finished successfully"
)

Result.success()
} catch (e: Exception) {
e.printStackTrace()

logsService.insert(
LogEntry.Priority.ERROR,
NAME,
"SendMessagesWorker finished with error: ${e.message ?: e.toString()}"
)

Result.retry()
}
}
Expand Down