Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make desktop.Start run on main thread #2475

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions cmd/stash/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"fmt"
"os"
"os/signal"
"runtime/pprof"
"syscall"

"github.com/apenwarr/fixconsole"
"github.com/stashapp/stash/internal/api"
"github.com/stashapp/stash/internal/desktop"
"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/source/file"
Expand All @@ -28,18 +29,21 @@ func main() {
manager.Initialize()
api.Start()

blockForever()

// stop any profiling at exit
pprof.StopCPUProfile()
go handleSignals()
desktop.Start(manager.GetInstance(), &manager.FaviconProvider{UIBox: ui.UIBox})

manager.GetInstance().Shutdown(0)
blockForever()
}

func blockForever() {
func handleSignals() {
// handle signals
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)

<-signals
manager.GetInstance().Shutdown(0)
}

func blockForever() {
select {}
}
1 change: 1 addition & 0 deletions internal/desktop/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type FaviconProvider interface {
}

// 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) {
if IsDesktop() {
c := config.GetInstance()
Expand Down
6 changes: 3 additions & 3 deletions internal/manager/favicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
const faviconDir = "v2.5/build/"

type FaviconProvider struct {
uiBox embed.FS
UIBox embed.FS
}

func (p *FaviconProvider) GetFavicon() []byte {
if runtime.GOOS == "windows" {
faviconPath := faviconDir + "favicon.ico"
ret, _ := p.uiBox.ReadFile(faviconPath)
ret, _ := p.UIBox.ReadFile(faviconPath)
return ret
}

Expand All @@ -23,6 +23,6 @@ func (p *FaviconProvider) GetFavicon() []byte {

func (p *FaviconProvider) GetFaviconPng() []byte {
faviconPath := faviconDir + "favicon.png"
ret, _ := p.uiBox.ReadFile(faviconPath)
ret, _ := p.UIBox.ReadFile(faviconPath)
return ret
}
7 changes: 5 additions & 2 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ func (s *singleton) PostInit(ctx context.Context) error {

s.ScraperCache = instance.initScraperCache()
writeStashIcon()
go desktop.Start(instance, &FaviconProvider{uiBox: ui.UIBox})

// clear the downloads and tmp directories
// #1021 - only clear these directories if the generated folder is non-empty
Expand Down Expand Up @@ -289,7 +288,7 @@ func (s *singleton) PostInit(ctx context.Context) error {

func writeStashIcon() {
p := FaviconProvider{
uiBox: ui.UIBox,
UIBox: ui.UIBox,
}

iconPath := filepath.Join(instance.Config.GetConfigPath(), "icon.png")
Expand Down Expand Up @@ -484,6 +483,9 @@ func (s *singleton) GetSystemStatus() *models.SystemStatus {

// Shutdown gracefully stops the manager
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
// for now, we just close the database.
err := database.Close()
Expand All @@ -493,5 +495,6 @@ func (s *singleton) Shutdown(code int) {
os.Exit(1)
}
}

os.Exit(code)
}