Make desktop.Start run on main thread (#2475)

This commit is contained in:
WithoutPants 2022-04-05 14:58:08 +10:00 committed by GitHub
parent 61d9f57ce9
commit 230d8f6028
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 12 deletions

View file

@ -5,12 +5,13 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"runtime/pprof"
"syscall" "syscall"
"github.com/apenwarr/fixconsole" "github.com/apenwarr/fixconsole"
"github.com/stashapp/stash/internal/api" "github.com/stashapp/stash/internal/api"
"github.com/stashapp/stash/internal/desktop"
"github.com/stashapp/stash/internal/manager" "github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/ui"
_ "github.com/golang-migrate/migrate/v4/database/sqlite3" _ "github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "github.com/golang-migrate/migrate/v4/source/file" _ "github.com/golang-migrate/migrate/v4/source/file"
@ -28,18 +29,21 @@ func main() {
manager.Initialize() manager.Initialize()
api.Start() api.Start()
go handleSignals()
desktop.Start(manager.GetInstance(), &manager.FaviconProvider{UIBox: ui.UIBox})
blockForever() blockForever()
// stop any profiling at exit
pprof.StopCPUProfile()
manager.GetInstance().Shutdown(0)
} }
func blockForever() { func handleSignals() {
// handle signals // handle signals
signals := make(chan os.Signal, 1) signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals <-signals
manager.GetInstance().Shutdown(0)
}
func blockForever() {
select {}
} }

View file

@ -26,6 +26,7 @@ type FaviconProvider interface {
} }
// Start starts the desktop icon process. It blocks until the process exits. // Start starts the desktop icon process. It blocks until the process exits.
// MUST be run on the main goroutine or will have no effect on macOS
func Start(shutdownHandler ShutdownHandler, faviconProvider FaviconProvider) { func Start(shutdownHandler ShutdownHandler, faviconProvider FaviconProvider) {
if IsDesktop() { if IsDesktop() {
c := config.GetInstance() c := config.GetInstance()

View file

@ -8,13 +8,13 @@ import (
const faviconDir = "v2.5/build/" const faviconDir = "v2.5/build/"
type FaviconProvider struct { type FaviconProvider struct {
uiBox embed.FS UIBox embed.FS
} }
func (p *FaviconProvider) GetFavicon() []byte { func (p *FaviconProvider) GetFavicon() []byte {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
faviconPath := faviconDir + "favicon.ico" faviconPath := faviconDir + "favicon.ico"
ret, _ := p.uiBox.ReadFile(faviconPath) ret, _ := p.UIBox.ReadFile(faviconPath)
return ret return ret
} }
@ -23,6 +23,6 @@ func (p *FaviconProvider) GetFavicon() []byte {
func (p *FaviconProvider) GetFaviconPng() []byte { func (p *FaviconProvider) GetFaviconPng() []byte {
faviconPath := faviconDir + "favicon.png" faviconPath := faviconDir + "favicon.png"
ret, _ := p.uiBox.ReadFile(faviconPath) ret, _ := p.UIBox.ReadFile(faviconPath)
return ret return ret
} }

View file

@ -255,7 +255,6 @@ func (s *singleton) PostInit(ctx context.Context) error {
s.ScraperCache = instance.initScraperCache() s.ScraperCache = instance.initScraperCache()
writeStashIcon() writeStashIcon()
go desktop.Start(instance, &FaviconProvider{uiBox: ui.UIBox})
// clear the downloads and tmp directories // clear the downloads and tmp directories
// #1021 - only clear these directories if the generated folder is non-empty // #1021 - only clear these directories if the generated folder is non-empty
@ -289,7 +288,7 @@ func (s *singleton) PostInit(ctx context.Context) error {
func writeStashIcon() { func writeStashIcon() {
p := FaviconProvider{ p := FaviconProvider{
uiBox: ui.UIBox, UIBox: ui.UIBox,
} }
iconPath := filepath.Join(instance.Config.GetConfigPath(), "icon.png") iconPath := filepath.Join(instance.Config.GetConfigPath(), "icon.png")
@ -484,6 +483,9 @@ func (s *singleton) GetSystemStatus() *models.SystemStatus {
// Shutdown gracefully stops the manager // Shutdown gracefully stops the manager
func (s *singleton) Shutdown(code int) { func (s *singleton) Shutdown(code int) {
// stop any profiling at exit
pprof.StopCPUProfile()
// TODO: Each part of the manager needs to gracefully stop at some point // TODO: Each part of the manager needs to gracefully stop at some point
// for now, we just close the database. // for now, we just close the database.
err := database.Close() err := database.Close()
@ -493,5 +495,6 @@ func (s *singleton) Shutdown(code int) {
os.Exit(1) os.Exit(1)
} }
} }
os.Exit(code) os.Exit(code)
} }