singleinstance

package
v0.0.0-...-8ec3fd9 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package singleinstance provides a mechanism to ensure, that only one instance of a program is running

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrAlreadyRunning another instance of is already running
	ErrAlreadyRunning = errors.New("another instance is already running")
)

Functions

This section is empty.

Types

type Option

type Option func(*SingleInstance)

Option configures Single

func WithLockPath

func WithLockPath(lockpath string) Option

WithLockPath configures the path for the lockfile

type SingleInstance

type SingleInstance struct {
	// contains filtered or unexported fields
}

SingleInstance represents the name and the open file descriptor

func New

func New(name string, opts ...Option) *SingleInstance

New creates a SingleInstance instance where name is the basename of the lock file (<name>.lock) if no path is given (WithLockPath option) the lock will be created in an operating specific path as <name>.lock panics if namr is empty

Example

ExampleNew is an example how to use singleinstance

// create a new lockfile in /var/lock/filename
one := New("filename", WithLockPath("/tmp"))

// lock and defer unlocking
if err := one.Lock(); err != nil {
	log.Fatal(err)
}

// run

if err := one.Unlock(); err != nil {
	log.Println(err)
}
Output:

func (*SingleInstance) Lock

func (s *SingleInstance) Lock() error

Lock tries to obtain an exclude lock on a lockfile and exits the program if an error occurs

func (*SingleInstance) Lockfile

func (s *SingleInstance) Lockfile() string

Lockfile returns the full path of the lock file

func (*SingleInstance) Unlock

func (s *SingleInstance) Unlock() error

Unlock releases the lock, closes and removes the lockfile

Jump to

Keyboard shortcuts

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