Merge pull request #20 from andrewslotin/18-timeout_if_db_is_locked

Timeout if DB is locked
This commit is contained in:
Brian Buller 2017-10-13 12:03:53 -05:00 committed by GitHub
commit f289915a70
2 changed files with 22 additions and 3 deletions

View File

@ -36,3 +36,9 @@ Just provide a BoltDB filename to be opened as the first argument on the command
```sh
boltbrowser <filename>
```
To see all options that are available, run:
```
boltbrowser --help
```

View File

@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"time"
"github.com/boltdb/bolt"
"github.com/nsf/termbox-go"
@ -20,9 +21,17 @@ var memBolt *BoltDB
var currentFilename string
const DefaultDBOpenTimeout = time.Second
var args struct {
DBOpenTimeout time.Duration
}
func init() {
flag.DurationVar(&args.DBOpenTimeout, "timeout", DefaultDBOpenTimeout, "DB file open timeout")
flag.Usage = func() {
fmt.Fprintf(os.Stdout, "Usage: %s <filename(s)>\n", ProgramName)
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] <filename(s)>\nOptions:\n", ProgramName)
flag.PrintDefaults()
}
}
@ -47,8 +56,12 @@ func main() {
databaseFiles := flag.Args()
for _, databaseFile := range databaseFiles {
currentFilename = databaseFile
db, err = bolt.Open(databaseFile, 0600, nil)
if err != nil {
db, err = bolt.Open(databaseFile, 0600, &bolt.Options{Timeout: args.DBOpenTimeout})
if err == bolt.ErrTimeout {
termbox.Close()
fmt.Printf("File %s is locked. Make sure it's not used by another app and try again\n", databaseFile)
os.Exit(1)
} else if err != nil {
if len(databaseFiles) > 1 {
mainLoop(nil, style)
continue