Ejemplo n.º 1
0
func main() {
	if len(os.Args) == 1 {
		log.Fatal("Argument required: <lock file>")
	}
	lockFilePath := os.Args[1]

	lockFile, err := os.OpenFile(lockFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
	if err != nil {
		log.Fatal(err)
	}
	defer lockFile.Close()

	err = lockapi.TryLockFile(lockFile)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Press ENTER to unlock...")
	fmt.Scanln()

	err = lockapi.UnlockFile(lockFile)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Press ENTER to end...")
	fmt.Scanln()
}
Ejemplo n.º 2
0
func (app *App) LockDirectory() (err error) {
	if app.lockFile != nil {
		return nil
	}

	lockFilePath := filepath.Join(app.Directory, lockFileName)

	log.Info("The lock file is: %v", lockFilePath)

	log.Info("Opening the lock file...")
	lockFile, err := os.OpenFile(lockFilePath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
	if err != nil {
		return err
	}

	log.Info("Obtaining the API lock...")
	err = lockapi.TryLockFile(lockFile)
	if err != nil {
		lockFile.Close()
		return err
	}
	log.Notice("Lock acquired")

	app.lockFile = lockFile

	return nil
}
Ejemplo n.º 3
0
func TestNonBlockingFileLockingCalls(t *testing.T) {
	lockFile, err := ioutil.TempFile(os.TempDir(), "LockAPI_TestFile")
	if err != nil {
		t.Fatal(err)
	}

	defer func() {
		lockFile.Close()
		os.Remove(lockFile.Name())
	}()

	err = lockapi.TryLockFile(lockFile)
	if err != nil {
		t.Fatal(err)
	}

	err = lockapi.UnlockFile(lockFile)
	if err != nil {
		t.Fatal(err)
	}
}