Add new configs

This commit is contained in:
Ben Sarmiento
2023-10-20 03:59:47 +02:00
parent 59eb51b37b
commit f1717a8d94
5 changed files with 40 additions and 16 deletions

View File

@@ -1,6 +1,12 @@
# Zurg configuration version # Zurg configuration version
zurg: v1 zurg: v1
token: YOUR_TOKEN_HERE
port: 9999
concurrent_workers: 10
check_for_changes_every_secs: 15
info_cache_time_hours: 12
# List of directory definitions and their filtering rules # List of directory definitions and their filtering rules
directories: directories:

View File

@@ -10,6 +10,9 @@ import (
type ConfigInterface interface { type ConfigInterface interface {
GetVersion() string GetVersion() string
GetToken() string GetToken() string
GetNumOfWorkers() int
GetRefreshEverySeconds() int
GetCacheTimeHours() int
GetPort() string GetPort() string
GetDirectories() []string GetDirectories() []string
MeetsConditions(directory, fileID, fileName string) bool MeetsConditions(directory, fileID, fileName string) bool

View File

@@ -1,7 +1,10 @@
package config package config
type ZurgConfig struct { type ZurgConfig struct {
Version string `yaml:"zurg"` Version string `yaml:"zurg"`
Token string `yaml:"token"` Token string `yaml:"token"`
Port string `yaml:"port"` Port string `yaml:"port"`
NumOfWorkers int `yaml:"concurrent_workers"`
RefreshEverySeconds int `yaml:"check_for_changes_every_secs"`
CacheTimeHours int `yaml:"info_cache_time_hours"`
} }

View File

@@ -28,6 +28,18 @@ func (z *ZurgConfigV1) GetPort() string {
return z.Port return z.Port
} }
func (z *ZurgConfigV1) GetNumOfWorkers() int {
return z.NumOfWorkers
}
func (z *ZurgConfigV1) GetRefreshEverySeconds() int {
return z.RefreshEverySeconds
}
func (z *ZurgConfigV1) GetCacheTimeHours() int {
return z.CacheTimeHours
}
func (z *ZurgConfigV1) GetDirectories() []string { func (z *ZurgConfigV1) GetDirectories() []string {
var rootDirectories []string var rootDirectories []string
for directory := range z.Directories { for directory := range z.Directories {

View File

@@ -21,21 +21,21 @@ type TorrentManager struct {
checksum string checksum string
} }
func (handler *TorrentManager) refreshTorrents() { func (t *TorrentManager) refreshTorrents() {
log.Println("Starting periodic refresh") log.Println("Starting periodic refresh")
for { for {
<-time.After(15 * time.Second) <-time.After(time.Duration(t.config.GetRefreshEverySeconds()) * time.Second)
checksum := handler.getChecksum() checksum := t.getChecksum()
if checksum == handler.checksum { if checksum == t.checksum {
continue continue
} }
handler.checksum = checksum t.checksum = checksum
handler.torrents = handler.getAll() t.torrents = t.getAll()
for _, torrent := range handler.torrents { for _, torrent := range t.torrents {
go func(id string) { go func(id string) {
handler.workerPool <- true t.workerPool <- true
handler.getInfo(id) t.getInfo(id)
<-handler.workerPool <-t.workerPool
time.Sleep(1 * time.Second) // sleep for 1 second to avoid rate limiting time.Sleep(1 * time.Second) // sleep for 1 second to avoid rate limiting
}(torrent.ID) }(torrent.ID)
} }
@@ -48,7 +48,7 @@ func (handler *TorrentManager) refreshTorrents() {
func NewTorrentManager(config config.ConfigInterface) *TorrentManager { func NewTorrentManager(config config.ConfigInterface) *TorrentManager {
handler := &TorrentManager{ handler := &TorrentManager{
token: config.GetToken(), token: config.GetToken(),
workerPool: make(chan bool, 10), workerPool: make(chan bool, config.GetNumOfWorkers()),
config: config, config: config,
} }
@@ -143,7 +143,7 @@ func (t *TorrentManager) RefreshInfo(torrentID string) {
if err == nil { if err == nil {
modTime := fileInfo.ModTime() modTime := fileInfo.ModTime()
// If the file was modified less than an hour ago, don't refresh // If the file was modified less than an hour ago, don't refresh
if time.Since(modTime) < time.Hour { if time.Since(modTime) < time.Duration(t.config.GetCacheTimeHours())*time.Hour {
return return
} }
err = os.Remove(filePath) err = os.Remove(filePath)
@@ -195,7 +195,7 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
var wg sync.WaitGroup var wg sync.WaitGroup
// Limit concurrency // Limit concurrency
sem := make(chan struct{}, 10) // e.g., 10 concurrent requests sem := make(chan struct{}, t.config.GetNumOfWorkers())
for _, link := range info.Links { for _, link := range info.Links {
wg.Add(1) wg.Add(1)