Skip to content
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

refactor/lockfile #377

Merged
merged 3 commits into from
Dec 13, 2024
Merged

refactor/lockfile #377

merged 3 commits into from
Dec 13, 2024

Conversation

fcanela
Copy link
Contributor

@fcanela fcanela commented Dec 6, 2024

Extracts the Lockfile logic so it is easier to remove once we normalize the repository to be deployable to our infrastructure. Provides tests.

This PR is based in #375, so the diff may contain content from that PR if it has not been merged yet.


PR-Codex overview

This PR focuses on implementing a locking mechanism to prevent multiple instances of a process from running simultaneously. It adds functions for claiming and releasing locks, along with corresponding tests to ensure functionality.

Detailed summary

  • Added claimLock and releaseLock functions in lock.ts.
  • Implemented getLockFilePath to generate lock file paths.
  • Introduced LockfileExistsError for error handling.
  • Updated initialize function in relayerHelpers.ts to use locking functions.
  • Added tests for locking functionality in lock.test.ts.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Summary by CodeRabbit

  • New Features

    • Introduced a locking mechanism to manage concurrent processes, ensuring only one instance can run at a time.
    • Added a new Jest configuration file for improved testing capabilities, including coverage reporting.
    • New script added to package.json for running tests with coverage.
    • Updated ESLint configuration to recognize Jest global variables.
  • Bug Fixes

    • Streamlined shutdown management process by removing the previous ShutdownManager class and integrating lock management functions.
  • Tests

    • Added unit tests for the locking mechanism to ensure proper functionality and error handling.

@fcanela fcanela self-assigned this Dec 6, 2024
Copy link
Contributor

coderabbitai bot commented Dec 6, 2024

Walkthrough

This pull request introduces several changes to the relayer-cli project, including the addition of a .gitconfig entry for code coverage, a new Jest configuration file, and updates to the package.json to support testing with coverage. It also implements lock management functionality through new files and modifies existing files to integrate these features, including the removal of the ShutdownManager class and the introduction of related unit tests.

Changes

File Path Change Summary
relayer-cli/.gitconfig Added configuration entry for coverage.
relayer-cli/jest.config.ts Introduced a Jest configuration file with settings for TypeScript processing and coverage collection.
relayer-cli/package.json Added a new test script for Jest with coverage and several new devDependencies for testing.
relayer-cli/src/utils/lock.test.ts Introduced unit tests for the locking mechanism, covering functionalities like path generation and error handling.
relayer-cli/src/utils/lock.ts Added functions for managing lock files, including getLockFilePath, claimLock, and releaseLock, along with a custom error class.
relayer-cli/src/utils/relayerHelpers.ts Removed ShutdownManager class and integrated lock management functions into the initialization process.
relayer-cli/src/utils/shutdownManager.test.ts Introduced tests for the ShutdownManager class, focusing on its constructor and methods.
relayer-cli/src/utils/shutdownManager.ts Added ShutdownManager class with methods for managing shutdown state.

Possibly related PRs

Suggested labels

Type: Maintenance :construction:, Type: Feature🗿

Suggested reviewers

  • jaybuidl
  • alcercu

🐰 In the land of code, a change took flight,
With locks and tests, all set just right.
Coverage now tracked, in .gitconfig it stays,
A new Jest setup, for testing our ways.
So hop along, let’s code and cheer,
For features and fixes, we hold so dear! 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 71c5280 and 4b5b491.

📒 Files selected for processing (3)
  • .eslintrc.json (1 hunks)
  • relayer-cli/package.json (2 hunks)
  • relayer-cli/src/utils/shutdownManager.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • relayer-cli/src/utils/shutdownManager.test.ts
  • relayer-cli/package.json
🔇 Additional comments (2)
.eslintrc.json (2)

11-11: LGTM: Jest environment enabled

The addition of the Jest environment aligns with the PR's objective of introducing Jest testing framework.


8-11: Consider removing Mocha environment

Both "mocha": true and "jest": true environments are enabled. Since this PR introduces Jest as the testing framework, consider removing the Mocha environment to avoid confusion and potential conflicts with global test functions.

Let's verify if Mocha is still in use:


🪧 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. (Experiment)
  • @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 or @coderabbitai title 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

