Compare commits

...

2 Commits

Author SHA1 Message Date
Jack Dallas
0fb1d2580c Add polling settings to ui 2022-06-30 21:09:05 +01:00
Jack Dallas
8dc653814b Add PollBlackhole option and update config version updating 2022-06-30 21:09:05 +01:00
6 changed files with 112 additions and 49 deletions

View File

@@ -81,11 +81,9 @@ 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 {
@@ -93,15 +91,41 @@ func loadConfigFromDisk(altConfigLocation string) (Config, error) {
return config, ErrFailedToFindConfigFile
}
log.Trace("Attempting to unmarshal config")
err = yaml.Unmarshal(file, &config)
log.Trace("Loading to interface")
var configInterface map[interface{}]interface{}
err = yaml.Unmarshal(file, &configInterface)
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("Unmarshalling to struct")
err = yaml.Unmarshal(file, &config)
if err != nil {
log.Errorf("Failed to unmarshal config file: %+v", err)
return config, ErrInvalidConfigFile
}
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
@@ -123,17 +147,6 @@ 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",
@@ -141,13 +154,15 @@ func defaultConfig() Config {
{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,
}
}

View File

@@ -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"`

View File

@@ -20,7 +20,7 @@ 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
}
@@ -28,7 +28,7 @@ func (w *WatchDirectory) Watch() error {
go func() {
for {
select {
case event, ok := <-w.watcher.Events:
case event, ok := <-w.Watcher.Events:
if !ok {
return
}
@@ -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
}
@@ -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
}
@@ -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()
}

View File

@@ -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
}

View File

@@ -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...")
@@ -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 {
@@ -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
}

View File

@@ -7,6 +7,8 @@
Modal,
FormGroup,
Dropdown,
Form,
Checkbox,
} from "carbon-components-svelte";
import {
Save,
@@ -21,6 +23,8 @@
let config = {
BlackholeDirectory: "",
PollBlackholeDirectory: false,
PollBlackholeIntervalMinutes: 10,
DownloadsDirectory: "",
UnzipDirectory: "",
BindIP: "",
@@ -261,6 +265,19 @@
labelText="Blackhole Directory"
bind:value={config.BlackholeDirectory}
/>
<Checkbox
disabled={inputDisabled}
bind:checked={config.PollBlackholeDirectory}
labelText="Poll Blackhole Directory"
/>
<TextInput
type="number"
disabled={inputDisabled}
labelText="Poll Blackhole Interval Minutes"
bind:value={config.PollBlackholeIntervalMinutes}
/>
</FormGroup>
<FormGroup>
<TextInput
disabled={inputDisabled}
labelText="Download Directory"