Skip to content

Commit

Permalink
multiprocessing logging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaeon committed Sep 3, 2014
1 parent 1b6846d commit e2e763e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 140 deletions.
79 changes: 31 additions & 48 deletions src/vpoller-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -30,74 +30,60 @@ between a pool of ZeroMQ workers.
"""

import os
import json
import logging
import multiprocessing

from sys import exit

from vpoller.client import VPollerClient
from vpoller.proxy import VPollerProxy
from vpoller.proxy import VPollerProxyManager
from docopt import docopt

def start(pidfile, config, daemon):
def start(config):
"""
Start the VPoller Proxy daemon
Args:
pidfile (string): Location to the daemon's pidfile
config (string): Configuration file of the VPoller Proxy
daemon (bool): If True daemonize the VPoller Proxy
config (string): Path to the vPoller configuration file
"""
proxy = VPollerProxy(pidfile)

if daemon:
# Run as daemon
proxy.start(config)
else:
# Run in the foreground
proxy.run(config)
manager = VPollerProxyManager(config_file=config)
manager.start()

def stop(endpoint):
"""
Stops the VPoller Proxy daemon
Stops the VPoller Proxy
Args:
endpoint (string): The endpoint we send the shutdown message to
"""
# The message we send to initiate the shutdown sequence
msg = { "method": "proxy.shutdown" }

# Send out our message
client = VPollerClient(endpoint=endpoint, timeout=1000, retries=3)
result = client.run(msg)
result = client.run({'method': 'shutdown'})

return result

def status(endpoint):
"""
Get status information from the VPoller Proxy daemon
Get status information from the VPoller Proxy
Args:
endpoint (string): The endpoint we send the status message to
"""
# The message we send to get status information
msg = { "method": "proxy.status" }

# Send out our message
client = VPollerClient(endpoint=endpoint, timeout=1000, retries=3)
result = client.run(msg)
result = client.run({'method': 'status'})

return result

def main():

usage="""
Usage: vpoller-proxy [-d] [-D] [-p <pidfile>] [-f <config-file>] [-o <logfile>] start
vpoller-proxy [-D] -e <endpoint> stop
vpoller-proxy [-D] -e <endpoint> status
Usage: vpoller-proxy [-d] [-f <config>] start
vpoller-proxy [-d] -e <endpoint> stop
vpoller-proxy [-d] -e <endpoint> status
vpoller-proxy --help
vpoller-proxy --version
Expand All @@ -109,38 +95,35 @@ Arguments:
Options:
-h, --help Display this usage info
-v, --version Display version and exit
-d, --daemon Start as a daemon, otherwise
run in the foreground
-D, --debug Debug mode, be more verbose
-p <pidfile>, --pidfile <pidfile> Specify pidfile file to use
[default: /var/run/vpoller/vpoller-proxy.pid]
-f <config-file>, --config <config-file> Specify config file to use
-d, --debug Debug mode, be more verbose
-f <config>, --file <config> Specify config file to use
[default: /etc/vpoller/vpoller.conf]
-e <endpoint>, --endpoint <endpoint> Specify the endpoint we connect to
-o <logfile>, --output <logfile> Specify the logfile to use
[default: /var/log/vpoller/vpoller-proxy.log]
"""

args = docopt(usage, version='0.2.9')

level = logging.DEBUG if args['--debug'] else logging.INFO
if not os.path.exists(args['--file']):
raise SystemExit, 'Configuration file %s does not exist'

if args['--debug']:
level = logging.DEBUG
else:
level = logging.INFO

logging.basicConfig(
filename=args['--output'],
format='%(asctime)s - %(levelname)s - vpoller-proxy[%(process)s]: %(message)s',
level=level
)
logger = multiprocessing.log_to_stderr()
logger.setLevel(level)

result = None
rc = 0
if args["start"]:
start(args["--pidfile"], args["--config"], args["--daemon"])
elif args["stop"]:
result = stop(args["--endpoint"])

if args['start']:
start(args['--file'])
elif args['stop']:
result = stop(args['--endpoint'])
elif args["status"]:
result = status(args["--endpoint"])
result = status(args['--endpoint'])

if result:
rc = result['success']
Expand Down
33 changes: 13 additions & 20 deletions src/vpoller-worker
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ from vpoller.client import VPollerClient
from vpoller.worker import VPollerWorkerManager
from docopt import docopt

def start(config, workers):
def start(config, concurrency):
"""
Start the vPoller Worker
Args:
config (string): Path to the vPoller configuration file
workers (int): Number of Worker processes to start
config (str): Path to the vPoller configuration file
concurrency (int): Number of Worker processes to start
"""
manager = VPollerWorkerManager(
config_file=config,
num_workers=int(workers)
num_workers=concurrency
)
manager.start()

Expand All @@ -67,12 +67,8 @@ def stop(endpoint):
endpoint (string): The endpoint we send the shutdown message to
"""
# The message we send to initiate the shutdown sequence
msg = { "method": "shutdown" }

# Send out our message
client = VPollerClient(endpoint=endpoint, timeout=1000, retries=3)
result = client.run(msg)
result = client.run({'method': 'shutdown'})

return result

Expand All @@ -84,12 +80,8 @@ def status(endpoint):
endpoint (string): The endpoint we send the status request to
"""
# The message we send to get status information
msg = { "method": "status" }

# Send out our message
client = VPollerClient(endpoint=endpoint, timeout=1000, retries=3)
result = client.run(msg)
result = client.run({'method': 'status'})

return result

Expand Down Expand Up @@ -132,13 +124,14 @@ Options:
logger = multiprocessing.log_to_stderr()
logger.setLevel(level)

result = None
rc = 0

if args['start']:
if not args['--concurrency'] or args['--concurrency'] <= 0:
concurrency = multiprocessing.cpu_count()
else:
concurrency = args['--concurrency']

start(config=args['--file'], workers=concurrency)
start(
config=args['--file'],
concurrency=args['--concurrency']
)
elif args['stop']:
result = stop(args['--endpoint'])
elif args['status']:
Expand Down
Loading

0 comments on commit e2e763e

Please sign in to comment.