25
24
h = logging.FileHandler(filename)
29
28
def setup_logging(home, config, foreground):
30
29
# i hate that stupid logging config format, so just set up logging here.
32
31
log_folder = os.path.join(home, 'logs')
33
32
if not os.path.exists(log_folder):
34
33
os.mkdir(log_folder)
36
35
f = logging.Formatter('%(levelname)-.3s [%(asctime)s.%(msecs)03d] %(name)s: %(message)s',
38
37
debug_log = make_handler(config, os.path.join(log_folder, 'debug.log'))
66
65
options, args = parser.parse_args()
68
67
parser.error('No filename arguments are used, only options.')
71
70
home = os.path.realpath(os.path.dirname(__file__))
72
71
pidfile = os.path.join(home, 'loggerhead.pid')
75
74
if daemon.is_running(pidfile):
77
76
sys.stderr.write('Did not find loggerhead running in %r; restarting...\n' % (pidfile,))
79
78
# read loggerhead config
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)
85
84
sys.path.insert(0, extra_path)
87
turbogears.update_config(configfile="dev.cfg", modulename="loggerhead.config")
89
potential_overrides = [ ('server.socket_port', int), ('server.webpath', str), ('server.thread_pool', int) ]
86
#turbogears.update_config(configfile="dev.cfg", modulename="loggerhead.config")
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)
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 })
101
#turbogears.config.update({ key: value })
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')
102
109
daemon.daemonize(pidfile, home)
104
111
setup_logging(home, config, foreground=options.foreground)
106
113
log = logging.getLogger('loggerhead')
107
114
log.info('Starting up...')
109
from loggerhead.controllers import Root
113
# re-index every 6 hours
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)
119
app = make_middleware(app)
120
app = make_filter(app, None)
123
if not webpath.endswith('/'):
125
def app(environ, start_response, app=app):
126
environ['SCRIPT_NAME'] = webpath
127
return app(environ, start_response)
119
turbogears.start_server(Root)
131
app, host=server_host, port=server_port,
132
threadpool_workers=nworkers)
121
134
log.info('Shutdown.')