gosuki/daemon.go

123 lines
2.9 KiB
Go
Raw Normal View History

2019-02-18 18:44:27 +00:00
package main
import (
"os"
2023-02-18 23:13:23 +00:00
"git.blob42.xyz/gomark/gosuki/modules"
"git.blob42.xyz/gomark/gosuki/parsing"
2023-09-05 17:45:21 +00:00
"git.blob42.xyz/gomark/gosuki/profiles"
2023-02-18 23:13:23 +00:00
"git.blob42.xyz/gomark/gosuki/utils"
"git.blob42.xyz/gomark/gosuki/watch"
2020-08-12 18:15:17 +00:00
2023-02-18 23:13:23 +00:00
"git.blob42.xyz/sp4ke/gum"
2019-02-26 18:41:02 +00:00
2020-08-12 18:13:01 +00:00
"github.com/urfave/cli/v2"
2019-02-18 18:44:27 +00:00
)
var startDaemonCmd = &cli.Command{
Name: "daemon",
Aliases: []string{"d"},
2019-02-18 18:44:27 +00:00
Usage: "run browser watchers",
// Category: "daemon"
Action: startDaemon,
2019-02-18 18:44:27 +00:00
}
func startDaemon(c *cli.Context) error {
defer utils.CleanFiles()
2019-02-18 18:44:27 +00:00
manager := gum.NewManager()
manager.ShutdownOn(os.Interrupt)
api := NewApi()
manager.AddUnit(api)
go manager.Run()
// Initialize sqlite database available in global `cacheDB` variable
initDB()
registeredBrowsers := modules.GetBrowserModules()
2019-02-22 18:50:26 +00:00
2022-11-07 19:07:13 +00:00
// instanciate all browsers
2022-11-01 13:27:19 +00:00
for _, browserMod := range registeredBrowsers {
2022-11-01 13:27:19 +00:00
mod := browserMod.ModInfo()
// Create context
modContext := &modules.Context{
Cli: c,
}
//Create a browser instance
browser, ok := mod.New().(modules.BrowserModule)
2022-11-01 13:27:19 +00:00
if !ok {
log.Criticalf("module <%s> is not a BrowserModule", mod.ID)
}
log.Debugf("created browser instance <%s>", browser.Config().Name)
2022-11-07 19:07:13 +00:00
// shutdown logic
s, isShutdowner := browser.(modules.Shutdowner)
2022-11-01 13:27:19 +00:00
if isShutdowner {
defer handleShutdown(browser.Config().Name, s)
}
2022-11-01 13:27:19 +00:00
log.Debugf("new browser <%s> instance", browser.Config().Name)
h, ok := browser.(modules.HookRunner)
if ok {
//TODO: document hook running on watch events
h.RegisterHooks(parsing.ParseTags)
}
2019-03-01 17:30:50 +00:00
2023-09-05 17:45:21 +00:00
//WIP: Handle multiple profiles for modules who announce it - here ?
// Check if browser implements ProfileManager
//TODO: global flag for watch all
// if watch-all then for each profile setup the browser
bpm, ok := browser.(profiles.ProfileManager)
if ok {
//TODO : for each profile spawn a watcher
// list profiles
profs, err := bpm.GetProfiles()
if err != nil {
log.Critical("could not get profiles")
}
for _, p := range profs {
log.Debugf("profile: <%s>", p.Name)
}
} else {
log.Debugf("<%s> does not implement profiles.ProfileManager",
browser.Config().Name)
}
// calls the setup logic for each browser instance which
2022-11-07 19:07:13 +00:00
// includes the browsers.Initializer and browsers.Loader interfaces
err := modules.Setup(browser, modContext)
2019-03-01 17:30:50 +00:00
if err != nil {
2022-11-01 13:27:19 +00:00
log.Errorf("setting up <%s> %v", browser.Config().Name, err)
if isShutdowner {
2022-11-01 13:27:19 +00:00
handleShutdown(browser.Config().Name, s)
}
2019-03-01 17:30:50 +00:00
continue
}
2022-11-01 13:27:19 +00:00
runner, ok := browser.(watch.WatchRunner)
if !ok {
log.Criticalf("<%s> must implement watch.WatchRunner interface", browser.Config().Name)
continue
}
log.Infof("start watching <%s>", runner.Watch().ID)
2022-11-01 13:27:19 +00:00
watch.SpawnWatcher(runner)
2019-02-18 18:44:27 +00:00
}
<-manager.Quit
return nil
2019-02-18 18:44:27 +00:00
}
2022-11-01 13:27:19 +00:00
func handleShutdown(id string, s modules.Shutdowner) {
2022-11-01 13:27:19 +00:00
err := s.Shutdown()
if err != nil {
log.Panicf("could not shutdown browser <%s>", id)
}
}