Skip to content

Commit

Permalink
[webhooks] add webhooks retry count option, see #125
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Oct 10, 2024
1 parent bd7e5b5 commit 910e4da
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class WebhooksSettings(
val internetRequired: Boolean
get() = storage.get<Boolean>(INTERNET_REQUIRED) ?: true

val retryCount: Int
get() = storage.get<Int>(RETRY_COUNT) ?: 15

companion object {
const val INTERNET_REQUIRED = "internet_required"
const val RETRY_COUNT = "retry_count"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import me.capcom.smsgateway.modules.logs.LogsService
import me.capcom.smsgateway.modules.logs.db.LogEntry
import me.capcom.smsgateway.modules.notifications.NotificationsService
import me.capcom.smsgateway.modules.webhooks.NAME
import me.capcom.smsgateway.modules.webhooks.WebhooksSettings
import me.capcom.smsgateway.modules.webhooks.domain.WebHookEventDTO
import org.json.JSONException
import org.koin.core.component.KoinComponent
Expand All @@ -45,6 +46,8 @@ class SendWebhookWorker(appContext: Context, params: WorkerParameters) :
private val notificationsSvc: NotificationsService by inject()
private val logsSvc: LogsService by inject()

private val settings: WebhooksSettings by inject()

override suspend fun doWork(): ListenableWorker.Result {
return when (val result = sendData()) {
Result.Success -> {
Expand Down Expand Up @@ -91,7 +94,7 @@ class SendWebhookWorker(appContext: Context, params: WorkerParameters) :

private suspend fun sendData(): Result {
try {
if (runAttemptCount > MAX_RETRIES) {
if (runAttemptCount >= settings.retryCount) {
return Result.Failure("Retry limit exceeded")
}

Expand Down Expand Up @@ -203,8 +206,6 @@ class SendWebhookWorker(appContext: Context, params: WorkerParameters) :

private val gson = GsonBuilder().configure().create()

private const val MAX_RETRIES = 14

private const val INPUT_URL = "url"
private const val INPUT_DATA = "data"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class SettingsFragment : PreferenceFragmentCompat() {

if (preference.key == "ping.interval_seconds"
|| preference.key == "logs.lifetime_days"
|| preference.key == "webhooks.retry_count"
) {
(preference as EditTextPreference).setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@
<string name="webhooks">Webhooks</string>
<string name="the_webhook_request_will_wait_for_an_internet_connection">The webhook request will wait for an internet connection</string>
<string name="require_internet_connection">Require Internet connection</string>
<string name="more_settings">More settings...</string>
<string name="delays_limits_etc">Delays, Limits, etc.</string>
<string name="retry_count">Retry count</string>
</resources>
8 changes: 7 additions & 1 deletion app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
<PreferenceCategory app:title="@string/messages_header">
<Preference
app:fragment="me.capcom.smsgateway.ui.settings.MessagesSettingsFragment"
app:title="More settings..." />
app:title="@string/more_settings"
app:summary="@string/delays_limits_etc"/>
</PreferenceCategory>

<PreferenceCategory app:title="@string/webhooks">
Expand All @@ -33,6 +34,11 @@
android:key="webhooks.internet_required"
app:summary="@string/the_webhook_request_will_wait_for_an_internet_connection"
app:title="@string/require_internet_connection" />
<EditTextPreference
android:key="webhooks.retry_count"
app:title="@string/retry_count"
app:defaultValue="15"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

<PreferenceCategory
Expand Down

0 comments on commit 910e4da

Please sign in to comment.