fix startup as a daemon stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 07 Oct 2009 19:15:43 +0200
branchstable
changeset 3606 8326aceecb46
parent 3605 1466323ddb9c
child 3607 a7c5e62c7e00
fix startup as a daemon
etwist/server.py
server/repository.py
--- a/etwist/server.py	Wed Oct 07 18:41:17 2009 +0200
+++ b/etwist/server.py	Wed Oct 07 19:15:43 2009 +0200
@@ -35,10 +35,10 @@
     # (start-repository command)
     # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
     if os.fork():   # launch child and...
-        os._exit(0)
+        return 1
     os.setsid()
-    if os.fork():   # launch child and...
-        os._exit(0) # kill off parent again.
+    if os.fork():   # launch child again.
+        return 1
     # move to the root to avoit mount pb
     os.chdir('/')
     # set paranoid umask
@@ -97,14 +97,17 @@
     addSlash = False
 
     def __init__(self, config, debug=None):
-        self.appli = CubicWebPublisher(config, debug=debug)
         self.debugmode = debug
         self.config = config
         self.base_url = config['base-url'] or config.default_base_url()
-        self.versioned_datadir = 'data%s' % config.instance_md5_version()
         assert self.base_url[-1] == '/'
         self.https_url = config['https-url']
         assert not self.https_url or self.https_url[-1] == '/'
+
+    def init_publisher(self):
+        config = self.config
+        self.appli = CubicWebPublisher(config, debug=self.debugmode)
+        self.versioned_datadir = 'data%s' % config.instance_md5_version()
         # when we have an in-memory repository, clean unused sessions every XX
         # seconds and properly shutdown the server
         if config.repo_method == 'inmemory':
@@ -122,6 +125,9 @@
             self.appli.repo.start_looping_tasks()
         self.set_url_rewriter()
         CW_EVENT_MANAGER.bind('after-registry-reload', self.set_url_rewriter)
+
+    def start_service(self):
+        config = self.config
         interval = min(config['cleanup-session-time'] or 120,
                        config['cleanup-anonymous-session-time'] or 720) / 2.
         start_task(interval, self.appli.session_handler.clean_sessions)
@@ -375,12 +381,12 @@
     # serve it via standard HTTP on port set in the configuration
     port = config['port'] or 8080
     reactor.listenTCP(port, channel.HTTPFactory(website))
-    baseurl = config['base-url'] or config.default_base_url()
     logger = getLogger('cubicweb.twisted')
-    logger.info('instance started on %s', baseurl)
+    logger.info('instance started on %s', root_resource.base_url)
     if not debug:
         print 'instance starting in the background'
-        daemonize()
+        if daemonize():
+            return # child process
         if config['pid-file']:
             # ensure the directory where the pid-file should be set exists (for
             # instance /var/run/cubicweb may be deleted on computer restart) 
@@ -388,6 +394,7 @@
             if not os.path.exists(piddir):
                 os.makedirs(piddir)
             file(config['pid-file'], 'w').write(str(os.getpid()))
+    root_resource.init_publisher() # before changing uid
     if config['uid'] is not None:
         try:
             uid = int(config['uid'])
@@ -395,6 +402,7 @@
             from pwd import getpwnam
             uid = getpwnam(config['uid']).pw_uid
         os.setuid(uid)
+    root_resource.start_service()
     if config['profile']:
         prof = hotshot.Profile(config['profile'])
         prof.runcall(reactor.run)
--- a/server/repository.py	Wed Oct 07 18:41:17 2009 +0200
+++ b/server/repository.py	Wed Oct 07 19:15:43 2009 +0200
@@ -1192,7 +1192,9 @@
 
     def pyro_register(self, host=''):
         """register the repository as a pyro object"""
-        from logilab.common.pyro_ext import register_object
+        import tempfile
+        from logilab.common.pyro_ext import register_object, config
+        config.PYRO_STORAGE = tempfile.gettempdir() # XXX until lgc > 0.45.1 is out
         appid = self.config['pyro-instance-id'] or self.config.appid
         daemon = register_object(self, appid, self.config['pyro-ns-group'],
                                  self.config['pyro-host'],