File tree Expand file tree Collapse file tree 4 files changed +38
-14
lines changed
Expand file tree Collapse file tree 4 files changed +38
-14
lines changed Original file line number Diff line number Diff 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
1011func 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}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package root
33import (
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 ,
Original file line number Diff line number Diff line change 1- package cmd
1+ package version
22
33import (
44 "fmt"
@@ -8,8 +8,10 @@ import (
88 "github.com/replicate/pget/pkg/version"
99)
1010
11+ const VersionCMDName = "version"
12+
1113var 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 ) {
Original file line number Diff line number Diff line change 66 "fmt"
77 "os"
88 "syscall"
9+
10+ "github.com/replicate/pget/pkg/logging"
911)
1012
1113type PIDFile struct {
@@ -22,8 +24,21 @@ func NewPIDFile(path string) (*PIDFile, error) {
2224}
2325
2426func (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 }
You can’t perform that action at this time.
0 commit comments