netlify bot commented Dec 6, 2024

Deploy Preview for veascan failed. Why did it fail? →

Name Link
🔨 Latest commit 3d6075c
🔍 Latest deploy log https://app.netlify.com/sites/veascan/deploys/675c0230e4ee810008a1bb58

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

🧹 Outside diff range and nitpick comments (5)
relayer-cli/package.json (1)

27-31: Review the necessity of adding yarn as a devDependency.

Including "yarn" in devDependencies is uncommon, as yarn is typically installed globally. This addition may cause version conflicts or redundancy.

Apply this diff to remove "yarn" from devDependencies:

{
  ...
  "devDependencies": {
    "@types/jest": "^29.5.14",
    "jest": "^29.7.0",
    "ts-jest": "^29.2.5",
    "ts-node": "^10.9.2",
-   "yarn": "^1.22.22"
  }
}
relayer-cli/src/utils/shutdownManager.test.ts (1)

33-35: Implement the pending test for triggerShutdown method.

The test marked as it.todo is pending implementation. Completing this test will enhance test coverage and ensure that triggerShutdown functions as expected.

relayer-cli/src/utils/lock.ts (1)

17-22: Pass error message to the superclass constructor and ensure consistent error naming.

Passing the error message to super() ensures proper error initialization. Also, setting the name property to match the class name improves error handling consistency.

Apply this diff to improve the error class:

export class LockfileExistsError extends Error {
  constructor(path: string) {
-   super();
-   this.message = `The application tried to claim the lockfile ${path} but it already exists. Please ensure no other instance is running and delete the lockfile before starting a new one.`;
-   this.name = "OnlyOneProcessError";
+   const message = `The application tried to claim the lockfile ${path} but it already exists. Please ensure no other instance is running and delete the lockfile before starting a new one.`;
+   super(message);
+   this.name = "LockfileExistsError";
  }
}
relayer-cli/src/utils/lock.test.ts (2)

4-20: Consider adding edge cases to getLockFilePath tests

The current tests cover the happy path and case sensitivity, but consider adding tests for:

  • Invalid network names (e.g., empty string, special characters)
  • Invalid chain IDs (e.g., negative numbers, zero)
  • Path traversal attempts

Example test cases:

it("should handle invalid network names", () => {
  expect(() => getLockFilePath("", 1)).toThrow();
  expect(() => getLockFilePath("../malicious", 1)).toThrow();
});

it("should handle invalid chain IDs", () => {
  expect(() => getLockFilePath("mainnet", -1)).toThrow();
  expect(() => getLockFilePath("mainnet", 0)).toThrow();
});
🧰 Tools
🪛 eslint

[error] 10-10: 'expect' is not defined.

(no-undef)


[error] 18-18: 'expect' is not defined.

(no-undef)


1-84: Well-structured tests supporting the Lockfile extraction

The test organization effectively supports the PR's objective of extracting Lockfile logic. The dependency injection pattern used throughout makes the tests maintainable and the lock management logic easily portable.

A few architectural suggestions:

  1. Consider moving the common test setup (network, chainId) to beforeEach blocks
  2. Consider adding integration tests that verify the complete lock lifecycle
  3. Document the expected filesystem structure in the test file
🧰 Tools
🪛 eslint

[error] 10-10: 'expect' is not defined.

(no-undef)


[error] 18-18: 'expect' is not defined.

(no-undef)


[error] 29-29: 'jest' is not defined.

(no-undef)


[error] 32-32: 'expect' is not defined.

(no-undef)


[error] 37-37: 'jest' is not defined.

(no-undef)


[error] 38-38: 'jest' is not defined.

(no-undef)


[error] 43-43: 'expect' is not defined.

(no-undef)


[error] 44-44: 'expect' is not defined.

(no-undef)


[error] 47-47: 'expect' is not defined.

(no-undef)


[error] 48-48: 'expect' is not defined.

(no-undef)


[error] 59-59: 'jest' is not defined.

(no-undef)


[error] 60-60: 'jest' is not defined.

(no-undef)


[error] 65-65: 'expect' is not defined.

(no-undef)


[error] 66-66: 'expect' is not defined.

(no-undef)


[error] 69-69: 'expect' is not defined.

