Skip to content

Commit aa98d87

Browse files
authored
Merge pull request #26 from rande/fix_missing_error_management_on_init
fix(init): handle error with boltdb initialization
2 parents c4d5e87 + 769329e commit aa98d87

File tree

10 files changed

+86
-65
lines changed

10 files changed

+86
-65
lines changed

mirror/bower/bower.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,19 @@ type BowerService struct {
4343
StateChan chan pkgmirror.State
4444
}
4545

46-
func (bs *BowerService) Init(app *goapp.App) error {
47-
var err error
48-
46+
func (bs *BowerService) Init(app *goapp.App) (err error) {
4947
bs.Logger.Info("Init")
5048

51-
bs.DB, err = bolt.Open(fmt.Sprintf("%s/%s.db", bs.Config.Path, bs.Config.Code), 0600, &bolt.Options{
52-
Timeout: 1 * time.Second,
53-
ReadOnly: false,
54-
})
55-
56-
if err != nil {
57-
return err
49+
if bs.DB, err = pkgmirror.OpenDatabaseWithBucket(bs.Config.Path, bs.Config.Code); err != nil {
50+
bs.Logger.WithFields(log.Fields{
51+
"error": err,
52+
"path": bs.Config.Path,
53+
"bucket": string(bs.Config.Code),
54+
"action": "Init",
55+
}).Error("Unable to open the internal database")
5856
}
5957

60-
return bs.DB.Update(func(tx *bolt.Tx) error {
61-
_, err := tx.CreateBucketIfNotExists(bs.Config.Code)
62-
63-
return err
64-
})
58+
return
6559
}
6660

6761
func (bs *BowerService) Serve(state *goapp.GoroutineState) error {

mirror/bower/bower_app.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ func ConfigureApp(config *pkgmirror.Config, l *goapp.Lifecycle) {
4040
"code": name,
4141
})
4242
s.StateChan = pkgmirror.GetStateChannel(fmt.Sprintf("pkgmirror.bower.%s", name), app.Get("pkgmirror.channel.state").(chan pkgmirror.State))
43-
s.Init(app)
43+
44+
if err := s.Init(app); err != nil {
45+
panic(err)
46+
}
4447

4548
return s
4649
}

mirror/composer/composer.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,19 @@ type ComposerService struct {
4545
StateChan chan pkgmirror.State
4646
}
4747

48-
func (ps *ComposerService) Init(app *goapp.App) error {
49-
var err error
50-
48+
func (ps *ComposerService) Init(app *goapp.App) (err error) {
5149
ps.Logger.Info("Init")
5250

53-
ps.DB, err = bolt.Open(fmt.Sprintf("%s/%s.db", ps.Config.Path, ps.Config.Code), 0600, &bolt.Options{
54-
Timeout: 1 * time.Second,
55-
ReadOnly: false,
56-
})
57-
58-
if err != nil {
59-
return err
51+
if ps.DB, err = pkgmirror.OpenDatabaseWithBucket(ps.Config.Path, ps.Config.Code); err != nil {
52+
ps.Logger.WithFields(log.Fields{
53+
"error": err,
54+
"path": ps.Config.Path,
55+
"bucket": string(ps.Config.Code),
56+
"action": "Init",
57+
}).Error("Unable to open the internal database")
6058
}
6159

62-
return ps.DB.Update(func(tx *bolt.Tx) error {
63-
_, err := tx.CreateBucketIfNotExists(ps.Config.Code)
64-
65-
return err
66-
})
60+
return
6761
}
6862

6963
func (ps *ComposerService) Serve(state *goapp.GoroutineState) error {

mirror/composer/composer_app.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ func ConfigureApp(config *pkgmirror.Config, l *goapp.Lifecycle) {
4242
"code": name,
4343
})
4444
s.StateChan = pkgmirror.GetStateChannel(fmt.Sprintf("pkgmirror.composer.%s", name), app.Get("pkgmirror.channel.state").(chan pkgmirror.State))
45-
s.Init(app)
45+
46+
if err := s.Init(app); err != nil {
47+
panic(err)
48+
}
4649

4750
return s
4851
}

mirror/git/git_app.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ func ConfigureApp(config *pkgmirror.Config, l *goapp.Lifecycle) {
5151
"code": name,
5252
})
5353
s.StateChan = pkgmirror.GetStateChannel(fmt.Sprintf("pkgmirror.git.%s", name), app.Get("pkgmirror.channel.state").(chan pkgmirror.State))
54-
s.Init(app)
54+
55+
if err := s.Init(app); err != nil {
56+
panic(err)
57+
}
5558

5659
return s
5760
}

mirror/npm/npm.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,19 @@ type NpmService struct {
5757
StateChan chan pkgmirror.State
5858
}
5959

60-
func (ns *NpmService) Init(app *goapp.App) error {
61-
var err error
62-
60+
func (ns *NpmService) Init(app *goapp.App) (err error) {
6361
ns.Logger.Info("Init")
6462

65-
ns.DB, err = bolt.Open(fmt.Sprintf("%s/%s.db", ns.Config.Path, ns.Config.Code), 0600, &bolt.Options{
66-
Timeout: 1 * time.Second,
67-
ReadOnly: false,
68-
})
69-
70-
if err != nil {
71-
return err
63+
if ns.DB, err = pkgmirror.OpenDatabaseWithBucket(ns.Config.Path, ns.Config.Code); err != nil {
64+
ns.Logger.WithFields(log.Fields{
65+
"error": err,
66+
"path": ns.Config.Path,
67+
"bucket": string(ns.Config.Code),
68+
"action": "Init",
69+
}).Error("Unable to open the internal database")
7270
}
7371

74-
return ns.DB.Update(func(tx *bolt.Tx) error {
75-
_, err := tx.CreateBucketIfNotExists(ns.Config.Code)
76-
77-
return err
78-
})
72+
return
7973
}
8074

8175
func (ns *NpmService) Serve(state *goapp.GoroutineState) error {

mirror/npm/npm_app.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ func ConfigureApp(config *pkgmirror.Config, l *goapp.Lifecycle) {
4848
},
4949
}
5050
s.StateChan = pkgmirror.GetStateChannel(fmt.Sprintf("pkgmirror.npm.%s", name), app.Get("pkgmirror.channel.state").(chan pkgmirror.State))
51-
s.Init(app)
51+
52+
if err := s.Init(app); err != nil {
53+
panic(err)
54+
}
5255

5356
return s
5457
}

mirror/static/static.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,19 @@ type StaticService struct {
5252
StateChan chan pkgmirror.State
5353
}
5454

55-
func (gs *StaticService) Init(app *goapp.App) error {
55+
func (gs *StaticService) Init(app *goapp.App) (err error) {
5656
os.MkdirAll(string(filepath.Separator)+gs.Config.Path, 0755)
5757

58-
var err error
59-
60-
gs.DB, err = bolt.Open(fmt.Sprintf("%s/%s.db", gs.Config.Path, gs.Config.Code), 0600, &bolt.Options{
61-
Timeout: 1 * time.Second,
62-
ReadOnly: false,
63-
})
64-
65-
if err != nil {
66-
return err
58+
if gs.DB, err = pkgmirror.OpenDatabaseWithBucket(gs.Config.Path, gs.Config.Code); err != nil {
59+
gs.Logger.WithFields(log.Fields{
60+
"error": err,
61+
"path": gs.Config.Path,
62+
"bucket": string(gs.Config.Code),
63+
"action": "Init",
64+
}).Error("Unable to open the internal database")
6765
}
6866

69-
return gs.DB.Update(func(tx *bolt.Tx) error {
70-
_, err := tx.CreateBucketIfNotExists(gs.Config.Code)
71-
72-
return err
73-
})
67+
return
7468
}
7569

7670
func (gs *StaticService) Serve(state *goapp.GoroutineState) error {

mirror/static/static_app.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ func ConfigureApp(config *pkgmirror.Config, l *goapp.Lifecycle) {
4949
"code": name,
5050
})
5151
s.StateChan = pkgmirror.GetStateChannel(fmt.Sprintf("pkgmirror.static.%s", name), app.Get("pkgmirror.channel.state").(chan pkgmirror.State))
52-
s.Init(app)
52+
53+
if err := s.Init(app); err != nil {
54+
panic(err)
55+
}
5356

5457
return s
5558
}

tools.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,42 @@ import (
99
"bytes"
1010
"compress/gzip"
1111
"encoding/json"
12+
"fmt"
1213
"io"
1314
"net/http"
1415
"os"
16+
"path/filepath"
1517
"sync"
18+
"time"
19+
20+
"github.com/boltdb/bolt"
1621
)
1722

23+
func OpenDatabaseWithBucket(basePath string, bucket []byte) (db *bolt.DB, err error) {
24+
if err = os.MkdirAll(string(filepath.Separator)+basePath, 0755); err != nil {
25+
return
26+
}
27+
28+
path := fmt.Sprintf("%s/%s.db", basePath, bucket)
29+
30+
db, err = bolt.Open(path, 0600, &bolt.Options{
31+
Timeout: 1 * time.Second,
32+
ReadOnly: false,
33+
})
34+
35+
if err != nil {
36+
return
37+
}
38+
39+
err = db.Update(func(tx *bolt.Tx) error {
40+
_, err := tx.CreateBucketIfNotExists(bucket)
41+
42+
return err
43+
})
44+
45+
return
46+
}
47+
1848
func LoadStruct(file string, v interface{}) error {
1949
r, err := os.Open(file)
2050

0 commit comments

Comments
 (0)