33 # XXX unix specific |
33 # XXX unix specific |
34 # XXX factorize w/ code in cw.server.server and cw.server.serverctl |
34 # XXX factorize w/ code in cw.server.server and cw.server.serverctl |
35 # (start-repository command) |
35 # (start-repository command) |
36 # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16 |
36 # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16 |
37 if os.fork(): # launch child and... |
37 if os.fork(): # launch child and... |
38 os._exit(0) |
38 return 1 |
39 os.setsid() |
39 os.setsid() |
40 if os.fork(): # launch child and... |
40 if os.fork(): # launch child again. |
41 os._exit(0) # kill off parent again. |
41 return 1 |
42 # move to the root to avoit mount pb |
42 # move to the root to avoit mount pb |
43 os.chdir('/') |
43 os.chdir('/') |
44 # set paranoid umask |
44 # set paranoid umask |
45 os.umask(077) |
45 os.umask(077) |
46 null = os.open('/dev/null', os.O_RDWR) |
46 null = os.open('/dev/null', os.O_RDWR) |
95 |
95 |
96 class CubicWebRootResource(resource.PostableResource): |
96 class CubicWebRootResource(resource.PostableResource): |
97 addSlash = False |
97 addSlash = False |
98 |
98 |
99 def __init__(self, config, debug=None): |
99 def __init__(self, config, debug=None): |
100 self.appli = CubicWebPublisher(config, debug=debug) |
|
101 self.debugmode = debug |
100 self.debugmode = debug |
102 self.config = config |
101 self.config = config |
103 self.base_url = config['base-url'] or config.default_base_url() |
102 self.base_url = config['base-url'] or config.default_base_url() |
104 self.versioned_datadir = 'data%s' % config.instance_md5_version() |
|
105 assert self.base_url[-1] == '/' |
103 assert self.base_url[-1] == '/' |
106 self.https_url = config['https-url'] |
104 self.https_url = config['https-url'] |
107 assert not self.https_url or self.https_url[-1] == '/' |
105 assert not self.https_url or self.https_url[-1] == '/' |
|
106 |
|
107 def init_publisher(self): |
|
108 config = self.config |
|
109 self.appli = CubicWebPublisher(config, debug=self.debugmode) |
|
110 self.versioned_datadir = 'data%s' % config.instance_md5_version() |
108 # when we have an in-memory repository, clean unused sessions every XX |
111 # when we have an in-memory repository, clean unused sessions every XX |
109 # seconds and properly shutdown the server |
112 # seconds and properly shutdown the server |
110 if config.repo_method == 'inmemory': |
113 if config.repo_method == 'inmemory': |
111 reactor.addSystemEventTrigger('before', 'shutdown', |
114 reactor.addSystemEventTrigger('before', 'shutdown', |
112 self.shutdown_event) |
115 self.shutdown_event) |
120 self.pyro_listen_timeout = 0.02 |
123 self.pyro_listen_timeout = 0.02 |
121 self.appli.repo.looping_task(1, self.pyro_loop_event) |
124 self.appli.repo.looping_task(1, self.pyro_loop_event) |
122 self.appli.repo.start_looping_tasks() |
125 self.appli.repo.start_looping_tasks() |
123 self.set_url_rewriter() |
126 self.set_url_rewriter() |
124 CW_EVENT_MANAGER.bind('after-registry-reload', self.set_url_rewriter) |
127 CW_EVENT_MANAGER.bind('after-registry-reload', self.set_url_rewriter) |
|
128 |
|
129 def start_service(self): |
|
130 config = self.config |
125 interval = min(config['cleanup-session-time'] or 120, |
131 interval = min(config['cleanup-session-time'] or 120, |
126 config['cleanup-anonymous-session-time'] or 720) / 2. |
132 config['cleanup-anonymous-session-time'] or 720) / 2. |
127 start_task(interval, self.appli.session_handler.clean_sessions) |
133 start_task(interval, self.appli.session_handler.clean_sessions) |
128 |
134 |
129 def set_url_rewriter(self): |
135 def set_url_rewriter(self): |
373 root_resource = CubicWebRootResource(config, debug) |
379 root_resource = CubicWebRootResource(config, debug) |
374 website = server.Site(root_resource) |
380 website = server.Site(root_resource) |
375 # serve it via standard HTTP on port set in the configuration |
381 # serve it via standard HTTP on port set in the configuration |
376 port = config['port'] or 8080 |
382 port = config['port'] or 8080 |
377 reactor.listenTCP(port, channel.HTTPFactory(website)) |
383 reactor.listenTCP(port, channel.HTTPFactory(website)) |
378 baseurl = config['base-url'] or config.default_base_url() |
|
379 logger = getLogger('cubicweb.twisted') |
384 logger = getLogger('cubicweb.twisted') |
380 logger.info('instance started on %s', baseurl) |
385 logger.info('instance started on %s', root_resource.base_url) |
381 if not debug: |
386 if not debug: |
382 print 'instance starting in the background' |
387 print 'instance starting in the background' |
383 daemonize() |
388 if daemonize(): |
|
389 return # child process |
384 if config['pid-file']: |
390 if config['pid-file']: |
385 # ensure the directory where the pid-file should be set exists (for |
391 # ensure the directory where the pid-file should be set exists (for |
386 # instance /var/run/cubicweb may be deleted on computer restart) |
392 # instance /var/run/cubicweb may be deleted on computer restart) |
387 piddir = os.path.dirname(config['pid-file']) |
393 piddir = os.path.dirname(config['pid-file']) |
388 if not os.path.exists(piddir): |
394 if not os.path.exists(piddir): |
389 os.makedirs(piddir) |
395 os.makedirs(piddir) |
390 file(config['pid-file'], 'w').write(str(os.getpid())) |
396 file(config['pid-file'], 'w').write(str(os.getpid())) |
|
397 root_resource.init_publisher() # before changing uid |
391 if config['uid'] is not None: |
398 if config['uid'] is not None: |
392 try: |
399 try: |
393 uid = int(config['uid']) |
400 uid = int(config['uid']) |
394 except ValueError: |
401 except ValueError: |
395 from pwd import getpwnam |
402 from pwd import getpwnam |
396 uid = getpwnam(config['uid']).pw_uid |
403 uid = getpwnam(config['uid']).pw_uid |
397 os.setuid(uid) |
404 os.setuid(uid) |
|
405 root_resource.start_service() |
398 if config['profile']: |
406 if config['profile']: |
399 prof = hotshot.Profile(config['profile']) |
407 prof = hotshot.Profile(config['profile']) |
400 prof.runcall(reactor.run) |
408 prof.runcall(reactor.run) |
401 else: |
409 else: |
402 reactor.run() |
410 reactor.run() |