equal
deleted
inserted
replaced
14 from datetime import date, timedelta |
14 from datetime import date, timedelta |
15 from urlparse import urlsplit, urlunsplit |
15 from urlparse import urlsplit, urlunsplit |
16 import hotshot |
16 import hotshot |
17 |
17 |
18 from twisted.application import strports |
18 from twisted.application import strports |
19 try: |
|
20 from twisted.scripts._twistd_unix import daemonize |
|
21 except ImportError: |
|
22 def daemonize(): |
|
23 raise NotImplementedError('not yet for win32') |
|
24 |
|
25 from twisted.internet import reactor, task, threads |
19 from twisted.internet import reactor, task, threads |
26 from twisted.internet.defer import maybeDeferred |
20 from twisted.internet.defer import maybeDeferred |
27 from twisted.web2 import channel, http, server, iweb |
21 from twisted.web2 import channel, http, server, iweb |
28 from twisted.web2 import static, resource, responsecode |
22 from twisted.web2 import static, resource, responsecode |
29 |
23 |
33 ExplicitLogin) |
27 ExplicitLogin) |
34 from cubicweb.web.application import CubicWebPublisher |
28 from cubicweb.web.application import CubicWebPublisher |
35 |
29 |
36 from cubicweb.etwist.request import CubicWebTwistedRequestAdapter |
30 from cubicweb.etwist.request import CubicWebTwistedRequestAdapter |
37 |
31 |
|
32 def daemonize(uid): |
|
33 # XXX unix specific |
|
34 # XXX factorize w/ code in cw.server.server and cw.server.serverctl |
|
35 # (start-repository command) |
|
36 if uid is not None: |
|
37 try: |
|
38 uid = int(uid) |
|
39 except ValueError: |
|
40 from pwd import getpwnam |
|
41 uid = getpwnam(uid).pw_uid |
|
42 os.setuid(uid) |
|
43 # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16 |
|
44 if os.fork(): # launch child and... |
|
45 return -1 |
|
46 os.setsid() |
|
47 if os.fork(): # launch child and... |
|
48 os._exit(0) # kill off parent again. |
|
49 # move to the root to avoit mount pb |
|
50 os.chdir('/') |
|
51 # set paranoid umask |
|
52 os.umask(077) |
|
53 null = os.open('/dev/null', os.O_RDWR) |
|
54 for i in range(3): |
|
55 try: |
|
56 os.dup2(null, i) |
|
57 except OSError, e: |
|
58 if e.errno != errno.EBADF: |
|
59 raise |
|
60 os.close(null) |
|
61 return None |
38 |
62 |
39 def start_task(interval, func): |
63 def start_task(interval, func): |
40 lc = task.LoopingCall(func) |
64 lc = task.LoopingCall(func) |
41 lc.start(interval) |
65 lc.start(interval) |
42 |
66 |
371 reactor.listenTCP(port, channel.HTTPFactory(website)) |
395 reactor.listenTCP(port, channel.HTTPFactory(website)) |
372 baseurl = config['base-url'] or config.default_base_url() |
396 baseurl = config['base-url'] or config.default_base_url() |
373 logger = getLogger('cubicweb.twisted') |
397 logger = getLogger('cubicweb.twisted') |
374 logger.info('instance started on %s', baseurl) |
398 logger.info('instance started on %s', baseurl) |
375 if not debug: |
399 if not debug: |
376 daemonize() |
400 if daemonize(config['uid']): |
|
401 # child process |
|
402 return |
377 if config['pid-file']: |
403 if config['pid-file']: |
378 # ensure the directory where the pid-file should be set exists (for |
404 # ensure the directory where the pid-file should be set exists (for |
379 # instance /var/run/cubicweb may be deleted on computer restart) |
405 # instance /var/run/cubicweb may be deleted on computer restart) |
380 piddir = os.path.dirname(config['pid-file']) |
406 piddir = os.path.dirname(config['pid-file']) |
381 if not os.path.exists(piddir): |
407 if not os.path.exists(piddir): |