Skip to content

Fix profiles worker #2708

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 5 commits into from
Dec 4, 2024
Merged

Fix profiles worker #2708

merged 5 commits into from
Dec 4, 2024

Conversation

sausage-todd
Copy link
Contributor

@sausage-todd sausage-todd commented Dec 4, 2024

Summary by CodeRabbit

  • New Features

    • Extended timeout for member update workflows to allow longer-running operations.
    • Improved handling of activity attributes and body length in data insertion processes.
  • Bug Fixes

    • Enhanced robustness of data handling to prevent errors during insertion.
  • Documentation

    • Updated method signatures for improved flexibility in query handling.

Copy link

coderabbitai bot commented Dec 4, 2024

Walkthrough

The pull request introduces several changes across different files. The memberUpdate.ts file updates the timeout for proxyActivities, extending it from 60 seconds to 60 minutes. In ilp.ts, the insertActivities function is modified to improve attribute handling and limit the body field length to 2000 characters. The sql.ts file alters the getClientSQL function's query method to accept variable arguments, enhancing its flexibility. Error handling remains consistent across all changes, ensuring robustness in data processing and workflow execution.

Changes

File Path Change Summary
services/apps/profiles_worker/src/workflows/member/memberUpdate.ts Updated startToCloseTimeout from 60 seconds to 60 minutes for proxyActivities.
services/libs/data-access-layer/src/activities/ilp.ts Modified insertActivities to replace objectToBytes with tryToUnwrapAttributes, limit body length to 2000 characters, and added type checks for input. Added function tryToUnwrapAttributes.
services/libs/questdb/src/sql.ts Updated getClientSQL to modify query method to accept variable arguments, enhancing flexibility.

Possibly related PRs

Suggested reviewers

  • themarolt
  • epipav

🐰 In the meadow where the bunnies play,
A timeout stretched, now longer each day.
With attributes fixed and queries that flow,
Our data dances, putting on a show!
So hop along, let's celebrate,
For changes made, oh, they are great! 🌼


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0f97a54 and 1e945c5.

📒 Files selected for processing (1)
  • services/libs/data-access-layer/src/activities/ilp.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • services/libs/data-access-layer/src/activities/ilp.ts

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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 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

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

🧹 Outside diff range and nitpick comments (2)
services/libs/data-access-layer/src/activities/ilp.ts (2)

208-211: Improve type checking in objectToBytes

The current implementation might allow non-string/non-object types through.

Consider adding more robust type checking:

- if (typeof input !== 'object') {
-   return input
- }
+ if (typeof input !== 'object' || input === null) {
+   return typeof input === 'string' ? input : JSON.stringify(input);
+ }

48-48: Update attribute handling chain

The chaining of objectToBytes(tryToUnfuckAttributes()) could be simplified with the new implementation.

Consider:

- .stringColumn('attributes', objectToBytes(tryToUnfuckAttributes(activity.attributes)))
+ .stringColumn('attributes', objectToBytes(parseAttributes(activity.attributes)))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9774498 and 0f97a54.

📒 Files selected for processing (3)
  • services/apps/profiles_worker/src/workflows/member/memberUpdate.ts (1 hunks)
  • services/libs/data-access-layer/src/activities/ilp.ts (3 hunks)
  • services/libs/questdb/src/sql.ts (1 hunks)
🔇 Additional comments (1)
services/apps/profiles_worker/src/workflows/member/memberUpdate.ts (1)

10-10: ⚠️ Potential issue

Investigate root cause for requiring 60-minute timeout

The timeout increase from 60 seconds to 60 minutes is concerning and might be masking underlying performance issues. While this change may temporarily fix timeout issues, it could lead to:

  • Resource exhaustion from long-running operations
  • Delayed error detection
  • Poor user experience

Consider:

  1. Investigating why member updates take longer than expected
  2. Adding performance monitoring
  3. Implementing partial updates or batching if dealing with large datasets

Let's check for potential performance bottlenecks:

