github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/jujud/agent/introspection.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package agent
     5  
     6  import (
     7  	"runtime"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/agent"
    12  	"github.com/juju/juju/worker"
    13  	"github.com/juju/juju/worker/dependency"
    14  	"github.com/juju/juju/worker/introspection"
    15  )
    16  
    17  // introspectionConfig defines the various components that the introspection
    18  // worker reports on or needs to start up.
    19  type introspectionConfig struct {
    20  	Agent      agent.Agent
    21  	Engine     *dependency.Engine
    22  	WorkerFunc func(config introspection.Config) (worker.Worker, error)
    23  }
    24  
    25  // startIntrospection creates the introspection worker. It cannot and should
    26  // not be in the engine itself as it reports on the engine, and other aspects
    27  // of the runtime. If we put it in the engine, then it is mostly likely shut
    28  // down in the times we need it most, which is when the agent is having
    29  // problems shutting down. Here we effectively start the worker and tie its
    30  // life to that of the engine that is returned.
    31  func startIntrospection(cfg introspectionConfig) error {
    32  	if runtime.GOOS != "linux" {
    33  		logger.Debugf("introspection worker not supported on %q", runtime.GOOS)
    34  		return nil
    35  	}
    36  
    37  	socketName := "jujud-" + cfg.Agent.CurrentConfig().Tag().String()
    38  	w, err := cfg.WorkerFunc(introspection.Config{
    39  		SocketName: socketName,
    40  		Reporter:   cfg.Engine,
    41  	})
    42  	if err != nil {
    43  		return errors.Trace(err)
    44  	}
    45  	go func() {
    46  		cfg.Engine.Wait()
    47  		logger.Debugf("engine stopped, stopping introspection")
    48  		w.Kill()
    49  		w.Wait()
    50  		logger.Debugf("introspection stopped")
    51  	}()
    52  
    53  	return nil
    54  }