-
Notifications
You must be signed in to change notification settings - Fork 7
refactor/lockfile #377
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
refactor/lockfile #377
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { claimLock, getLockFilePath, LockfileExistsError, releaseLock } from "./lock"; | ||
|
|
||
| describe("Lock", () => { | ||
| describe("getLockFilePath", () => { | ||
| it("should return the lock file path for a given network and chain id", () => { | ||
| const network = "mainnet"; | ||
| const chainId = 1; | ||
|
|
||
| const result = getLockFilePath(network, chainId); | ||
| expect(result).toBe("./state/mainnet_1.pid"); | ||
| }); | ||
|
|
||
| it("should ensure the network name is lowercase", () => { | ||
| const network = "MAINNET"; | ||
| const chainId = 1; | ||
|
|
||
| const result = getLockFilePath(network, chainId); | ||
| expect(result).toBe("./state/mainnet_1.pid"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("claimLock", () => { | ||
| const network = "mainnet"; | ||
| const chainId = 1; | ||
| const expectedLockFilePath = getLockFilePath(network, chainId); | ||
|
|
||
| it("should throw an error if the lockfile already exists", () => { | ||
| const deps = { | ||
| fileExistsFn: jest.fn().mockReturnValue(true), | ||
| }; | ||
|
|
||
| expect(() => claimLock(network, chainId, deps)).toThrow(LockfileExistsError); | ||
| }); | ||
|
|
||
| it("should write a file with the PID if none exists", () => { | ||
| const deps = { | ||
| fileExistsFn: jest.fn().mockReturnValue(false), | ||
| writeFileFn: jest.fn(), | ||
| }; | ||
|
|
||
| claimLock(network, chainId, deps); | ||
|
|
||
| expect(deps.fileExistsFn).toHaveBeenCalledTimes(1); | ||
| expect(deps.writeFileFn).toHaveBeenCalledTimes(1); | ||
|
|
||
| const [path, pid] = deps.writeFileFn.mock.calls[0]; | ||
| expect(path).toBe(expectedLockFilePath); | ||
| expect(pid).toBe(process.pid.toString()); | ||
| }); | ||
| }); | ||
mani99brar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe("releaseLock", () => { | ||
| const network = "mainnet"; | ||
| const chainId = 1; | ||
| const expectedLockFilePath = getLockFilePath(network, chainId); | ||
|
|
||
| it("should remove the lockfile if it exists", () => { | ||
| const deps = { | ||
| fileExistsFn: jest.fn().mockReturnValue(true), | ||
| unlinkFileFn: jest.fn(), | ||
| }; | ||
|
|
||
| releaseLock(network, chainId, deps); | ||
|
|
||
| expect(deps.fileExistsFn).toHaveBeenCalledTimes(1); | ||
| expect(deps.unlinkFileFn).toHaveBeenCalledTimes(1); | ||
|
|
||
| const [path] = deps.unlinkFileFn.mock.calls[0]; | ||
| expect(path).toBe(expectedLockFilePath); | ||
| }); | ||
|
|
||
| it("should do nothing if the file does not exist", () => { | ||
| const deps = { | ||
| fileExistsFn: jest.fn().mockReturnValue(false), | ||
| unlinkFileFn: jest.fn(), | ||
| }; | ||
|
|
||
| releaseLock(network, chainId, deps); | ||
|
|
||
| expect(deps.fileExistsFn).toHaveBeenCalledTimes(1); | ||
| expect(deps.unlinkFileFn).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); | ||
mani99brar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import fs from "fs"; | ||
|
|
||
| /** | ||
| * Returns the lock file path for a given network and chain id | ||
| * | ||
| * @param network - The network name | ||
| * @param chainId - The numerical identifier of the chain | ||
| * @returns The lock file path | ||
| * | ||
| * @example | ||
| * getLockFilePath('goerli', 1); // './state/goerli_1.pid' | ||
| */ | ||
| export function getLockFilePath(network: string, chainId: number) { | ||
| return `./state/${network.toLowerCase()}_${chainId}.pid`; | ||
| } | ||
|
|
||
| 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"; | ||
| } | ||
| } | ||
|
|
||
| type ClaimLockDependencies = { | ||
| fileExistsFn?: typeof fs.existsSync; | ||
| writeFileFn?: typeof fs.writeFileSync; | ||
| }; | ||
|
|
||
| /** | ||
| * Ensures there is only one process running at the same time for a given lock file. | ||
| * | ||
| * If the lock file exists, thrown an error. If it does not exists, creates it with the current process id. | ||
| * | ||
| * @param network - The network name | ||
| * @param chain - The chain id | ||
| * @param dependencies - FS methods to be used | ||
| * | ||
| * @example | ||
| * claimLock('/opt/app/lock.pid'); | ||
| */ | ||
| export function claimLock( | ||
| network: string, | ||
| chain: number, | ||
| dependencies: ClaimLockDependencies = { | ||
| fileExistsFn: fs.existsSync, | ||
| writeFileFn: fs.writeFileSync, | ||
| } | ||
| ) { | ||
| const path = getLockFilePath(network, chain); | ||
| const { fileExistsFn, writeFileFn } = dependencies; | ||
|
|
||
| if (fileExistsFn(path)) throw new LockfileExistsError(path); | ||
| writeFileFn(path, process.pid.toString(), { encoding: "utf8" }); | ||
mani99brar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| type ReleaseLockDependencies = { | ||
| fileExistsFn?: typeof fs.existsSync; | ||
| unlinkFileFn?: typeof fs.unlinkSync; | ||
| }; | ||
|
|
||
| /** | ||
| * Ensures the lock file is removed | ||
| * | ||
| * @param network - The network name | ||
| * @param chainId - The numerical identifier of the chain | ||
| * @param dependencies - FS methods to be used | ||
| * | ||
| * @example | ||
| * releaseLock('/opt/app/lock.pid'); | ||
| */ | ||
| export function releaseLock( | ||
| network: string, | ||
| chain: number, | ||
| dependencies: ReleaseLockDependencies = { | ||
| fileExistsFn: fs.existsSync, | ||
| unlinkFileFn: fs.unlinkSync, | ||
| } | ||
| ) { | ||
| const { fileExistsFn, unlinkFileFn } = dependencies; | ||
| const path = getLockFilePath(network, chain); | ||
|
|
||
| if (!fileExistsFn(path)) return; | ||
| unlinkFileFn(path); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.