Skip to content

Commit

Permalink
Add PollBlackhole option and update config version updating
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDallas committed Jun 30, 2022
1 parent d9bd141 commit 8dc6538
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 48 deletions.
63 changes: 39 additions & 24 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,51 @@ func loadConfigFromDisk(altConfigLocation string) (Config, error) {
var config Config

log.Trace("Trying to load config from disk")

configLocation := path.Join(altConfigLocation, "config.yaml")

log.Tracef("Reading config from %s", configLocation)

file, err := ioutil.ReadFile(configLocation)

if err != nil {
log.Trace("Failed to find config file")
return config, ErrFailedToFindConfigFile
}

log.Trace("Attempting to unmarshal config")
log.Trace("Loading to interface")
var configInterface map[interface{}]interface{}
err = yaml.Unmarshal(file, &configInterface)
if err != nil {
log.Errorf("Failed to unmarshal config file: %+v", err)
return config, ErrInvalidConfigFile
}

log.Trace("Unmarshalling to struct")
err = yaml.Unmarshal(file, &config)
if err != nil {
log.Trace("Failed to unmarshal config")
log.Errorf("Failed to unmarshal config file: %+v", err)
return config, ErrInvalidConfigFile
}

log.Trace("Config loaded, running version update")
config, updated := versionUpdateConfig(config)
log.Trace("Checking for missing config fields")
updated := false

if configInterface["PollBlackholeDirectory"] == nil {
log.Info("PollBlackholeDirectory not set, setting to false")
config.PollBlackholeDirectory = false
updated = true
}

if configInterface["SimultaneousDownloads"] == nil {
log.Info("SimultaneousDownloads not set, setting to 5")
config.SimultaneousDownloads = 5
updated = true
}

if configInterface["PollBlackholeIntervalMinutes"] == nil {
log.Info("PollBlackholeIntervalMinutes not set, setting to 10")
config.PollBlackholeIntervalMinutes = 10
updated = true
}

config.altConfigLocation = altConfigLocation

Expand All @@ -123,31 +147,22 @@ func loadConfigFromDisk(altConfigLocation string) (Config, error) {
return config, nil
}

func versionUpdateConfig(config Config) (Config, bool) {
updated := false
// 1.1.3
if config.SimultaneousDownloads == 0 {
config.SimultaneousDownloads = 5
updated = true
}

return config, updated
}

func defaultConfig() Config {
return Config{
PremiumizemeAPIKey: "xxxxxxxxx",
Arrs: []ArrConfig{
{Name: "Sonarr", URL: "http://localhost:8989", APIKey: "xxxxxxxxx", Type: Sonarr},
{Name: "Radarr", URL: "http://localhost:7878", APIKey: "xxxxxxxxx", Type: Radarr},
},
BlackholeDirectory: "",
DownloadsDirectory: "",
UnzipDirectory: "",
BindIP: "0.0.0.0",
BindPort: "8182",
WebRoot: "",
SimultaneousDownloads: 5,
BlackholeDirectory: "",
PollBlackholeDirectory: false,
PollBlackholeIntervalMinutes: 10,
DownloadsDirectory: "",
UnzipDirectory: "",
BindIP: "0.0.0.0",
BindPort: "8182",
WebRoot: "",
SimultaneousDownloads: 5,
}
}

Expand Down
5 changes: 4 additions & 1 deletion internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ type Config struct {

Arrs []ArrConfig `yaml:"Arrs"`

BlackholeDirectory string `yaml:"BlackholeDirectory"`
BlackholeDirectory string `yaml:"BlackholeDirectory"`
PollBlackholeDirectory bool `yaml:"PollBlackholeDirectory"`
PollBlackholeIntervalMinutes int `yaml:"PollBlackholeIntervalMinutes"`

DownloadsDirectory string `yaml:"DownloadsDirectory"`

UnzipDirectory string `yaml:"UnzipDirectory"`
Expand Down
14 changes: 7 additions & 7 deletions internal/directory_watcher/directory_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func NewDirectoryWatcher(path string, recursive bool, matchFunction func(string)

func (w *WatchDirectory) Watch() error {
var err error
w.watcher, err = fsnotify.NewWatcher()
w.Watcher, err = fsnotify.NewWatcher()
if err != nil {
return err
}

go func() {
for {
select {
case event, ok := <-w.watcher.Events:
case event, ok := <-w.Watcher.Events:
if !ok {
return
}
Expand All @@ -37,7 +37,7 @@ func (w *WatchDirectory) Watch() error {
w.CallbackFunction(event.Name)
}
}
case _, ok := <-w.watcher.Errors:
case _, ok := <-w.Watcher.Errors:
if !ok {
return
}
Expand All @@ -51,7 +51,7 @@ func (w *WatchDirectory) Watch() error {
return err
}

err = w.watcher.Add(cleanPath)
err = w.Watcher.Add(cleanPath)
if err != nil {
return err
}
Expand All @@ -60,11 +60,11 @@ func (w *WatchDirectory) Watch() error {
}

func (w *WatchDirectory) UpdatePath(path string) error {
w.watcher.Remove(w.Path)
w.Watcher.Remove(w.Path)
w.Path = path
return w.watcher.Add(w.Path)
return w.Watcher.Add(w.Path)
}

func (w *WatchDirectory) Stop() error {
return w.watcher.Close()
return w.Watcher.Close()
}
2 changes: 1 addition & 1 deletion internal/directory_watcher/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ type WatchDirectory struct {
// Callback is the function to call when a file is created that matches with MatchFunction.
CallbackFunction func(string)
// watcher is the fsnotify watcher.
watcher *fsnotify.Watcher
Watcher *fsnotify.Watcher
}
58 changes: 43 additions & 15 deletions internal/service/directory_watcher_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ func (dw *DirectoryWatcherService) ConfigUpdatedCallback(currentConfig config.Co
if currentConfig.BlackholeDirectory != newConfig.BlackholeDirectory {
log.Info("Blackhole directory changed, restarting directory watcher...")
log.Info("Running initial directory scan...")
go dw.initialDirectoryScan(dw.config.BlackholeDirectory)
go dw.directoryScan(dw.config.BlackholeDirectory)
dw.watchDirectory.UpdatePath(newConfig.BlackholeDirectory)
}

if currentConfig.PollBlackholeDirectory != newConfig.PollBlackholeDirectory {
log.Info("Poll blackhole directory changed, restarting directory watcher...")
dw.Start()
}
}

func (dw *DirectoryWatcherService) GetStatus() string {
return dw.status
}

//TODO (Radarr): accept paths as a parameter, support multiple paths
//Watch: This is the entrypoint for the directory watcher
//Start: This is the entrypoint for the directory watcher
func (dw *DirectoryWatcherService) Start() {
log.Info("Starting directory watcher...")

Expand All @@ -71,23 +75,47 @@ func (dw *DirectoryWatcherService) Start() {
go dw.processUploads()

log.Info("Running initial directory scan...")
go dw.initialDirectoryScan(dw.config.BlackholeDirectory)
go dw.directoryScan(dw.config.BlackholeDirectory)

// Build and start a DirectoryWatcher
dw.watchDirectory = directory_watcher.NewDirectoryWatcher(dw.config.BlackholeDirectory,
false,
dw.checkFile,
dw.addFileToQueue,
)
if dw.watchDirectory != nil {
log.Info("Stopping directory watcher...")
err := dw.watchDirectory.Stop()
if err != nil {
log.Errorf("Error stopping directory watcher: %s", err)
}
}

dw.watchDirectory.Watch()
if dw.config.PollBlackholeDirectory {
log.Info("Starting directory poller...")
go func() {
for {
if !dw.config.PollBlackholeDirectory {
log.Info("Directory poller stopped")
break
}
time.Sleep(time.Duration(dw.config.PollBlackholeIntervalMinutes) * time.Minute)
log.Infof("Running directory scan of %s", dw.config.BlackholeDirectory)
dw.directoryScan(dw.config.BlackholeDirectory)
log.Infof("Scan complete, next scan in %d minutes", dw.config.PollBlackholeIntervalMinutes)
}
}()
} else {
log.Info("Starting directory watcher...")
dw.watchDirectory = directory_watcher.NewDirectoryWatcher(dw.config.BlackholeDirectory,
false,
dw.checkFile,
dw.addFileToQueue,
)
dw.watchDirectory.Watch()
}
}

func (dw *DirectoryWatcherService) initialDirectoryScan(p string) {
log.Trace("Initial directory scan")
func (dw *DirectoryWatcherService) directoryScan(p string) {
log.Trace("Running directory scan")
files, err := ioutil.ReadDir(p)
if err != nil {
log.Errorf("Error with initial directory scan %+v", err)
log.Errorf("Error with directory scan %+v", err)
return
}

for _, file := range files {
Expand All @@ -110,7 +138,7 @@ func (dw *DirectoryWatcherService) checkFile(path string) bool {
}

if fi.IsDir() {
log.Errorf("Directory created in blackhole %s ignoring (Warning premiumizearrzed does not look in subfolders!)", path)
log.Errorf("Directory created in blackhole %s ignoring (Warning premiumizearrd does not look in subfolders!)", path)
return false
}

Expand Down

0 comments on commit 8dc6538

Please sign in to comment.