Skip to content

Commit

Permalink
[core] only start collector/dogstatsd when needed
Browse files Browse the repository at this point in the history
Before any call to `agent.py` or `dogstatsd.py` would actually create a
`Agent` or `Dogstatsd` instance, without doing anaything with it (and
diplaying a message about its pidfile).

This commit changes this behaviour and only create them when needed.
  • Loading branch information
degemer committed Apr 30, 2015
1 parent 230b0ad commit 3e02b65
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
21 changes: 14 additions & 7 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def _handle_sigusr1(self, signum, frame):
self._handle_sigterm(signum, frame)
self._do_restart()

@classmethod
def info(self, verbose=None):
logging.getLogger().setLevel(logging.ERROR)
return CollectorStatus.print_latest_status(verbose=verbose)
Expand Down Expand Up @@ -212,19 +213,24 @@ def main():
autorestart = agentConfig.get('autorestart', False)
hostname = get_hostname(agentConfig)

COMMANDS = [
COMMANDS_AGENT = [
'start',
'stop',
'restart',
'foreground',
'status',
'foreground',
]

COMMANDS_NO_AGENT = [
'info',
'check',
'configcheck',
'jmx',
'flare',
]

COMMANDS = COMMANDS_AGENT + COMMANDS_NO_AGENT

if len(args) < 1:
sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS)))
return 2
Expand All @@ -240,12 +246,13 @@ def main():
from utils.deprecations import deprecate_old_command_line_tools
deprecate_old_command_line_tools()

pid_file = PidFile('dd-agent')
if command in COMMANDS_AGENT:
pid_file = PidFile('dd-agent')

if options.clean:
pid_file.clean()
if options.clean:
pid_file.clean()

agent = Agent(pid_file.get_path(), autorestart)
agent = Agent(pid_file.get_path(), autorestart)

if command in START_COMMANDS:
log.info('Agent version %s' % get_version())
Expand All @@ -266,7 +273,7 @@ def main():
agent.status()

elif 'info' == command:
return agent.info(verbose=options.verbose)
return Agent.info(verbose=options.verbose)

elif 'foreground' == command:
logging.info('Running in foreground')
Expand Down
2 changes: 1 addition & 1 deletion daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def run(self):
"""
raise NotImplementedError


@classmethod
def info(self):
"""
You should override this method when you subclass Daemon. It will be
Expand Down
20 changes: 14 additions & 6 deletions dogstatsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ def run(self):
if self.autorestart:
sys.exit(AgentSupervisor.RESTART_EXIT_STATUS)

@classmethod
def info(self):
logging.getLogger().setLevel(logging.ERROR)
return DogstatsdStatus.print_latest_status()
Expand Down Expand Up @@ -424,16 +425,23 @@ def main(config_path=None):
from utils.deprecations import deprecate_old_command_line_tools
deprecate_old_command_line_tools()

COMMANDS_START_DOGSTATSD = [
'start',
'stop',
'restart',
'status'
]

parser = optparse.OptionParser("%prog [start|stop|restart|status]")
parser.add_option('-u', '--use-local-forwarder', action='store_true',
dest="use_forwarder", default=False)
opts, args = parser.parse_args()

reporter, server, cnf = init(config_path, use_watchdog=True,
use_forwarder=opts.use_forwarder, args=args)
pid_file = PidFile('dogstatsd')
daemon = Dogstatsd(pid_file.get_path(), server, reporter,
cnf.get('autorestart', False))
if not args or args[0] in COMMANDS_START_DOGSTATSD:
reporter, server, cnf = init(config_path, use_watchdog=True, use_forwarder=opts.use_forwarder, args=args)
pid_file = PidFile('dogstatsd')
daemon = Dogstatsd(pid_file.get_path(), server, reporter,
cnf.get('autorestart', False))

# If no args were passed in, run the server in the foreground.
if not args:
Expand All @@ -453,7 +461,7 @@ def main(config_path=None):
elif command == 'status':
daemon.status()
elif command == 'info':
return daemon.info()
return Dogstatsd.info()
else:
sys.stderr.write("Unknown command: %s\n\n" % command)
parser.print_help()
Expand Down

0 comments on commit 3e02b65

Please sign in to comment.