lock

package
v13.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 3 Imported by: 1

Documentation

Overview

Package lock provides methods for working with lock files

Index

Examples

Constants

This section is empty.

Variables

View Source
var Dir = "/var/tmp"

Dir is a path to directory with lock files

Functions

func Create

func Create(name string) error

Create creates new lock file

Example
err := Create("my_app")

if err != nil {
	panic(err.Error())
}

// do some stuff

err = Remove("my_app")

if err != nil {
	panic(err.Error())
}
Output:

func Has

func Has(name string) bool

Has returns true if lock file exists

Example
if Has("my_app") {
	panic("Can't start application due to lock file")
}
Output:

func IsExpired

func IsExpired(name string, ttl time.Duration) bool

IsExpired returns true if lock file reached TTL

Example
if Has("my_app") {
	if IsExpired("my_app", time.Hour) {
		// looks like lock file was created long time ago, so delete it
		Remove("my_app")
	} else {
		panic("Can't start application due to lock file")
	}
}
Output:

func Remove

func Remove(name string) error

Remove deletes lock file

Example
err := Create("my_app")

if err != nil {
	panic(err.Error())
}

// do your stuff

err = Remove("my_app")

if err != nil {
	panic(err.Error())
}
Output:

func Wait

func Wait(name string, deadline time.Time) bool

Wait waits until lock file being deleted

Example
ok := Wait("my_app", time.Now().Add(time.Minute))

if !ok {
	panic("Can't start application due to lock file")
}

// do your stuff
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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