lockfile

package
v0.0.0-...-2af5034 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2016 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrBusy        = errors.New("Locked by other process") // If you get this, retry after a short sleep might help
	ErrNeedAbsPath = errors.New("Lockfiles must be given as absolute path names")
	ErrInvalidPid  = errors.New("Lockfile contains invalid pid for system")
	ErrDeadOwner   = errors.New("Lockfile contains pid of process not existent on this system anymore")
)

Functions

This section is empty.

Types

type Lockfile

type Lockfile struct {
	Path string
}
Example
package main

import (
	"fmt"
	"github.com/dropbox/changes-client/common/lockfile"
)

func main() {
	lock, err := lockfile.New("/tmp/lock.me.now.lck")
	if err != nil {
		fmt.Printf("Cannot init lock. reason: %s\n", err)
		panic(err)
	}
	err = lock.TryLock()

	// Error handling is essential, as we only try to get the lock.
	if err != nil {
		fmt.Printf("Cannot lock \"%v\", reason: %s\n", lock, err)
		panic(err)
	}

	defer lock.Unlock()

	fmt.Println("Do stuff under lock")
}
Output:

Do stuff under lock

func New

func New(path string) (*Lockfile, error)

Describe a new filename located at path. It is expected to be an absolute path

func (*Lockfile) GetOwner

func (l *Lockfile) GetOwner() (*os.Process, error)

Who owns the lockfile?

func (*Lockfile) TryLock

func (l *Lockfile) TryLock() error

Try to get Lockfile lock. Returns nil, if successful and and error describing the reason, it didn't work out. Please note, that existing lockfiles containing pids of dead processes and lockfiles containing no pid at all are deleted.

func (*Lockfile) Unlock

func (l *Lockfile) Unlock() error

Release a lock again. Returns any error that happend during release of lock.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL