Skip to content

[messages] fix queue stuck on scheduled messages#362

Merged
capcom6 merged 1 commit intomasterfrom
messages/fix-send-message-scheduling
May 5, 2026
Merged

[messages] fix queue stuck on scheduled messages#362
capcom6 merged 1 commit intomasterfrom
messages/fix-send-message-scheduling

Conversation

@capcom6
Copy link
Copy Markdown
Owner

@capcom6 capcom6 commented Apr 29, 2026

Summary by CodeRabbit

  • Refactor
    • Improved scheduling logic: the system more accurately determines the next scheduled send time.
    • Expedited send behavior updated: a message can be treated as expedited when no future scheduled send exists or by priority.
    • Worker start behavior adjusted: enqueuing messages more consistently triggers the send worker to run.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 29, 2026

🤖 Pull request artifacts

file commit
app-release.apk c3dbc59
app-release.aab c3dbc59
app-insecure.apk c3dbc59
app-insecure.aab c3dbc59

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 29, 2026

Walkthrough

DAO SQL for computing the next scheduled send was changed to coalesce NULL scheduleAt inside MIN() and the table name capitalized; service scheduling logic was altered to change when SendMessagesWorker is started and how expedited decisions use nextScheduled.

Changes

Cohort / File(s) Summary
MessagesDao SQL Query
app/src/main/java/me/capcom/smsgateway/data/dao/MessagesDao.kt
nextScheduledTime() SQL now uses MIN(COALESCE(\scheduleAt`, '1970-01-01T00:00:00.000Z'))withstrftimeand converts to milliseconds;FROM messageFROM Message. Method signature remains Long?`.
MessagesService Scheduling Logic
app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt
start now calls SendMessagesWorker with true. enqueueMessage no longer computes earliest; it treats nextScheduled as positive-or-null, starts a future send only if scheduleAt is future and scheduleAt < nextScheduled (or when nextScheduled absent/0), and sets expedited when `nextScheduled == null

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant MessagesService
  participant MessagesDao
  participant SendMessagesWorker

  Client->>MessagesService: enqueueMessage(message, scheduleAt?, priority)
  MessagesService->>MessagesDao: nextScheduledTime()
  MessagesDao-->>MessagesService: nextScheduled (ms or null/0)
  alt scheduleAt in future AND (nextScheduled == null OR scheduleAt < nextScheduled)
    MessagesService->>SendMessagesWorker: start(expedited=true, scheduleAt)
  else
    alt nextScheduled == null OR priority >= PRIORITY_EXPEDITED
      MessagesService->>SendMessagesWorker: start(expedited=true)
    else
      MessagesService-->>Client: enqueue without expedited start
    end
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

codex

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title '[messages] fix queue stuck on scheduled messages' directly addresses the main issue being fixed: scheduling logic for messages that was causing the queue to become stuck.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch messages/fix-send-message-scheduling

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt (1)

99-102: Remove dead code.

This commented-out block appears to be remnants of the old implementation and should be removed to keep the codebase clean.

🧹 Suggested cleanup
     val nextScheduled = dao.nextScheduledTime()?.takeIf { it > 0 }

-//        val earliest = request.params.scheduleAt?.let {
-//            val nextScheduled = dao.nextScheduledTime() ?: 0
-//            nextScheduled != 0L && it.time < nextScheduled
-//        } ?: true
-
     val message = try {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt`
around lines 99 - 102, Remove the dead commented-out block that defines the
local variable "earliest" in MessagesService (the lines referencing
request.params.scheduleAt, dao.nextScheduledTime(), and nextScheduled); delete
the entire commented section so the file no longer contains this stale
implementation and commit the cleanup to keep MessagesService.kt tidy.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt`:
- Around line 99-102: Remove the dead commented-out block that defines the local
variable "earliest" in MessagesService (the lines referencing
request.params.scheduleAt, dao.nextScheduledTime(), and nextScheduled); delete
the entire commented section so the file no longer contains this stale
implementation and commit the cleanup to keep MessagesService.kt tidy.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e901868c-54e0-437c-8082-d212c3698b6e

📥 Commits

Reviewing files that changed from the base of the PR and between 3f82b13 and 6464029.

📒 Files selected for processing (2)
  • app/src/main/java/me/capcom/smsgateway/data/dao/MessagesDao.kt
  • app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt`:
- Around line 97-113: The code collapses the distinct dao.nextScheduledTime()
states by using .takeIf { it > 0 }; revert to val nextScheduled =
dao.nextScheduledTime() (so 0 stays distinct from null) and change the
future-scheduling comparison to use scheduleAt < (nextScheduled ?:
Long.MAX_VALUE) in the block that calls SendMessagesWorker.start, keeping the
existing else-branch logic that checks nextScheduled == null || priority >=
Message.PRIORITY_EXPEDITED; this preserves the three-state semantics (null, 0,
>0) and avoids cancelling an in-flight worker.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1761504d-bf6d-4d03-b5a0-c6bc5f9ffb82

📥 Commits

Reviewing files that changed from the base of the PR and between 6464029 and 88c5ae7.

📒 Files selected for processing (1)
  • app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt

@capcom6 capcom6 force-pushed the messages/fix-send-message-scheduling branch from 88c5ae7 to c3dbc59 Compare April 30, 2026 02:27
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt (1)

97-113: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Preserve nextScheduled three-state semantics (null, 0, >0).

Line 97 collapses 0 to null, which breaks the distinction introduced by MessagesDao.nextScheduledTime() and can cause incorrect force/replace behavior at Line 113.

Suggested fix
-        val nextScheduled = dao.nextScheduledTime()?.takeIf { it > 0 }
+        val nextScheduled = dao.nextScheduledTime()
@@
         if (scheduleAt != null
             && scheduleAt > System.currentTimeMillis()
-            && scheduleAt < (nextScheduled ?: 0)
+            && scheduleAt < (nextScheduled ?: Long.MAX_VALUE)
         ) {
             SendMessagesWorker.start(context, true, scheduleAt)
         } else {
             SendMessagesWorker.start(
                 context,
-                nextScheduled == null || priority >= Message.PRIORITY_EXPEDITED,
+                priority >= Message.PRIORITY_EXPEDITED,
                 SendMessagesWorker.IMMEDIATE
             )
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt`
around lines 97 - 113, The code collapses a zero nextScheduled to null by using
takeIf { it > 0 }, losing the three-state semantics (null, 0, >0); replace val
nextScheduled = dao.nextScheduledTime()?.takeIf { it > 0 } with the raw value
from MessagesDao (e.g., val nextScheduled = dao.nextScheduledTime()) and update
the comparison in the scheduling branch to treat a null as "no scheduled time"
by using a safe fallback (for example compare scheduleAt < (nextScheduled ?:
Long.MAX_VALUE)); keep the later check that uses nextScheduled == null ||
priority >= Message.PRIORITY_EXPEDITED unchanged so the original null/0/>0
semantics are preserved for SendMessagesWorker.start.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt`:
- Around line 97-113: The code collapses a zero nextScheduled to null by using
takeIf { it > 0 }, losing the three-state semantics (null, 0, >0); replace val
nextScheduled = dao.nextScheduledTime()?.takeIf { it > 0 } with the raw value
from MessagesDao (e.g., val nextScheduled = dao.nextScheduledTime()) and update
the comparison in the scheduling branch to treat a null as "no scheduled time"
by using a safe fallback (for example compare scheduleAt < (nextScheduled ?:
Long.MAX_VALUE)); keep the later check that uses nextScheduled == null ||
priority >= Message.PRIORITY_EXPEDITED unchanged so the original null/0/>0
semantics are preserved for SendMessagesWorker.start.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7dd5b5bc-57d0-46d2-a10b-ef91ac345dcf

📥 Commits

Reviewing files that changed from the base of the PR and between 88c5ae7 and c3dbc59.

📒 Files selected for processing (2)
  • app/src/main/java/me/capcom/smsgateway/data/dao/MessagesDao.kt
  • app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt

@capcom6 capcom6 merged commit 3722a64 into master May 5, 2026
4 checks passed
@capcom6 capcom6 deleted the messages/fix-send-message-scheduling branch May 5, 2026 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant