Skip to content

Commit

Permalink
refactor: Move directory creation to the Store instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Castell committed Jan 20, 2020
1 parent 35c3920 commit b3525bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 0 additions & 11 deletions dkron/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,17 +453,6 @@ func (a *Agent) StartServer() {
}

if a.Store == nil {
dirExists, err := exists(a.config.DataDir)
if err != nil {
log.WithError(err).WithField("dir", a.config.DataDir).Fatal("Invalid Dir")
}
if !dirExists {
// Try to create the directory
err := os.Mkdir(a.config.DataDir, 0700)
if err != nil {
log.WithError(err).WithField("dir", a.config.DataDir).Fatal("Error Creating Dir")
}
}
s, err := NewStore(filepath.Join(a.config.DataDir, a.config.NodeName))
if err != nil {
log.WithError(err).Fatal("dkron: Error initializing store")
Expand Down
13 changes: 13 additions & 0 deletions dkron/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"os"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -44,6 +45,18 @@ type JobOptions struct {

// NewStore creates a new Storage instance.
func NewStore(dir string) (*Store, error) {
dirExists, err := exists(dir)
if err != nil {
return nil, fmt.Errorf("Ivalid directory %s: %w", dir, err)
}
if !dirExists {
// Try to create the directory
err := os.Mkdir(dir, 0700)
if err != nil {
return nil, fmt.Errorf("Error creating directory %s: %w", dir, err)
}
}

opts := badger.DefaultOptions(dir).
WithLogger(log)

Expand Down

0 comments on commit b3525bf

Please sign in to comment.