Skip to content

Commit

Permalink
Make Arr history update interval configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDallas committed Aug 13, 2022
1 parent 1f7431d commit 6c6d8d8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
5 changes: 3 additions & 2 deletions internal/arr/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (arr *RadarrArr) GetHistory() (radarr.History, error) {
arr.LastUpdateCountMutex.Lock()
defer arr.LastUpdateCountMutex.Unlock()

if time.Since(arr.LastUpdate) > 30*time.Second || arr.History == nil {
if time.Since(arr.LastUpdate) > time.Duration(arr.Config.ArrHistoryUpdateIntervalSeconds)*time.Second || arr.History == nil {
his, err := arr.Client.GetHistory(0, 1000)
if err != nil {
return radarr.History{}, err
Expand All @@ -35,9 +35,10 @@ func (arr *RadarrArr) GetHistory() (radarr.History, error) {
arr.History = his
arr.LastUpdate = time.Now()
arr.LastUpdateCount = his.TotalRecords
log.Debugf("[Radarr] [%s]: Updated history, next update in %d seconds", arr.Name, arr.Config.ArrHistoryUpdateIntervalSeconds)
}

log.Tracef("Radarr.GetHistory(): Returning from GetHistory")
log.Tracef("[Radarr] [%s]: Returning from GetHistory", arr.Name)
return *arr.History, nil
}

Expand Down
5 changes: 3 additions & 2 deletions internal/arr/sonarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (arr *SonarrArr) GetHistory() (sonarr.History, error) {
arr.LastUpdateCountMutex.Lock()
defer arr.LastUpdateCountMutex.Unlock()

if time.Since(arr.LastUpdate) > 30*time.Second || arr.History == nil {
if time.Since(arr.LastUpdate) > time.Duration(arr.Config.ArrHistoryUpdateIntervalSeconds)*time.Second || arr.History == nil {
his, err := arr.Client.GetHistory(0, 1000)
if err != nil {
return sonarr.History{}, err
Expand All @@ -35,9 +35,10 @@ func (arr *SonarrArr) GetHistory() (sonarr.History, error) {
arr.History = his
arr.LastUpdate = time.Now()
arr.LastUpdateCount = his.TotalRecords
log.Debugf("[Sonarr] [%s]: Updated history, next update in %d seconds", arr.Name, arr.Config.ArrHistoryUpdateIntervalSeconds)
}

log.Tracef("Sonarr.GetHistory(): Returning from GetHistory")
log.Tracef("[Sonarr] [%s]: Returning from GetHistory", arr.Name)
return *arr.History, nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/arr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"
"time"

"github.com/jackdallas/premiumizearr/internal/config"
"github.com/jackdallas/premiumizearr/internal/utils"
"github.com/jackdallas/premiumizearr/pkg/premiumizeme"
"golift.io/starr/radarr"
Expand Down Expand Up @@ -54,6 +55,7 @@ type SonarrArr struct {
LastUpdate time.Time
LastUpdateCount int
LastUpdateCountMutex sync.Mutex
Config *config.Config
}

type RadarrArr struct {
Expand All @@ -66,4 +68,5 @@ type RadarrArr struct {
LastUpdate time.Time
LastUpdateCount int
LastUpdateCountMutex sync.Mutex
Config *config.Config
}
25 changes: 16 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ func loadConfigFromDisk(altConfigLocation string) (Config, error) {
updated = true
}

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

config.altConfigLocation = altConfigLocation

if updated {
Expand Down Expand Up @@ -154,15 +160,16 @@ func defaultConfig() Config {
{Name: "Sonarr", URL: "http://localhost:8989", APIKey: "xxxxxxxxx", Type: Sonarr},
{Name: "Radarr", URL: "http://localhost:7878", APIKey: "xxxxxxxxx", Type: Radarr},
},
BlackholeDirectory: "",
PollBlackholeDirectory: false,
PollBlackholeIntervalMinutes: 10,
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,
ArrHistoryUpdateIntervalSeconds: 20,
}
}

Expand Down
6 changes: 4 additions & 2 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ var (
ErrFailedToSaveConfig = errors.New("failed to save config")
)

//ArrType enum for Sonarr/Radarr
// ArrType enum for Sonarr/Radarr
type ArrType string

//AppCallback - Callback for the app to use
// AppCallback - Callback for the app to use
type AppCallback func(oldConfig Config, newConfig Config)

const (
Expand Down Expand Up @@ -49,4 +49,6 @@ type Config struct {
WebRoot string `yaml:"WebRoot" json:"WebRoot"`

SimultaneousDownloads int `yaml:"SimultaneousDownloads" json:"SimultaneousDownloads"`

ArrHistoryUpdateIntervalSeconds int `yaml:"ArrHistoryUpdateIntervalSeconds" json:"ArrHistoryUpdateIntervalSeconds"`
}
2 changes: 2 additions & 0 deletions internal/service/arrs_manager_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (am *ArrsManagerService) Start() {
Client: sonarr.New(c),
History: nil,
LastUpdate: time.Now(),
Config: am.config,
}
am.arrs = append(am.arrs, &wrapper)
log.Tracef("Added Sonarr arr: %s", arr_config.Name)
Expand All @@ -47,6 +48,7 @@ func (am *ArrsManagerService) Start() {
Client: radarr.New(c),
History: nil,
LastUpdate: time.Now(),
Config: am.config,
}
am.arrs = append(am.arrs, &wrapper)
log.Tracef("Added Radarr arr: %s", arr_config.Name)
Expand Down
12 changes: 9 additions & 3 deletions web/src/pages/Config.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
Modal,
FormGroup,
Dropdown,
Form,
Checkbox,
Form,
Checkbox,
} from "carbon-components-svelte";
import {
Save,
Expand Down Expand Up @@ -171,7 +171,7 @@ Checkbox,
SetTestArr(index, HelpFilled, "secondary", false);
}, 1000 * seconds);
}
getConfig();
</script>

Expand All @@ -180,6 +180,12 @@ Checkbox,
<Column>
<h4>*Arr Settings</h4>
<FormGroup>
<TextInput
type="number"
disabled={inputDisabled}
labelText="Arr Update History Interval (seconds)"
bind:value={config.ArrHistoryUpdateIntervalSeconds}
/>
{#if config.Arrs !== undefined}
{#each config.Arrs as arr, i}
<h5>- {arr.Name ? arr.Name : i}</h5>
Expand Down

0 comments on commit 6c6d8d8

Please sign in to comment.