Skip to content

Commit

Permalink
home: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Aug 28, 2023
1 parent 2dd357f commit f8a37b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
33 changes: 10 additions & 23 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"path/filepath"
"runtime"
"sync"
"sync/atomic"
"syscall"
"time"

Expand Down Expand Up @@ -100,21 +99,11 @@ func Main(clientBuildFS fs.FS) {
// package flag.
opts := loadCmdLineOpts()

done := make(chan struct{})

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)

// TODO(s.chzhen): Think of better way to do graceful shutdown.
var cleaningMu sync.Mutex
cleanExit := func() {
cleaningMu.Lock()
defer cleaningMu.Unlock()

cleanup(context.Background())
cleanupAlways()

os.Exit(0)
}

go func() {
for {
sig := <-signals
Expand All @@ -124,23 +113,21 @@ func Main(clientBuildFS fs.FS) {
Context.clients.reloadARP()
Context.tls.reload()
default:
cleanExit()
cleanup(context.Background())
cleanupAlways()
close(done)
}
}
}()

if opts.serviceControlAction != "" {
isStopping := new(atomic.Bool)
handleServiceControlAction(opts, clientBuildFS, isStopping)
if isStopping.Load() {
cleanExit()
}
handleServiceControlAction(opts, clientBuildFS, done)

return
}

// run the protection
run(opts, clientBuildFS)
run(opts, clientBuildFS, done)
}

// setupContext initializes [Context] fields. It also reads and upgrades
Expand Down Expand Up @@ -520,7 +507,7 @@ func fatalOnError(err error) {
}

// run configures and starts AdGuard Home.
func run(opts options, clientBuildFS fs.FS) {
func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
// Configure config filename.
initConfigFilename(opts)

Expand Down Expand Up @@ -625,8 +612,8 @@ func run(opts options, clientBuildFS fs.FS) {

Context.web.start()

// Wait indefinitely for other goroutines to complete their job.
select {}
// Wait for other goroutines to complete their job.
<-done
}

// initUsers initializes context auth module. Clears config users field.
Expand Down
13 changes: 7 additions & 6 deletions internal/home/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"runtime"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"

Expand All @@ -34,7 +33,7 @@ const (
// daemon.
type program struct {
clientBuildFS fs.FS
isStopping *atomic.Bool
done chan struct{}
opts options
}

Expand All @@ -47,14 +46,16 @@ func (p *program) Start(_ service.Service) (err error) {
args := p.opts
args.runningAsService = true

go run(args, p.clientBuildFS)
go run(args, p.clientBuildFS, p.done)

return nil
}

// Stop implements service.Interface interface for *program.
func (p *program) Stop(_ service.Service) (err error) {
p.isStopping.Store(true)
log.Info("service: stopping: waiting for cleanup")

<-p.done

return nil
}
Expand Down Expand Up @@ -194,7 +195,7 @@ func restartService() (err error) {
// - run: This is a special command that is not supposed to be used directly
// it is specified when we register a service, and it indicates to the app
// that it is being run as a service/daemon.
func handleServiceControlAction(opts options, clientBuildFS fs.FS, isStopping *atomic.Bool) {
func handleServiceControlAction(opts options, clientBuildFS fs.FS, done chan struct{}) {
// Call chooseSystem explicitly to introduce OpenBSD support for service
// package. It's a noop for other GOOS values.
chooseSystem()
Expand Down Expand Up @@ -228,7 +229,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS, isStopping *a

s, err := service.New(&program{
clientBuildFS: clientBuildFS,
isStopping: isStopping,
done: done,
opts: runOpts,
}, svcConfig)
if err != nil {
Expand Down

0 comments on commit f8a37b4

Please sign in to comment.