* * * stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 May 2010 20:45:21 +0200
branchstable
changeset 5553 646737f80c88
parent 5552 11175c77be9c
child 5554 9b7bdbfee68b
* * * [server] move daemonize code to lgc.daemon. Fix on the way pb w/closed standard output on repository config
__pkginfo__.py
debian/control
etwist/server.py
server/server.py
server/serverctl.py
--- a/__pkginfo__.py	Thu May 20 10:55:33 2010 +0200
+++ b/__pkginfo__.py	Thu May 20 20:45:21 2010 +0200
@@ -40,7 +40,7 @@
 ]
 
 __depends__ = {
-    'logilab-common': '>= 0.50.0',
+    'logilab-common': '>= 0.50.2',
     'logilab-mtconverter': '>= 0.6.0',
     'rql': '>= 0.26.0',
     'yams': '>= 0.28.1',
--- a/debian/control	Thu May 20 10:55:33 2010 +0200
+++ b/debian/control	Thu May 20 20:45:21 2010 +0200
@@ -97,7 +97,7 @@
 Package: cubicweb-common
 Architecture: all
 XB-Python-Version: ${python:Versions}
-Depends: ${python:Depends}, graphviz, gettext, python-logilab-mtconverter (>= 0.6.0), python-logilab-common (>= 0.50.0), python-yams (>= 0.29.0), python-rql (>= 0.26.0), python-lxml
+Depends: ${python:Depends}, graphviz, gettext, python-logilab-mtconverter (>= 0.6.0), python-logilab-common (>= 0.50.2), python-yams (>= 0.29.0), python-rql (>= 0.26.0), python-lxml
 Recommends: python-simpletal (>= 4.0), python-crypto
 Conflicts: cubicweb-core
 Replaces: cubicweb-core
--- a/etwist/server.py	Thu May 20 10:55:33 2010 +0200
+++ b/etwist/server.py	Thu May 20 20:45:21 2010 +0200
@@ -41,6 +41,7 @@
 from cubicweb.web import dumps
 
 from logilab.common.decorators import monkeypatch
+from logilab.common.daemon import daemonize
 
 from cubicweb import AuthenticationError, ConfigurationError, CW_EVENT_MANAGER
 from cubicweb.web import Redirect, DirectResponse, StatusResponse, LogOut
@@ -49,30 +50,6 @@
 from cubicweb.etwist.request import CubicWebTwistedRequestAdapter
 from cubicweb.etwist.http import HTTPResponse
 
-def daemonize():
-    # XXX unix specific
-    # XXX factorize w/ code in cw.server.server and cw.server.serverctl
-    # (start-repository command)
-    # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
-    if os.fork():   # launch child and...
-        return 1
-    os.setsid()
-    if os.fork():   # launch child again.
-        return 1
-    # move to the root to avoit mount pb
-    os.chdir('/')
-    # set paranoid umask
-    os.umask(077)
-    null = os.open('/dev/null', os.O_RDWR)
-    for i in range(3):
-        try:
-            os.dup2(null, i)
-        except OSError, e:
-            if e.errno != errno.EBADF:
-                raise
-    os.close(null)
-    return None
-
 def start_task(interval, func):
     lc = task.LoopingCall(func)
     # wait until interval has expired to actually start the task, else we have
@@ -418,15 +395,8 @@
             raise ConfigurationError("Under windows, you must use the service management "
                                      "commands (e.g : 'net start my_instance)'")
         print 'instance starting in the background'
-        if daemonize():
+        if daemonize(config['pid-file']):
             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)
-            piddir = os.path.dirname(config['pid-file'])
-            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:
--- a/server/server.py	Thu May 20 10:55:33 2010 +0200
+++ b/server/server.py	Thu May 20 20:45:21 2010 +0200
@@ -15,9 +15,8 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-"""Pyro RQL server
+"""Pyro RQL server"""
 
-"""
 __docformat__ = "restructuredtext en"
 
 import os
@@ -26,6 +25,8 @@
 import warnings
 from time import localtime, mktime
 
+from logilab.common.daemon import daemonize
+
 from cubicweb.cwconfig import CubicWebConfiguration
 from cubicweb.server.repository import Repository
 
@@ -83,7 +84,6 @@
         self.quiting = None
         # event queue
         self.events = []
-        # start repository looping tasks
 
     def add_event(self, event):
         """add an event to the loop"""
@@ -103,6 +103,7 @@
 
     def run(self, req_timeout=5.0):
         """enter the service loop"""
+        # start repository looping tasks
         self.repo.start_looping_tasks()
         while self.quiting is None:
             try:
@@ -130,34 +131,6 @@
         signal.signal(signal.SIGINT, lambda x, y, s=self: s.quit())
         signal.signal(signal.SIGTERM, lambda x, y, s=self: s.quit())
 
-    def daemonize(self, pid_file=None):
-        """daemonize the process"""
-        # fork so the parent can exist
-        if (os.fork()):
-            return -1
-        # deconnect from tty and create a new session
-        os.setsid()
-        # fork again so the parent, (the session group leader), can exit.
-        # as a non-session group leader, we can never regain a controlling
-        # terminal.
-        if (os.fork()):
-            return -1
-        # move to the root to avoit mount pb
-        os.chdir('/')
-        # set paranoid umask
-        os.umask(077)
-        if pid_file is not None:
-            # write pid in a file
-            f = open(pid_file, 'w')
-            f.write(str(os.getpid()))
-            f.close()
-        # filter warnings
-        warnings.filterwarnings('ignore')
-        # close standard descriptors
-        sys.stdin.close()
-        sys.stdout.close()
-        sys.stderr.close()
-
 from logging import getLogger
 from cubicweb import set_log_methods
 LOGGER = getLogger('cubicweb.reposerver')
--- a/server/serverctl.py	Thu May 20 10:55:33 2010 +0200
+++ b/server/serverctl.py	Thu May 20 20:45:21 2010 +0200
@@ -525,6 +525,7 @@
         )
 
     def run(self, args):
+        from logilab.common.daemon import daemonize
         from cubicweb.server.server import RepositoryServer
         appid = pop_arg(args, msg='No instance specified !')
         config = ServerConfiguration.config_for(appid)
@@ -544,7 +545,7 @@
         piddir = os.path.dirname(pidfile)
         if not os.path.exists(piddir):
             os.makedirs(piddir)
-        if not debug and server.daemonize(pidfile) == -1:
+        if not debug and daemonize(pidfile) == -1:
             return
         uid = config['uid']
         if uid is not None: