~loggerhead-team/loggerhead/trunk-rich

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python2.4

import pkg_resources
pkg_resources.require("TurboGears")

import os

import turbogears
import cherrypy
cherrypy.lowercase_api = True

import sys

home = os.path.realpath(os.path.dirname(__file__))
pidfile = os.path.join(home, 'loggerhead.pid')

# read loggerhead config

from configobj import ConfigObj
config = sys._loggerhead_config = ConfigObj(os.path.join(home, 'loggerhead.conf'), encoding='utf-8')
extra_path = config.get('bzrpath', None)
if extra_path:
    sys.path.insert(0, extra_path)

turbogears.update_config(configfile="dev.cfg", modulename="loggerhead.config")

potential_overrides = [ ('server.socket_port', int), ('server.webpath', str), ('server.thread_pool', int) ]
for key, keytype in potential_overrides:
    value = config.get(key, None)
    if value is not None:
        value = keytype(value)
        turbogears.config.update({ key: value })


sys.stderr.write('\n')
sys.stderr.write('Launching loggerhead into the background.\n')
sys.stderr.write('PID file: %s\n' % (pidfile,))
sys.stderr.write('\n')

from loggerhead.daemon import daemonize
daemonize(pidfile, home)

# i hate that stupid logging config format, so just set up logging here.
import logging

log_folder = os.path.join(home, 'logs')
if not os.path.exists(log_folder):
    os.mkdir(log_folder)

f = logging.Formatter('%(levelname)-.3s [%(asctime)s.%(msecs)03d] %(name)s: %(message)s',
                      '%Y%m%d-%H:%M:%S')
debug_log = logging.FileHandler(os.path.join(log_folder, 'debug.log'))
debug_log.setLevel(logging.DEBUG)
debug_log.setFormatter(f)
f = logging.Formatter('[%(asctime)s.%(msecs)03d] %(message)s',
                      '%Y%m%d-%H:%M:%S')
access_log = logging.FileHandler(os.path.join(log_folder, 'access.log'))
access_log.setLevel(logging.INFO)
access_log.setFormatter(f)

logging.getLogger('').addHandler(debug_log)
logging.getLogger('turbogears.access').addHandler(access_log)
logging.getLogger('turbogears.controllers').setLevel(logging.INFO)

log = logging.getLogger('loggerhead')
log.info('Starting up...')

from loggerhead.controllers import Root

# re-index every 6 hours
index_freq = 6 * 3600
turbogears.scheduler.add_interval_task(initialdelay=1, interval=index_freq, action=Root._check_rebuild)

try:
    turbogears.start_server(Root)
finally:
    log.info('Shutdown.')
    os.remove(pidfile)