This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
butler_inspect.go
154 lines (148 loc) · 7.01 KB
/
butler_inspect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"time"
"github.com/hekmon/transmissionrpc"
)
func inspectTorrents(torrents []*transmissionrpc.Torrent) (
freeseedCandidates, globalratioCandidates, customratioCandidates, todeleteCandidates []*transmissionrpc.Torrent) {
// Only 1 run at a time !
defer butlerRun.Unlock()
logger.Debugf("[Butler] Waiting for butlerRun lock")
butlerRun.Lock()
// Prepare
freeseedCandidates = make([]*transmissionrpc.Torrent, 0, len(torrents))
globalratioCandidates = make([]*transmissionrpc.Torrent, 0, len(torrents))
customratioCandidates = make([]*transmissionrpc.Torrent, 0, len(torrents))
todeleteCandidates = make([]*transmissionrpc.Torrent, 0, len(torrents))
now := time.Now()
// Start inspection
for index, torrent := range torrents {
// Checks
if !torrentOK(torrent, index) {
continue
}
// We can now safely access metadata
if logger.IsDebugShown() {
logger.Debugf("[Butler] Inspecting torrent %d:\n\tid:\t\t%d\n\tname:\t\t%s\n\tsize:\t\t%s\n\tstatus:\t\t%s\n\tdoneDate:\t%v\n\tseedRatioLimit:\t%f\n\tseedRatioMode:\t%s\n\tuploadRatio:\t%f",
index, *torrent.ID, *torrent.Name, *torrent.Status, *torrent.TotalSize, *torrent.DoneDate, *torrent.SeedRatioLimit, *torrent.SeedRatioMode, *torrent.UploadRatio)
}
// For seeding torrents
if *torrent.Status == transmissionrpc.TorrentStatusSeed || *torrent.Status == transmissionrpc.TorrentStatusSeedWait {
// Is this a custom torrent, should we leave it alone ?
if *torrent.SeedRatioMode == transmissionrpc.SeedRatioModeCustom {
if logger.IsDebugShown() {
logger.Debugf("[Butler] Seeding torrent id %d (%s) has a custom ratio enabled: skipping", *torrent.ID, *torrent.Name)
}
continue
}
// Else process it
inspectSeedingTorrent(torrent, now, &freeseedCandidates, &globalratioCandidates, &customratioCandidates)
}
// For stopped/finished torrents
if conf.Butler.DeleteDone && *torrent.Status == transmissionrpc.TorrentStatusStopped {
inspectStoppedTorrent(torrent, &todeleteCandidates)
}
}
return
}
func torrentOK(torrent *transmissionrpc.Torrent, index int) (ok bool) {
if torrent == nil {
logger.Warningf("[Butler] Encountered a nil torrent at index %d", index)
return
}
if torrent.ID == nil {
logger.Warningf("[Butler] Encountered a nil torrent id at index %d", index)
return
}
if torrent.Name == nil {
logger.Warningf("[Butler] Encountered a nil torrent name at index %d", index)
return
}
if torrent.Status == nil {
logger.Warningf("[Butler] Encountered a nil torrent status at index %d", index)
return
}
if torrent.DoneDate == nil {
logger.Warningf("[Butler] Encountered a nil torrent doneDate at index %d", index)
return
}
if torrent.SeedRatioLimit == nil {
logger.Warningf("[Butler] Encountered a nil torrent seedRatioLimit at index %d", index)
return
}
if torrent.SeedRatioMode == nil {
logger.Warningf("[Butler] Encountered a nil torrent seedRatioMode at index %d", index)
return
}
if torrent.UploadRatio == nil {
logger.Warningf("[Butler] Encountered a nil torrent ID at index %d", index)
return
}
return true
}
func inspectSeedingTorrent(torrent *transmissionrpc.Torrent, now time.Time, freeseedCandidates, globalratioCandidates, customratioCandidates *[]*transmissionrpc.Torrent) {
// Does this torrent is under/over the free seed time range ?
if torrent.DoneDate.Add(conf.Butler.FreeSeed).Before(now) {
// Torrent is over the unlimited seed time range
if conf.Butler.RestoreCustom && *torrent.SeedRatioLimit != conf.Butler.TargetRatio {
// This torrent had a custom ratio saved, let's check if this torrent does not need to be restored as custom ratio
if *torrent.SeedRatioMode != transmissionrpc.SeedRatioModeCustom {
logger.Infof("[Butler] Seeding torrent id %d (%s) is now over its unlimited seed period: adding it to the restore custom ratio list",
*torrent.ID, *torrent.Name)
*customratioCandidates = append(*customratioCandidates, torrent)
} else if logger.IsDebugShown() {
logger.Debugf("[Butler] Seeding torrent id %d (%s) is correctly set to use the custom ratio mode (free seed ending date: %v, RestoreCustom: %v, TorrentRatio: %v, GlobalRatio: %v)",
*torrent.ID, *torrent.Name, torrent.DoneDate.Add(conf.Butler.FreeSeed), conf.Butler.RestoreCustom, *torrent.SeedRatioLimit, conf.Butler.TargetRatio)
}
} else {
// Let's check if this torrent is in global ratio mode as it should be
if *torrent.SeedRatioMode != transmissionrpc.SeedRatioModeGlobal {
logger.Infof("[Butler] Seeding torrent id %d (%s) is now over its unlimited seed period: adding it to the global ratio list",
*torrent.ID, *torrent.Name)
*globalratioCandidates = append(*globalratioCandidates, torrent)
} else if logger.IsDebugShown() {
logger.Debugf("[Butler] Seeding torrent id %d (%s) is correctly set to use the global ratio mode (free seed ending date: %v, RestoreCustom: %v, TorrentRatio: %v, GlobalRatio: %v)",
*torrent.ID, *torrent.Name, torrent.DoneDate.Add(conf.Butler.FreeSeed), conf.Butler.RestoreCustom, *torrent.SeedRatioLimit, conf.Butler.TargetRatio)
}
}
} else {
// Torrent is still within the unlimited seed time range
if *torrent.SeedRatioMode != transmissionrpc.SeedRatioModeNoRatio {
logger.Infof("[Butler] Seeding torrent id %d (%s) is still young: adding it to the free seed ratio list",
*torrent.ID, *torrent.Name)
*freeseedCandidates = append(*freeseedCandidates, torrent)
} else if logger.IsDebugShown() {
logger.Debugf("[Butler] Seeding torrent id %d (%s) is correctly set to use the free seed mode (free seed ending date: %v)",
*torrent.ID, *torrent.Name, torrent.DoneDate.Add(conf.Butler.FreeSeed))
}
}
}
func inspectStoppedTorrent(torrent *transmissionrpc.Torrent, todeleteCandidates *[]*transmissionrpc.Torrent) {
var targetRatio float64
// Should we handle this stopped torrent ?
if *torrent.SeedRatioMode == transmissionrpc.SeedRatioModeCustom {
targetRatio = *torrent.SeedRatioLimit
} else if *torrent.SeedRatioMode == transmissionrpc.SeedRatioModeGlobal {
targetRatio = conf.Butler.TargetRatio
} else {
if *torrent.SeedRatioMode == transmissionrpc.SeedRatioModeNoRatio {
if logger.IsDebugShown() {
logger.Debugf("[Butler] Torrent id %d (%s) is finished (ratio %f) but it does not have a ratio target (custom or global): skipping",
*torrent.ID, *torrent.Name, *torrent.UploadRatio)
}
} else {
logger.Warningf("[Butler] Torrent id %d (%s) is finished but has an unknown seed ratio mode (%d): skipping",
*torrent.ID, *torrent.Name, *torrent.SeedRatioMode)
}
return
}
// We should handle it but does it have seeded enought ?
if *torrent.UploadRatio >= targetRatio {
logger.Infof("[Butler] Torrent id %d (%s) is finished (ratio %f/%f): adding it to deletion list",
*torrent.ID, *torrent.Name, *torrent.UploadRatio, targetRatio)
*todeleteCandidates = append(*todeleteCandidates, torrent)
} else if logger.IsDebugShown() {
logger.Debugf("[Butler] Torrent id %d (%s) is finished but it does not have reached its target ratio yet: %f/%f",
*torrent.ID, *torrent.Name, *torrent.UploadRatio, targetRatio)
}
}