forked from marcsauter/single
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle.go
More file actions
34 lines (28 loc) · 726 Bytes
/
single.go
File metadata and controls
34 lines (28 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// package single provides a mechanism to ensure, that only one instance of a program is running
package single
import (
"errors"
"log"
"os"
)
var (
// ErrAlreadyRunning -- the instance is already running
ErrAlreadyRunning = errors.New("the program is already running")
// Lockfile -- the lock file to check
Lockfile string
)
// Single represents the name and the open file descriptor
type Single struct {
name string
file *os.File
}
// New creates a Single instance
func New(name string) *Single {
return &Single{name: name}
}
// Lock tries to obtain an exclude lock on a lockfile and exits the program if an error occurs
func (s *Single) Lock() {
if err := s.CheckLock(); err != nil {
log.Fatal(err)
}
}