forked from elgatito/elementum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
355 lines (301 loc) · 8.54 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"expvar"
"fmt"
"io"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"syscall"
"time"
"github.com/anacrolix/missinggo/perf"
"github.com/anacrolix/sync"
"github.com/anacrolix/tagflag"
"github.com/op/go-logging"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/elgatito/elementum/api"
"github.com/elgatito/elementum/bittorrent"
"github.com/elgatito/elementum/broadcast"
"github.com/elgatito/elementum/config"
"github.com/elgatito/elementum/database"
"github.com/elgatito/elementum/exit"
"github.com/elgatito/elementum/library"
"github.com/elgatito/elementum/lockfile"
"github.com/elgatito/elementum/repository"
"github.com/elgatito/elementum/trakt"
"github.com/elgatito/elementum/util"
"github.com/elgatito/elementum/util/ident"
"github.com/elgatito/elementum/util/ip"
"github.com/elgatito/elementum/xbmc"
)
var (
log = logging.MustGetLogger("main")
logPath = ""
fileLogger *lumberjack.Logger
)
func init() {
sync.Enable()
}
func ensureSingleInstance(conf *config.Configuration) (lock *lockfile.LockFile, err error) {
// Avoid killing any process when running as a shared library
if exit.IsShared {
return
}
file := filepath.Join(conf.Info.Profile, ".lockfile")
lock, err = lockfile.New(file)
if err != nil {
log.Critical("Unable to initialize lockfile:", err)
return
}
var pid int
var p *os.Process
pid, err = lock.Lock()
if pid <= 0 {
if err = os.Remove(lock.File); err != nil {
log.Critical("Unable to remove lockfile")
return
}
_, err = lock.Lock()
} else if err != nil {
log.Warningf("Unable to acquire lock %q: %v, killing...", lock.File, err)
p, err = os.FindProcess(pid)
if err != nil {
log.Warning("Unable to find other process:", err)
return
}
if err = p.Kill(); err != nil {
log.Critical("Unable to kill other process:", err)
return
}
if err = os.Remove(lock.File); err != nil {
log.Critical("Unable to remove lockfile")
return
}
_, err = lock.Lock()
}
return
}
func stopLogging() {
if fileLogger != nil {
log.Infof("Stopping file logger")
fileLogger.Close()
fileLogger = nil
}
}
func setupLogging() {
var backend *logging.LogBackend
if config.Args.LogPath != "" {
logPath = config.Args.LogPath
}
if logPath != "" && util.IsWritablePath(filepath.Base(logPath)) == nil {
fileLogger = &lumberjack.Logger{
Filename: logPath,
MaxSize: 10, // Size in Megabytes
MaxBackups: 5,
}
backend = logging.NewLogBackend(fileLogger, "", 0)
} else {
backend = logging.NewLogBackend(os.Stdout, "", 0)
}
// Make sure we reset to initial state before configuring logger instance
logging.Reset()
logging.SetFormatter(logging.MustStringFormatter(
`%{color}%{level:.4s} %{module:-12s} ▶ %{shortfunc:-15s} %{color:reset}%{message}`,
))
logging.SetBackend(logging.NewLogBackend(io.Discard, "", 0), backend)
}
func main() {
now := time.Now()
// If running in shared library mode, parse Args from variable, provided by library caller.
if !exit.IsShared || exit.Args == "" {
tagflag.Parse(&config.Args)
} else {
if err := tagflag.ParseErr(&config.Args, strings.Fields(exit.Args)); err != nil {
fmt.Printf("Error parsing CLI arguments: %s", err)
exit.Exit(exit.ExitCodeError)
return
}
}
// Make sure we are properly multithreaded.
runtime.GOMAXPROCS(runtime.NumCPU())
setupLogging()
defer func() {
stopLogging()
if r := recover(); r != nil {
log.Errorf("Got a panic: %s", r)
log.Errorf("Stacktrace: \n" + string(debug.Stack()))
exit.Exit(exit.ExitCodeError)
}
}()
if exit.IsShared {
log.Infof("Starting Elementum daemon in shared library mode")
} else {
log.Infof("Starting Elementum daemon")
}
log.Infof("Version: %s LibTorrent: %s Go: %s, Threads: %d", ident.GetVersion(), ident.GetTorrentVersion(), runtime.Version(), runtime.GOMAXPROCS(0))
// Init default XBMC connections
xbmc.Init()
conf, err := config.Reload()
if err != nil || conf == nil {
log.Errorf("Could not get addon configuration: %s", err)
exit.Exit(exit.ExitCodeError)
return
}
xbmc.KodiVersion = conf.Platform.Kodi
log.Infof("Addon: %s v%s", conf.Info.ID, conf.Info.Version)
lock, err := ensureSingleInstance(conf)
if err != nil {
if lock != nil {
log.Warningf("Unable to acquire lock %q: %s, exiting...", lock.File, err)
} else {
log.Warningf("Unable to acquire lock: %s, exiting...", err)
}
exit.Exit(exit.ExitCodeError)
}
if lock != nil {
defer lock.Unlock()
}
db, err := database.InitStormDB(conf)
if err != nil {
log.Errorf("Could not open application database: %s", err)
exit.Exit(exit.ExitCodeError)
return
}
cacheDB, errCache := database.InitCacheDB(conf)
if errCache != nil {
log.Errorf("Could not open cache database: %s", errCache)
exit.Exit(exit.ExitCodeError)
return
}
s := bittorrent.NewService()
var shutdown = func(code int) {
if s == nil || s.Closer.IsSet() {
return
}
// Set global Closer
broadcast.Closer.Set()
s.Closer.Set()
log.Infof("Shutting down with code %d ...", code)
library.CloseLibrary()
s.Close(true)
db.Close()
cacheDB.Close()
// Wait until service is finally stopped
<-s.CloserNotifier.C()
log.Info("Goodbye")
if lock != nil {
lock.Unlock()
}
// If we don't give an exit code - python treat as well done and not
// restarting the daemon. So when we come here from Signal -
// we should properly exit with non-0 exitcode.
exit.Exit(code)
}
var watchParentProcess = func() {
for {
if os.Getppid() == 1 {
log.Warning("Parent shut down, shutting down too...")
go shutdown(exit.ExitCodeSuccess)
break
}
time.Sleep(1 * time.Second)
}
}
// If we run with custom config, then we run as daemon, thus no need to watch for parent process
if config.Args.ConfigPath == "" {
go watchParentProcess()
}
// Make sure HTTP mux is empty
http.DefaultServeMux = new(http.ServeMux)
// Debug handlers
http.HandleFunc("/debug/pprof/", pprof.Index)
http.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
http.HandleFunc("/debug/pprof/profile", pprof.Profile)
http.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
http.HandleFunc("/debug/pprof/trace", pprof.Trace)
http.HandleFunc("/debug/perf", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
perf.WriteEventsTable(w)
})
http.HandleFunc("/debug/lockTimes", func(w http.ResponseWriter, r *http.Request) {
sync.PrintLockTimes(w)
})
http.Handle("/debug/vars", expvar.Handler())
http.Handle("/", api.Routes(s, shutdown))
http.Handle("/files/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Connection", "close")
handler := http.StripPrefix("/files/", http.FileServer(bittorrent.NewTorrentFS(s, r.Method)))
handler.ServeHTTP(w, r)
}))
if config.Get().GreetingEnabled {
if xbmcHost, _ := xbmc.GetLocalXBMCHost(); xbmcHost != nil {
xbmcHost.Notify("Elementum", "LOCALIZE[30208]", config.AddonIcon())
}
}
sigc := make(chan os.Signal, 2)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
signal.Ignore(syscall.SIGPIPE, syscall.SIGILL)
defer close(sigc)
go func() {
closer := s.Closer.C()
for {
select {
case <-closer:
return
case <-exit.Closer.C():
shutdown(exit.ExitCodeSuccess)
case <-sigc:
log.Infof("Initiating shutdown after receiving signal")
shutdown(exit.ExitCodeError)
}
}
}()
go func() {
xbmcHost, _ := xbmc.GetLocalXBMCHost()
if xbmcHost == nil || !xbmcHost.Ping() {
return
}
if repository.CheckRepository(xbmcHost, conf.SkipRepositorySearch, config.Get().Info.Path) {
// Wait until repository is available before using it
for i := 0; i <= 30; i++ {
if ip.TestRepositoryURL() == nil {
break
}
time.Sleep(1 * time.Second)
}
log.Info("Updating Kodi add-on repositories... ")
xbmcHost.UpdateAddonRepos()
go repository.CheckBurst(xbmcHost, conf.SkipBurstSearch, config.AddonIcon())
}
xbmcHost.DialogProgressBGCleanup()
xbmcHost.ResetRPC()
}()
go library.Init()
go trakt.TokenRefreshHandler()
go db.MaintenanceRefreshHandler()
go cacheDB.MaintenanceRefreshHandler()
go util.FreeMemoryGC()
localAddress := fmt.Sprintf("%s:%d", config.Args.LocalHost, config.Args.LocalPort)
log.Infof("Prepared in %s", time.Since(now))
log.Infof("Starting HTTP server at %s", localAddress)
exit.Server = &http.Server{
Addr: localAddress,
Handler: nil,
}
if err = exit.Server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
exit.Panic(err)
return
}
if !exit.IsShared {
os.Exit(exit.Code)
}
}