- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 874
 
cli: add dev lock file to prevent 2 dev processes running at the same time in the same dir #1854
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
                    Changes from all commits
      Commits
    
    
  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
    
  
  
    
              
  
    
      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,93 @@ | ||
| import path from "node:path"; | ||
| import { readFile } from "../utilities/fileSystem.js"; | ||
| import { tryCatch } from "@trigger.dev/core/utils"; | ||
| import { logger } from "../utilities/logger.js"; | ||
| import { mkdir, writeFile } from "node:fs/promises"; | ||
| import { existsSync, unlinkSync } from "node:fs"; | ||
| import { onExit } from "signal-exit"; | ||
| 
     | 
||
| const LOCK_FILE_NAME = "dev.lock"; | ||
| 
     | 
||
| export async function createLockFile(cwd: string) { | ||
| const currentPid = process.pid; | ||
| const lockFilePath = path.join(cwd, ".trigger", LOCK_FILE_NAME); | ||
| 
     | 
||
                
      
                  coderabbitai[bot] marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| logger.debug("Checking for lockfile", { lockFilePath, currentPid }); | ||
| 
     | 
||
| const removeLockFile = () => { | ||
| try { | ||
| logger.debug("Removing lockfile", { lockFilePath }); | ||
| return unlinkSync(lockFilePath); | ||
| } catch (e) { | ||
| // This sometimes fails on Windows with EBUSY | ||
| } | ||
| }; | ||
| const removeExitListener = onExit(removeLockFile); | ||
| 
     | 
||
| const [, existingLockfileContents] = await tryCatch(readFile(lockFilePath)); | ||
| 
     | 
||
| if (existingLockfileContents) { | ||
| // Read the pid number from the lockfile | ||
| const existingPid = Number(existingLockfileContents); | ||
| 
     | 
||
| logger.debug("Lockfile exists", { lockFilePath, existingPid, currentPid }); | ||
| 
     | 
||
| if (existingPid === currentPid) { | ||
| logger.debug("Lockfile exists and is owned by current process", { | ||
| lockFilePath, | ||
| existingPid, | ||
| currentPid, | ||
| }); | ||
| 
     | 
||
| return () => { | ||
| removeExitListener(); | ||
| removeLockFile(); | ||
| }; | ||
| } | ||
| 
     | 
||
| // If the pid is different, try and kill the existing pid | ||
| logger.debug("Lockfile exists and is owned by another process, killing it", { | ||
| lockFilePath, | ||
| existingPid, | ||
| currentPid, | ||
| }); | ||
| 
     | 
||
| try { | ||
| process.kill(existingPid); | ||
| // If it did kill the process, it will have exited, deleting the lockfile, so let's wait for that to happen | ||
| // But let's not wait forever | ||
| await new Promise((resolve, reject) => { | ||
| const timeout = setTimeout(() => { | ||
| clearInterval(interval); | ||
| reject(new Error("Timed out waiting for lockfile to be deleted")); | ||
| }, 5000); | ||
| 
     | 
||
| const interval = setInterval(() => { | ||
| if (!existsSync(lockFilePath)) { | ||
| clearInterval(interval); | ||
| clearTimeout(timeout); | ||
| resolve(true); | ||
| } | ||
| }, 100); | ||
| }); | ||
| } catch (error) { | ||
| logger.debug("Failed to kill existing process, lets assume it's not running", { error }); | ||
| } | ||
| } | ||
| 
     | 
||
| // Now write the current pid to the lockfile | ||
| await writeFileAndEnsureDirExists(lockFilePath, currentPid.toString()); | ||
| 
     | 
||
| logger.debug("Lockfile created", { lockFilePath, currentPid }); | ||
| 
     | 
||
| return () => { | ||
| removeExitListener(); | ||
| removeLockFile(); | ||
| }; | ||
| } | ||
| 
     | 
||
| async function writeFileAndEnsureDirExists(filePath: string, data: string) { | ||
| const dir = path.dirname(filePath); | ||
| await mkdir(dir, { recursive: true }); | ||
| await writeFile(filePath, data); | ||
| } | ||
  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.