~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to start-loggerhead.py

  • Committer: Michael Hudson
  • Date: 2008-06-20 02:20:05 UTC
  • mfrom: (163 trunk)
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080620022005-a7l387c2u2o43w2b
merge trunk, including final version of zpt.cleaner_urls

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python2.4
2
 
 
3
 
import pkg_resources
4
 
pkg_resources.require("TurboGears")
 
1
#!/usr/bin/env python
5
2
 
6
3
import logging
 
4
import logging.handlers
7
5
import os
8
6
import sys
9
7
from optparse import OptionParser
10
8
 
11
 
import turbogears
12
 
import cherrypy
13
 
cherrypy.lowercase_api = True
 
9
from loggerhead.apps.config import Root
 
10
from paste import httpserver
 
11
from paste.httpexceptions import make_middleware
 
12
from paste.translogger import make_filter
14
13
 
15
14
from loggerhead import daemon, release
16
15
 
25
24
        h = logging.FileHandler(filename)
26
25
    return h
27
26
 
28
 
        
 
27
 
29
28
def setup_logging(home, config, foreground):
30
29
    # i hate that stupid logging config format, so just set up logging here.
31
30
 
32
31
    log_folder = os.path.join(home, 'logs')
33
32
    if not os.path.exists(log_folder):
34
33
        os.mkdir(log_folder)
35
 
    
 
34
 
36
35
    f = logging.Formatter('%(levelname)-.3s [%(asctime)s.%(msecs)03d] %(name)s: %(message)s',
37
36
                          '%Y%m%d-%H:%M:%S')
38
37
    debug_log = make_handler(config, os.path.join(log_folder, 'debug.log'))
47
46
    access_log = make_handler(config, os.path.join(log_folder, 'access.log'))
48
47
    access_log.setLevel(logging.INFO)
49
48
    access_log.setFormatter(f)
50
 
    
 
49
 
51
50
    logging.getLogger('').addHandler(debug_log)
52
51
    logging.getLogger('turbogears.access').addHandler(access_log)
53
52
    logging.getLogger('turbogears.controllers').setLevel(logging.INFO)
54
 
    
 
53
 
55
54
    if foreground:
56
55
        logging.getLogger('').addHandler(stdout_log)
57
 
    
 
56
 
58
57
 
59
58
 
60
59
def main():
66
65
    options, args = parser.parse_args()
67
66
    if len(args) > 0:
68
67
        parser.error('No filename arguments are used, only options.')
69
 
        
70
 
        
 
68
 
 
69
 
71
70
    home = os.path.realpath(os.path.dirname(__file__))
72
71
    pidfile = os.path.join(home, 'loggerhead.pid')
73
 
    
 
72
 
74
73
    if options.check:
75
74
        if daemon.is_running(pidfile):
76
75
            sys.exit(0)
77
76
        sys.stderr.write('Did not find loggerhead running in %r; restarting...\n' % (pidfile,))
78
 
    
 
77
 
79
78
    # read loggerhead config
80
 
    
 
79
 
81
80
    from configobj import ConfigObj
82
81
    config = ConfigObj(os.path.join(home, 'loggerhead.conf'), encoding='utf-8')
83
82
    extra_path = config.get('bzrpath', None)
84
83
    if extra_path:
85
84
        sys.path.insert(0, extra_path)
86
 
    
87
 
    turbogears.update_config(configfile="dev.cfg", modulename="loggerhead.config")
88
 
    
89
 
    potential_overrides = [ ('server.socket_port', int), ('server.webpath', str), ('server.thread_pool', int) ]
 
85
 
 
86
    #turbogears.update_config(configfile="dev.cfg", modulename="loggerhead.config")
 
87
 
 
88
    potential_overrides = [ ('server.socket_port', int),
 
89
                            ('server.webpath', str),
 
90
                            ('server.thread_pool', int),
 
91
                            ('server.socket_host' ,str) ]
 
92
    server_port = int(config.get('server.socket_port', 8080))
 
93
    nworkers = int(config.get('server.thread_pool', 10))
 
94
    server_host = config.get('server.socket_host', '0.0.0.0')
 
95
    webpath = config.get('server.webpath', None)
 
96
 
90
97
    for key, keytype in potential_overrides:
91
98
        value = config.get(key, None)
92
99
        if value is not None:
93
100
            value = keytype(value)
94
 
            turbogears.config.update({ key: value })
95
 
    
 
101
            #turbogears.config.update({ key: value })
 
102
 
96
103
    if not options.foreground:
97
104
        sys.stderr.write('\n')
98
105
        sys.stderr.write('Launching loggerhead into the background.\n')
99
106
        sys.stderr.write('PID file: %s\n' % (pidfile,))
100
107
        sys.stderr.write('\n')
101
 
    
 
108
 
102
109
        daemon.daemonize(pidfile, home)
103
110
 
104
111
    setup_logging(home, config, foreground=options.foreground)
105
 
        
 
112
 
106
113
    log = logging.getLogger('loggerhead')
107
114
    log.info('Starting up...')
108
 
    
109
 
    from loggerhead.controllers import Root
110
 
    
111
 
    Root = Root(config)
112
 
    
113
 
    # re-index every 6 hours
114
 
    
115
 
    index_freq = config.get('cache_rebuild_frequency', 6 * 3600)
116
 
    turbogears.scheduler.add_interval_task(initialdelay=1, interval=index_freq, action=Root._check_rebuild)
117
 
    
 
115
 
 
116
    app = Root(config)
 
117
 
 
118
    app = app
 
119
    app = make_middleware(app)
 
120
    app = make_filter(app, None)
 
121
 
 
122
    if webpath:
 
123
        if not webpath.endswith('/'):
 
124
            webpath += '/'
 
125
        def app(environ, start_response, app=app):
 
126
            environ['SCRIPT_NAME'] = webpath
 
127
            return app(environ, start_response)
 
128
 
118
129
    try:
119
 
        turbogears.start_server(Root)
 
130
        httpserver.serve(
 
131
            app, host=server_host, port=server_port,
 
132
            threadpool_workers=nworkers)
120
133
    finally:
121
134
        log.info('Shutdown.')
122
135
        try: