added mutex to prevent multiple instances (Windows)

This commit is contained in:
Jeroen Verburgh 2024-11-14 02:25:16 +01:00
parent 0d1636abfc
commit f5b5f03635
3 changed files with 17 additions and 0 deletions

1
go.mod
View File

@ -27,6 +27,7 @@ require (
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/rodolfoag/gow32 v0.0.0-20230512144032-1e896a3c51aa // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a // indirect
golang.org/x/sys v0.15.0 // indirect

2
go.sum
View File

@ -42,6 +42,8 @@ github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgF
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rodolfoag/gow32 v0.0.0-20230512144032-1e896a3c51aa h1:cd9mmDEXO4YxGYTbrhbfEt7btgUlcXFedTMoZ9fA4Ns=
github.com/rodolfoag/gow32 v0.0.0-20230512144032-1e896a3c51aa/go.mod h1:w/ebPUfAcyZMYjstwPIWTEGSahChHx5R3Y+xElrvxDc=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

14
main.go
View File

@ -11,14 +11,18 @@ import (
"os"
"runtime"
"sync/atomic"
"syscall"
"time"
"github.com/cornelk/hashmap"
"github.com/gen2brain/beeep"
"github.com/getlantern/systray"
"github.com/tawesoft/golib/v2/dialog"
"github.com/rodolfoag/gow32"
)
const MutexName = "proxmoxbackupclient_go"
var defaultMailSubjectTemplate = "Backup {{.Status}}"
var defaultMailBodyTemplate = `{{if .Success}}Backup complete ({{.FromattedDuration}})
Chunks New {{.NewChunks}}, Reused {{.ReusedChunks}}.{{else}}Error occurred while working, backup may be not completed.
@ -75,6 +79,16 @@ func main() {
}
if runtime.GOOS == "windows" {
mutexid, err := gow32.CreateMutex(MutexName)
if (err != nil) {
if exitcode := int(err.(syscall.Errno)); exitcode == gow32.ERROR_ALREADY_EXISTS {
dialog.Error("Backup jobs need to run exclusively, please wait until the previous job has finished")
os.Exit(2)
}
// this should never happen
panic(err)
}
defer gow32.ReleaseMutex(mutexid)
go systray.Run(func() {
systray.SetIcon(ICON)