Skip to content

V4 dequeue performance (return faster) #1989

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

Merged
merged 2 commits into from
Apr 28, 2025
Merged

Conversation

matt-aitken
Copy link
Member

@matt-aitken matt-aitken commented Apr 28, 2025

When dequeuing in v4 we now only look at each tenant and queue a maximum of one time, returning as soon as we have enough messages.

This prevents a situation where we have stale queues and keep considering them again and again. We want to return ASAP so we can get fresh queues again. Instead focusing on having multiple separate simultaneous calls to the overall dequeuing.

Summary by CodeRabbit

  • Refactor
    • Simplified task triggering logic, reducing the number of triggered tasks in certain workflows.
    • Streamlined message queue processing for improved efficiency and reliability.
  • Chores
    • Removed unused environment variable and related configuration options.

Copy link

changeset-bot bot commented Apr 28, 2025

⚠️ No Changeset found

Latest commit: 3973927

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Apr 28, 2025

Error: Could not generate a valid Mermaid diagram after multiple attempts.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@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: 2

🧹 Nitpick comments (2)
internal-packages/run-engine/src/run-queue/index.ts (1)

395-436: Single-pass dequeue may under-deliver messages

The refactor iterates over each queue exactly once. If maxCount is 10 but there are 5 envs × 1 message ready per queue, the loop will collect only 5 and return early, forcing another round-trip to Redis on the next API call.

Unless that latency is desirable, consider repeating the traversal until either
a) messages.length === maxCount or
b) no additional messages were dequeued in the last pass.

- for (const env of envQueues) {
+ let progress = true;
+ while (progress && messages.length < maxCount) {
+   progress = false;
+   for (const env of envQueues) {
       ...
       if (message) {
         messages.push(message);
+        progress = true;
       }
-     // If we've reached maxCount, we don't want to look at this env anymore
+     if (messages.length >= maxCount) break;
   }
 }

This preserves the “don’t spin forever” goal while still maximising throughput.

references/hello-world/src/trigger/deadlocks.ts (1)

27-29: Queue saturation risk

deadlockNestedTask is pinned to deadlockQueue (concurrency = 1). If the mutual-wait loop above is not addressed, the single slot becomes blocked indefinitely and every subsequent trigger will queue-pile, effectively acting as a DoS on that queue.

Consider:

  • Raising the concurrency, or
  • Enabling releaseConcurrencyOnWaitpoint: true, or
  • Removing the circular wait.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3d744c6 and 3973927.

📒 Files selected for processing (6)
  • apps/webapp/app/env.server.ts (0 hunks)
  • apps/webapp/app/v3/runEngine.server.ts (0 hunks)
  • internal-packages/run-engine/src/engine/index.ts (0 hunks)
  • internal-packages/run-engine/src/engine/types.ts (0 hunks)
  • internal-packages/run-engine/src/run-queue/index.ts (2 hunks)
  • references/hello-world/src/trigger/deadlocks.ts (1 hunks)
💤 Files with no reviewable changes (4)
  • internal-packages/run-engine/src/engine/types.ts
  • apps/webapp/app/v3/runEngine.server.ts
  • apps/webapp/app/env.server.ts
  • internal-packages/run-engine/src/engine/index.ts
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - npm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: units / 🧪 Unit Tests
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (1)
internal-packages/run-engine/src/run-queue/index.ts (1)

33-34:

✅ Verification successful

Confirm tryCatch contract & import path

tryCatch is destructured as [error, message], implying it returns a tuple in the form [Error | undefined, T | undefined].

  1. Please double-check that the helper exported from @trigger.dev/core actually follows that signature and is await-able – some utility wrappers are synchronous. A mismatch will break the runtime destructuring.
  2. If the return type is a discriminated union ({ error?; data? }) or throws, this new code will silently mis-handle errors.

Run the snippet below to grep for the helper definition and verify its return type:


🏁 Script executed:

#!/bin/bash
ast-grep --pattern $'export function tryCatch($$$) {' | head -n 20

Length of output: 67


🏁 Script executed:

#!/bin/bash
set -e
# Search for all occurrences of tryCatch to locate its definition and signature
rg "tryCatch" -n -C3 | head -n 50

Length of output: 3950


🏁 Script executed:

#!/bin/bash
set -e
# Find where tryCatch is defined and inspect its signature (including async flag)
rg -E "export (async )?(function|const) tryCatch" -n . | head -n 20

Length of output: 157


🏁 Script executed:

#!/bin/bash
# Search for tryCatch definition in the monorepo (excluding node_modules)
rg --ignore-dir node_modules "export.*tryCatch" -n . || true
rg --ignore-dir node_modules "const tryCatch" -n . || true
rg --ignore-dir node_modules "function tryCatch" -n . || true

Length of output: 1513


🏁 Script executed:

#!/bin/bash
set -e
# List top-level directories and packages to locate the core package
echo "Root directory contents:"
ls -1
echo
echo "packages directory contents:"
ls -1 packages

Length of output: 561


🏁 Script executed:

#!/bin/bash
set -e
echo "Searching for tryCatch in the core package source:"
rg "tryCatch" -n packages/core/src | head -n 50

Length of output: 3281


🏁 Script executed:

#!/bin/bash
set -e
echo "=== tryCatch implementation (packages/core/src/v3/tryCatch.ts) ==="
sed -n '1,200p' packages/core/src/v3/tryCatch.ts || true
echo
echo "=== Utils re-export (packages/core/src/utils.ts) ==="
sed -n '1,200p' packages/core/src/utils.ts || true
echo
echo "=== Result type definitions in core ==="
rg "type Result" -n packages/core/src || true

Length of output: 1487


🏁 Script executed:

#!/bin/bash
set -e
echo "packages/core/src contents:"
ls -1 packages/core/src || true

echo
echo "packages/core/src/index.ts (if exists):"
if [ -f packages/core/src/index.ts ]; then
  sed -n '1,200p' packages/core/src/index.ts
else
  echo "No index.ts in packages/core/src"
fi

echo
echo "package.json fields in packages/core:"
grep -E '"(main|exports|module|types)"' -n packages/core/package.json || true

echo
echo "First 200 lines of packages/core/package.json:"
sed -n '1,200p' packages/core/package.json

Length of output: 11030


Confirmed tryCatch is async and returns the expected tuple – no changes needed
The helper in @trigger.dev/core/src/utils.ts (and re-exported in src/index.ts) is declared as

export async function tryCatch<T, E = Error>(
  promise: Promise<T> | undefined
): Promise<[null, T] | [E, null]> {  }

– it is await-able, always returns a two-element tuple, and never throws. Importing it directly from @trigger.dev/core is valid. No updates required.

Comment on lines +407 to 421
const [error, message] = await tryCatch(
this.#callDequeueMessage({
messageQueue: queue,
})
);

