Skip to content

Commit 7cdb0b5

Browse files
mfainberg-cftempusfrangit
authored andcommitted
Improve PID locking wait
* Do not block on pidfile when using the version command * Emit warnings when blocking on pid file flock. Closes: #124
1 parent a233a23 commit 7cdb0b5

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

cmd/cmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55

66
"github.com/replicate/pget/cmd/multifile"
77
"github.com/replicate/pget/cmd/root"
8+
"github.com/replicate/pget/cmd/version"
89
)
910

1011
func GetRootCommand() *cobra.Command {
1112
rootCMD := root.GetCommand()
1213
rootCMD.AddCommand(multifile.GetCommand())
13-
rootCMD.AddCommand(VersionCMD)
14+
rootCMD.AddCommand(version.VersionCMD)
1415
return rootCMD
1516
}

cmd/root/root.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package root
33
import (
44
"context"
55
"fmt"
6+
"github.com/replicate/pget/cmd/version"
67
"os"
78
"runtime"
89
"time"
@@ -51,20 +52,25 @@ func GetCommand() *cobra.Command {
5152
if err := config.PersistentStartupProcessFlags(); err != nil {
5253
return err
5354
}
54-
pidFilePath := viper.GetString(config.OptPIDFile)
55-
pid, err := cli.NewPIDFile(pidFilePath)
56-
if err != nil {
57-
return err
58-
}
59-
err = pid.Acquire()
60-
if err != nil {
61-
return err
55+
if cmd.CalledAs() != version.VersionCMDName {
56+
pidFilePath := viper.GetString(config.OptPIDFile)
57+
pid, err := cli.NewPIDFile(pidFilePath)
58+
if err != nil {
59+
return err
60+
}
61+
err = pid.Acquire()
62+
if err != nil {
63+
return err
64+
}
65+
pidFile = pid
6266
}
63-
pidFile = pid
6467
return nil
6568
},
6669
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
67-
return pidFile.Release()
70+
if pidFile != nil {
71+
return pidFile.Release()
72+
}
73+
return nil
6874
},
6975
PreRun: rootCmdPreRun,
7076
RunE: runRootCMD,

cmd/version.go renamed to cmd/version/version.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package version
22

33
import (
44
"fmt"
@@ -8,8 +8,10 @@ import (
88
"github.com/replicate/pget/pkg/version"
99
)
1010

11+
const VersionCMDName = "version"
12+
1113
var VersionCMD = &cobra.Command{
12-
Use: "version",
14+
Use: VersionCMDName,
1315
Short: "print version and build information",
1416
Long: "Print the version information",
1517
Run: func(cmd *cobra.Command, args []string) {

pkg/cli/pid.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"os"
88
"syscall"
9+
10+
"github.com/replicate/pget/pkg/logging"
911
)
1012

1113
type PIDFile struct {
@@ -22,8 +24,21 @@ func NewPIDFile(path string) (*PIDFile, error) {
2224
}
2325

2426
func (p *PIDFile) Acquire() error {
27+
logger := logging.GetLogger()
2528
funcs := []func() error{
26-
func() error { return syscall.Flock(p.fd, syscall.LOCK_EX) },
29+
func() error {
30+
logger.Debug().Str("blocking_lock_acquire", "false").Msg("Waiting on Lock")
31+
err := syscall.Flock(p.fd, syscall.LOCK_EX|syscall.LOCK_NB)
32+
if err != nil {
33+
logger.Warn().
34+
Err(err).
35+
Str("message", "Another pget process may be running, use 'pget multifile' to download multiple files in parallel").
36+
Msg("Waiting on Lock")
37+
logger.Debug().Str("blocking_lock_acquire", "true").Msg("Waiting on Lock")
38+
err = syscall.Flock(p.fd, syscall.LOCK_EX)
39+
}
40+
return err
41+
},
2742
p.writePID,
2843
p.file.Sync,
2944
}

0 commit comments

Comments
 (0)