Skip to content

Commit

Permalink
Register as a Windows service and fix issue with reader closer.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakerouse committed Jul 27, 2020
1 parent 06b40e9 commit a70e58e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
10 changes: 2 additions & 8 deletions x-pack/elastic-agent/pkg/agent/application/info/agent_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
return nil, err
}

// reader is closed by this function
cfg, err := config.NewConfigFrom(reader)
if err != nil {
return nil, errors.New(err,
Expand All @@ -94,10 +95,6 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
errors.M(errors.MetaKeyPath, agentConfigFile))
}

if err := reader.Close(); err != nil {
return nil, err
}

configMap, err := cfg.ToMapStr()
if err != nil {
return nil, errors.New(err,
Expand Down Expand Up @@ -130,17 +127,14 @@ func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error {
return err
}

// reader is closed by this function
cfg, err := config.NewConfigFrom(reader)
if err != nil {
return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, agentConfigFile))
}

if err := reader.Close(); err != nil {
return err
}

configMap := make(map[string]interface{})
if err := cfg.Unpack(&configMap); err != nil {
return errors.New(err, "failed to unpack stored config to map")
Expand Down
26 changes: 25 additions & 1 deletion x-pack/elastic-agent/pkg/agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
package cmd

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/service"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration"
Expand Down Expand Up @@ -57,12 +60,20 @@ func run(flags *globalFlags, streams *cli.IOStreams) error {
return err
}

// Windows: Mark service as stopped.
// After this is run, the service is considered by the OS to be stopped.
// This must be the first deferred cleanup task (last to execute).
defer service.NotifyTermination()

locker := application.NewAppLocker(paths.Data())
if err := locker.TryLock(); err != nil {
return err
}
defer locker.Unlock()

service.BeforeRun()
defer service.Cleanup()

app, err := application.New(logger, pathConfigFile)
if err != nil {
return err
Expand All @@ -72,11 +83,24 @@ func run(flags *globalFlags, streams *cli.IOStreams) error {
return err
}

// register as a service
stop := make(chan bool)
_, cancel := context.WithCancel(context.Background())
var stopBeat = func() {
close(stop)
}
service.HandleSignals(stopBeat, cancel)

// listen for kill signal
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGQUIT)

<-signals
select {
case <-stop:
break
case <-signals:
break
}

return app.Stop()
}

0 comments on commit a70e58e

Please sign in to comment.