if (error) {
this.logger.error(
`[dequeueMessageInSharedQueue][${this.name}] Failed to dequeue from queue ${queue}`,
{
error,
}
);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Errors are swallowed – consider surfacing them to callers

tryCatch logs the error but then execution continues as if nothing happened.
In production this can create “black-hole” failures where a queue is permanently unhealthy yet no call-site notices. At minimum:

 if (error) {
   this.logger.error(
     `[dequeueMessageInSharedQueue][${this.name}] Failed to dequeue from queue ${queue}`,
     {
       error,
     }
   );
+  // Bubble a warning to the tracing span so alerts can fire
+  span.recordException(error as Error);
 }

Better: propagate a metric / circuit-breaker so repeated failures trigger recovery logic.

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +17 to +19
await deadlockNestedTask.triggerAndWait({
message: "Hello, world!",
});
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Infinite mutual triggering – system can still dead-lock

deadlockTester now triggers deadlockNestedTask once (instead of twice) but nothing prevents the callee from immediately triggering the caller again (see lines 27-29). Without a depth or retry guard this still creates an unbounded recursion chain:

Tester → Nested → Tester → Nested …​

Add a counter or a unique correlation id to detect and stop after n hops, or use trigger (fire-and-forget) instead of triggerAndWait if synchronous wait isn’t required.

@matt-aitken matt-aitken merged commit f579afb into main Apr 28, 2025
11 of 13 checks passed
@matt-aitken matt-aitken deleted the v4-dequeue-performance branch April 28, 2025 13:49
@coderabbitai coderabbitai bot mentioned this pull request Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants