Ensure only a single instance of after-lock runs at a time
This commit is contained in:
@ -2,19 +2,50 @@ package main
|
||||
|
||||
import (
|
||||
"after-lock/afterlock"
|
||||
"after-lock/lockfile"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
initialDelay = 3
|
||||
loopDelay = 6
|
||||
keyboardDeviceNode = "/dev/input/by-id/usb-Dell_Dell_USB_Entry_Keyboard-event-kbd"
|
||||
lockfilePath = "/tmp/after-lock.lock"
|
||||
printStackTraces = false
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
defer handleErrors()
|
||||
|
||||
lock := grabExclusiveProcessLock()
|
||||
defer lock.Unlock()
|
||||
|
||||
al := afterlock.New(keyboardDeviceNode)
|
||||
al.InitialDelay = initialDelay
|
||||
al.LoopDelay = loopDelay
|
||||
|
||||
al.Start()
|
||||
}
|
||||
|
||||
func handleErrors() {
|
||||
if err := recover(); err != nil {
|
||||
fmt.Printf("Crashed with unexpected error: %v!\n", err)
|
||||
if printStackTraces {
|
||||
fmt.Printf("Stack trace:\n\n")
|
||||
debug.PrintStack()
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func grabExclusiveProcessLock() *lockfile.Lockfile {
|
||||
lf := lockfile.New(lockfilePath)
|
||||
err := lf.Lock()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return lf
|
||||
}
|
Reference in New Issue
Block a user