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: 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
directories:

View File

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

View File

@@ -4,4 +4,7 @@ type ZurgConfig struct {
Version string `yaml:"zurg"`
Token string `yaml:"token"`
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
}
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 {
var rootDirectories []string
for directory := range z.Directories {

View File

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