(no-undef)


[error] 74-74: 'jest' is not defined.

(no-undef)


[error] 75-75: 'jest' is not defined.

(no-undef)


[error] 80-80: 'expect' is not defined.

(no-undef)


[error] 81-81: 'expect' is not defined.

(no-undef)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3eebb3f and 71c5280.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (8)
  • relayer-cli/.gitconfig (1 hunks)
  • relayer-cli/jest.config.ts (1 hunks)
  • relayer-cli/package.json (2 hunks)
  • relayer-cli/src/utils/lock.test.ts (1 hunks)
  • relayer-cli/src/utils/lock.ts (1 hunks)
  • relayer-cli/src/utils/relayerHelpers.ts (2 hunks)
  • relayer-cli/src/utils/shutdownManager.test.ts (1 hunks)
  • relayer-cli/src/utils/shutdownManager.ts (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • relayer-cli/.gitconfig
  • relayer-cli/jest.config.ts
🧰 Additional context used
🪛 eslint
relayer-cli/src/utils/shutdownManager.test.ts

[error] 7-7: 'expect' is not defined.

(no-undef)


[error] 12-12: 'expect' is not defined.

(no-undef)


[error] 17-17: 'expect' is not defined.

(no-undef)


[error] 24-24: 'expect' is not defined.

(no-undef)


[error] 29-29: 'expect' is not defined.

(no-undef)

relayer-cli/src/utils/lock.test.ts

[error] 10-10: 'expect' is not defined.

(no-undef)


[error] 18-18: 'expect' is not defined.

(no-undef)


[error] 29-29: 'jest' is not defined.

(no-undef)


[error] 32-32: 'expect' is not defined.

(no-undef)


[error] 37-37: 'jest' is not defined.

(no-undef)


[error] 38-38: 'jest' is not defined.

(no-undef)


[error] 43-43: 'expect' is not defined.

(no-undef)


[error] 44-44: 'expect' is not defined.

(no-undef)


[error] 47-47: 'expect' is not defined.

(no-undef)


[error] 48-48: 'expect' is not defined.

(no-undef)


[error] 59-59: 'jest' is not defined.

(no-undef)


[error] 60-60: 'jest' is not defined.

(no-undef)


[error] 65-65: 'expect' is not defined.

(no-undef)


[error] 66-66: 'expect' is not defined.

(no-undef)


[error] 69-69: 'expect' is not defined.

(no-undef)


[error] 74-74: 'jest' is not defined.

(no-undef)


[error] 75-75: 'jest' is not defined.

(no-undef)


[error] 80-80: 'expect' is not defined.

(no-undef)


[error] 81-81: 'expect' is not defined.

(no-undef)

🔇 Additional comments (3)
relayer-cli/src/utils/relayerHelpers.ts (1)

2-3: Imports updated to reflect new module structure.

The import statements correctly include claimLock, releaseLock, and ShutdownManager from their respective modules, aligning with the refactored codebase.

relayer-cli/src/utils/shutdownManager.ts (1)

1-15: Class implementation is clean and straightforward.

The ShutdownManager class correctly encapsulates the shutdown state with appropriate methods.

relayer-cli/package.json (1)

13-13: Testing script added successfully.

The "test" script enables running Jest with coverage, aligning with the testing additions in the project.

relayer-cli/src/utils/relayerHelpers.ts Show resolved Hide resolved
relayer-cli/src/utils/shutdownManager.test.ts Outdated Show resolved Hide resolved
relayer-cli/src/utils/shutdownManager.test.ts Outdated Show resolved Hide resolved
relayer-cli/src/utils/lock.ts Show resolved Hide resolved
relayer-cli/src/utils/lock.test.ts Show resolved Hide resolved
relayer-cli/src/utils/lock.test.ts Show resolved Hide resolved
relayer-cli/src/utils/lock.test.ts Show resolved Hide resolved
relayer-cli/src/utils/relayerHelpers.ts Show resolved Hide resolved
relayer-cli/package.json Outdated Show resolved Hide resolved
@mani99brar mani99brar merged commit 5041531 into dev Dec 13, 2024
10 of 15 checks passed
@mani99brar mani99brar deleted the refactor/lockfile branch December 16, 2024 09:59
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