Comment on lines +57 to +61
;(client as any).query = async (query, options, ...args) => {
const { replacements } = options || {}
const timer = telemetry.timer('questdb.query_duration')
try {
return oldQuery.apply(client, [query, options])
return oldQuery.apply(client, [query, options, ...args])
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation for variable arguments

While adding support for variable arguments increases flexibility, it could lead to issues if invalid arguments are passed. Consider:

  1. Validating args before passing to oldQuery
  2. Adding type safety for the args parameter
  3. Documenting expected argument types

Here's a suggested improvement:

-  (client as any).query = async (query, options, ...args) => {
+  (client as any).query = async (query: string, options?: Record<string, unknown>, ...args: unknown[]) => {
+    if (args.some(arg => arg === undefined)) {
+      throw new Error('Undefined arguments are not allowed');
+    }
     const { replacements } = options || {}
     const timer = telemetry.timer('questdb.query_duration')
     try {
       return oldQuery.apply(client, [query, options, ...args])
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
;(client as any).query = async (query, options, ...args) => {
const { replacements } = options || {}
const timer = telemetry.timer('questdb.query_duration')
try {
return oldQuery.apply(client, [query, options])
return oldQuery.apply(client, [query, options, ...args])
(client as any).query = async (query: string, options?: Record<string, unknown>, ...args: unknown[]) => {
if (args.some(arg => arg === undefined)) {
throw new Error('Undefined arguments are not allowed');
}
const { replacements } = options || {}
const timer = telemetry.timer('questdb.query_duration')
try {
return oldQuery.apply(client, [query, options, ...args])

Comment on lines 219 to 227
function tryToUnfuckAttributes(attributes: string | object): object {
// eslint-disable-next-line no-constant-condition
while (true) {
if (typeof attributes === 'object') {
return attributes
}
attributes = JSON.parse(attributes)
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Fix infinite loop risk in attribute parsing

The tryToUnfuckAttributes function has several critical issues:

  1. Infinite loop risk if JSON.parse results in a string
  2. No error handling for invalid JSON
  3. Unprofessional function naming

Replace with this safer implementation:

- function tryToUnfuckAttributes(attributes: string | object): object {
-   // eslint-disable-next-line no-constant-condition
-   while (true) {
-     if (typeof attributes === 'object') {
-       return attributes
-     }
-     attributes = JSON.parse(attributes)
-   }
- }
+ function parseAttributes(attributes: string | object): object {
+   try {
+     if (typeof attributes === 'object') {
+       return attributes || {};
+     }
+     const parsed = JSON.parse(attributes);
+     return typeof parsed === 'object' ? parsed : {};
+   } catch (error) {
+     return {};
+   }
+ }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function tryToUnfuckAttributes(attributes: string | object): object {
// eslint-disable-next-line no-constant-condition
while (true) {
if (typeof attributes === 'object') {
return attributes
}
attributes = JSON.parse(attributes)
}
}
function parseAttributes(attributes: string | object): object {
try {
if (typeof attributes === 'object') {
return attributes || {};
}
const parsed = JSON.parse(attributes);
return typeof parsed === 'object' ? parsed : {};
} catch (error) {
return {};
}
}

@@ -122,7 +122,7 @@
}

if (activity.body) {
row.stringColumn('body', activity.body)
row.stringColumn('body', activity.body.slice(0, 2000))
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add warning for truncated body content

The body field is silently truncated to 2000 characters, which could lead to data loss without notification.

Consider:

  1. Logging when truncation occurs
  2. Making the limit configurable
  3. Preserving important content
- row.stringColumn('body', activity.body.slice(0, 2000))
+ const MAX_BODY_LENGTH = 2000;
+ if (activity.body.length > MAX_BODY_LENGTH) {
+   telemetry.increment('activity.body.truncated', 1);
+ }
+ row.stringColumn('body', activity.body.slice(0, MAX_BODY_LENGTH))

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

@sausage-todd sausage-todd merged commit 521ff8a into main Dec 4, 2024
7 checks passed
@sausage-todd sausage-todd deleted the fix-profiles-worker branch December 4, 2024 09:10
@coderabbitai coderabbitai bot mentioned this pull request Jan 10, 2025
5 tasks
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.

1 participant