author | David Douard <david.douard@logilab.fr> |
Tue, 15 Dec 2015 12:14:47 +0100 | |
changeset 11672 | 2018cdf2909e |
parent 11664 | 7567e99d6ed5 |
child 11679 | 04b127f7ba4e |
permissions | -rw-r--r-- |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
1 |
""" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
2 |
Provides a 'pyramid' command as a replacement to the 'start' command. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
3 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
4 |
The reloading strategy is heavily inspired by (and partially copied from) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
5 |
the pyramid script 'pserve'. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
6 |
""" |
11663
6fb9d9276880
[ccplugin] print_function
Julien Cristau <julien.cristau@logilab.fr>
parents:
11658
diff
changeset
|
7 |
from __future__ import print_function |
6fb9d9276880
[ccplugin] print_function
Julien Cristau <julien.cristau@logilab.fr>
parents:
11658
diff
changeset
|
8 |
|
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
9 |
import atexit |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
10 |
import errno |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
11 |
import os |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
12 |
import signal |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
13 |
import sys |
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
14 |
import tempfile |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
15 |
import time |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
16 |
import threading |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
17 |
import subprocess |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
18 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
19 |
from cubicweb import BadCommandUsage, ExecutionError |
11652
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
20 |
from cubicweb.__pkginfo__ import numversion as cwversion |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
21 |
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
22 |
from cubicweb.cwctl import CWCTL, InstanceCommand, init_cmdline_log_threshold |
11672
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
23 |
from cubicweb.server import set_debug |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
24 |
|
11644
41bd2b316228
Move the cors middleware initialisation to pyramid-cubicweb to reduce code duplication
Christophe de Vienne <christophe@unlish.com>
parents:
11641
diff
changeset
|
25 |
from pyramid_cubicweb import wsgi_application_from_cwconfig |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
26 |
import waitress |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
27 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
28 |
MAXFD = 1024 |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
29 |
|
11672
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
30 |
DBG_FLAGS = ('RQL', 'SQL', 'REPO', 'HOOKS', 'OPS', 'SEC', 'MORE') |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
31 |
LOG_LEVELS = ('debug', 'info', 'warning', 'error') |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
32 |
|
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
33 |
class PyramidStartHandler(InstanceCommand): |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
34 |
"""Start an interactive pyramid server. |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
35 |
|
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
36 |
This command requires http://hg.logilab.org/review/pyramid_cubicweb/ |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
37 |
|
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
38 |
<instance> |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
39 |
identifier of the instance to configure. |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
40 |
""" |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
41 |
name = 'pyramid' |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
42 |
|
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
43 |
options = ( |
11641
3d4e7be2e3c3
Add a --no-daemon option
Christophe de Vienne <christophe@unlish.com>
parents:
11640
diff
changeset
|
44 |
('no-daemon', |
3d4e7be2e3c3
Add a --no-daemon option
Christophe de Vienne <christophe@unlish.com>
parents:
11640
diff
changeset
|
45 |
{'action': 'store_true', |
3d4e7be2e3c3
Add a --no-daemon option
Christophe de Vienne <christophe@unlish.com>
parents:
11640
diff
changeset
|
46 |
'help': 'Run the server in the foreground.'}), |
11645
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
47 |
('debug-mode', |
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
48 |
{'action': 'store_true', |
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
49 |
'help': 'Activate the repository debug mode (' |
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
50 |
'logs in the console and the debug toolbar).' |
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
51 |
' Implies --no-daemon'}), |
11641
3d4e7be2e3c3
Add a --no-daemon option
Christophe de Vienne <christophe@unlish.com>
parents:
11640
diff
changeset
|
52 |
('debug', |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
53 |
{'short': 'D', 'action': 'store_true', |
11645
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
54 |
'help': 'Equals to "--debug-mode --no-daemon --reload"'}), |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
55 |
('reload', |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
56 |
{'action': 'store_true', |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
57 |
'help': 'Restart the server if any source file is changed'}), |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
58 |
('reload-interval', |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
59 |
{'type': 'int', 'default': 1, |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
60 |
'help': 'Interval, in seconds, between file modifications checks'}), |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
61 |
('loglevel', |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
62 |
{'short': 'l', 'type': 'choice', 'metavar': '<log level>', |
11672
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
63 |
'default': None, 'choices': LOG_LEVELS, |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
64 |
'help': 'debug if -D is set, error otherwise; one of %s' % (LOG_LEVELS,), |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
65 |
}), |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
66 |
('dbglevel', |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
67 |
{'type': 'multiple_choice', 'metavar': '<dbg level>', |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
68 |
'default': None, |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
69 |
'choices': DBG_FLAGS, |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
70 |
'help': ('Set the server debugging flags; you may choose several ' |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
71 |
'values in %s; imply "debug" loglevel' % (DBG_FLAGS,)), |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
72 |
}), |
11648
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
73 |
('profile', |
11652
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
74 |
{'action': 'store_true', |
11648
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
75 |
'default': False, |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
76 |
'help': 'Enable profiling'}), |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
77 |
('profile-output', |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
78 |
{'type': 'string', |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
79 |
'default': None, |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
80 |
'help': 'Profiling output file (default: "program.prof")'}), |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
81 |
('profile-dump-every', |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
82 |
{'type': 'int', |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
83 |
'default': None, |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
84 |
'metavar': 'N', |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
85 |
'help': 'Dump profile stats to ouput every N requests ' |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
86 |
'(default: 100)'}), |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
87 |
) |
11652
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
88 |
if cwversion >= (3, 21, 0): |
11658
a63023a8d457
[ccplugin] fix a bug introduced in e95725d7ce90 (closes #5731783)
David Douard <david.douard@logilab.fr>
parents:
11652
diff
changeset
|
89 |
options = options + ( |
11652
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
90 |
('param', |
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
91 |
{'short': 'p', 'type': 'named', 'metavar': 'key1:value1,key2:value2', |
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
92 |
'default': {}, |
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
93 |
'help': 'override <key> configuration file option with <value>.', |
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
94 |
}), |
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
95 |
) |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
96 |
|
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
97 |
_reloader_environ_key = 'CW_RELOADER_SHOULD_RUN' |
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
98 |
_reloader_filelist_environ_key = 'CW_RELOADER_FILELIST' |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
99 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
100 |
def debug(self, msg): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
101 |
print('DEBUG - %s' % msg) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
102 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
103 |
def info(self, msg): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
104 |
print('INFO - %s' % msg) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
105 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
106 |
def ordered_instances(self): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
107 |
instances = super(PyramidStartHandler, self).ordered_instances() |
11645
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
108 |
if (self['debug-mode'] or self['debug'] or self['reload']) \ |
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
109 |
and len(instances) > 1: |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
110 |
raise BadCommandUsage( |
11645
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
111 |
'--debug-mode, --debug and --reload can be used on a single ' |
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
112 |
'instance only') |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
113 |
return instances |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
114 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
115 |
def quote_first_command_arg(self, arg): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
116 |
""" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
117 |
There's a bug in Windows when running an executable that's |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
118 |
located inside a path with a space in it. This method handles |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
119 |
that case, or on non-Windows systems or an executable with no |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
120 |
spaces, it just leaves well enough alone. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
121 |
""" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
122 |
if (sys.platform != 'win32' or ' ' not in arg): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
123 |
# Problem does not apply: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
124 |
return arg |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
125 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
126 |
import win32api |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
127 |
except ImportError: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
128 |
raise ValueError( |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
129 |
"The executable %r contains a space, and in order to " |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
130 |
"handle this issue you must have the win32api module " |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
131 |
"installed" % arg) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
132 |
arg = win32api.GetShortPathName(arg) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
133 |
return arg |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
134 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
135 |
def _remove_pid_file(self, written_pid, filename): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
136 |
current_pid = os.getpid() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
137 |
if written_pid != current_pid: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
138 |
# A forked process must be exiting, not the process that |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
139 |
# wrote the PID file |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
140 |
return |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
141 |
if not os.path.exists(filename): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
142 |
return |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
143 |
with open(filename) as f: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
144 |
content = f.read().strip() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
145 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
146 |
pid_in_file = int(content) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
147 |
except ValueError: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
148 |
pass |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
149 |
else: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
150 |
if pid_in_file != current_pid: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
151 |
msg = "PID file %s contains %s, not expected PID %s" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
152 |
self.out(msg % (filename, pid_in_file, current_pid)) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
153 |
return |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
154 |
self.info("Removing PID file %s" % filename) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
155 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
156 |
os.unlink(filename) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
157 |
return |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
158 |
except OSError as e: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
159 |
# Record, but don't give traceback |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
160 |
self.out("Cannot remove PID file: (%s)" % e) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
161 |
# well, at least lets not leave the invalid PID around... |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
162 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
163 |
with open(filename, 'w') as f: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
164 |
f.write('') |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
165 |
except OSError as e: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
166 |
self.out('Stale PID left in file: %s (%s)' % (filename, e)) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
167 |
else: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
168 |
self.out('Stale PID removed') |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
169 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
170 |
def record_pid(self, pid_file): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
171 |
pid = os.getpid() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
172 |
self.debug('Writing PID %s to %s' % (pid, pid_file)) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
173 |
with open(pid_file, 'w') as f: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
174 |
f.write(str(pid)) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
175 |
atexit.register( |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
176 |
self._remove_pid_file, pid, pid_file) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
177 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
178 |
def daemonize(self, pid_file): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
179 |
pid = live_pidfile(pid_file) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
180 |
if pid: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
181 |
raise ExecutionError( |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
182 |
"Daemon is already running (PID: %s from PID file %s)" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
183 |
% (pid, pid_file)) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
184 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
185 |
self.debug('Entering daemon mode') |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
186 |
pid = os.fork() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
187 |
if pid: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
188 |
# The forked process also has a handle on resources, so we |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
189 |
# *don't* want proper termination of the process, we just |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
190 |
# want to exit quick (which os._exit() does) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
191 |
os._exit(0) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
192 |
# Make this the session leader |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
193 |
os.setsid() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
194 |
# Fork again for good measure! |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
195 |
pid = os.fork() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
196 |
if pid: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
197 |
os._exit(0) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
198 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
199 |
# @@: Should we set the umask and cwd now? |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
200 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
201 |
import resource # Resource usage information. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
202 |
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
203 |
if (maxfd == resource.RLIM_INFINITY): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
204 |
maxfd = MAXFD |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
205 |
# Iterate through and close all file descriptors. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
206 |
for fd in range(0, maxfd): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
207 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
208 |
os.close(fd) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
209 |
except OSError: # ERROR, fd wasn't open to begin with (ignored) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
210 |
pass |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
211 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
212 |
if (hasattr(os, "devnull")): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
213 |
REDIRECT_TO = os.devnull |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
214 |
else: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
215 |
REDIRECT_TO = "/dev/null" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
216 |
os.open(REDIRECT_TO, os.O_RDWR) # standard input (0) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
217 |
# Duplicate standard input to standard output and standard error. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
218 |
os.dup2(0, 1) # standard output (1) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
219 |
os.dup2(0, 2) # standard error (2) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
220 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
221 |
def restart_with_reloader(self): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
222 |
self.debug('Starting subprocess with file monitor') |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
223 |
|
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
224 |
with tempfile.NamedTemporaryFile(delete=False) as f: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
225 |
filelist_path = f.name |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
226 |
|
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
227 |
while True: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
228 |
args = [self.quote_first_command_arg(sys.executable)] + sys.argv |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
229 |
new_environ = os.environ.copy() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
230 |
new_environ[self._reloader_environ_key] = 'true' |
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
231 |
new_environ[self._reloader_filelist_environ_key] = filelist_path |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
232 |
proc = None |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
233 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
234 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
235 |
proc = subprocess.Popen(args, env=new_environ) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
236 |
exit_code = proc.wait() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
237 |
proc = None |
11663
6fb9d9276880
[ccplugin] print_function
Julien Cristau <julien.cristau@logilab.fr>
parents:
11658
diff
changeset
|
238 |
print("Process exited with", exit_code) |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
239 |
except KeyboardInterrupt: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
240 |
self.info('^C caught in monitor process') |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
241 |
return 1 |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
242 |
finally: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
243 |
if proc is not None: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
244 |
proc.terminate() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
245 |
self.info( |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
246 |
'Waiting for the server to stop. Hit CTRL-C to exit') |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
247 |
exit_code = proc.wait() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
248 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
249 |
if exit_code != 3: |
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
250 |
with open(filelist_path) as f: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
251 |
filelist = [line.strip() for line in f] |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
252 |
if filelist: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
253 |
self.info("Reloading failed. Waiting for a file to change") |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
254 |
mon = Monitor(extra_files=filelist, nomodules=True) |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
255 |
while mon.check_reload(): |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
256 |
time.sleep(1) |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
257 |
else: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
258 |
return exit_code |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
259 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
260 |
self.info('%s %s %s' % ('-' * 20, 'Restarting', '-' * 20)) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
261 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
262 |
def set_needreload(self): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
263 |
self._needreload = True |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
264 |
|
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
265 |
def install_reloader(self, poll_interval, extra_files, filelist_path): |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
266 |
mon = Monitor( |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
267 |
poll_interval=poll_interval, extra_files=extra_files, |
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
268 |
atexit=self.set_needreload, filelist_path=filelist_path) |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
269 |
mon_thread = threading.Thread(target=mon.periodic_reload) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
270 |
mon_thread.daemon = True |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
271 |
mon_thread.start() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
272 |
|
11647
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
273 |
def configfiles(self, cwconfig): |
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
274 |
"""Generate instance configuration files""" |
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
275 |
yield cwconfig.main_config_file() |
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
276 |
for f in ( |
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
277 |
'sources', 'logging.conf', 'pyramid.ini', 'pyramid-debug.ini'): |
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
278 |
f = os.path.join(cwconfig.apphome, f) |
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
279 |
if os.path.exists(f): |
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
280 |
yield f |
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
281 |
|
11639
f38ec5e29de3
Watch for i18n files changes for auto-reload
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11638
diff
changeset
|
282 |
def i18nfiles(self, cwconfig): |
11647
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
283 |
"""Generate instance i18n files""" |
11639
f38ec5e29de3
Watch for i18n files changes for auto-reload
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11638
diff
changeset
|
284 |
i18ndir = os.path.join(cwconfig.apphome, 'i18n') |
f38ec5e29de3
Watch for i18n files changes for auto-reload
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11638
diff
changeset
|
285 |
if os.path.exists(i18ndir): |
f38ec5e29de3
Watch for i18n files changes for auto-reload
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11638
diff
changeset
|
286 |
for lang in cwconfig.available_languages(): |
f38ec5e29de3
Watch for i18n files changes for auto-reload
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11638
diff
changeset
|
287 |
f = os.path.join(i18ndir, lang, 'LC_MESSAGES', 'cubicweb.mo') |
f38ec5e29de3
Watch for i18n files changes for auto-reload
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11638
diff
changeset
|
288 |
if os.path.exists(f): |
f38ec5e29de3
Watch for i18n files changes for auto-reload
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11638
diff
changeset
|
289 |
yield f |
f38ec5e29de3
Watch for i18n files changes for auto-reload
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11638
diff
changeset
|
290 |
|
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
291 |
def pyramid_instance(self, appid): |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
292 |
self._needreload = False |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
293 |
|
11645
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
294 |
debugmode = self['debug-mode'] or self['debug'] |
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
295 |
autoreload = self['reload'] or self['debug'] |
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
296 |
daemonize = not (self['no-daemon'] or debugmode or autoreload) |
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
297 |
|
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
298 |
if autoreload and not os.environ.get(self._reloader_environ_key): |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
299 |
return self.restart_with_reloader() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
300 |
|
11645
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
301 |
cwconfig = cwcfg.config_for(appid, debugmode=debugmode) |
11652
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
302 |
if cwversion >= (3, 21, 0): |
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
303 |
cwconfig.cmdline_options = self.config.param |
11645
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
304 |
if autoreload: |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
305 |
_turn_sigterm_into_systemexit() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
306 |
self.debug('Running reloading file monitor') |
11647
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
307 |
extra_files = [sys.argv[0]] |
7f2cfe9f79cb
Monitor more configuration files
Christophe de Vienne <christophe@unlish.com>
parents:
11645
diff
changeset
|
308 |
extra_files.extend(self.configfiles(cwconfig)) |
11639
f38ec5e29de3
Watch for i18n files changes for auto-reload
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11638
diff
changeset
|
309 |
extra_files.extend(self.i18nfiles(cwconfig)) |
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
310 |
self.install_reloader( |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
311 |
self['reload-interval'], extra_files, |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
312 |
filelist_path=os.environ.get( |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
313 |
self._reloader_filelist_environ_key)) |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
314 |
|
11645
2a949974002d
--debug now activates all debug options
Christophe de Vienne <christophe@unlish.com>
parents:
11644
diff
changeset
|
315 |
if daemonize: |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
316 |
self.daemonize(cwconfig['pid-file']) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
317 |
self.record_pid(cwconfig['pid-file']) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
318 |
|
11672
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
319 |
if self['dbglevel']: |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
320 |
self['loglevel'] = 'debug' |
2018cdf2909e
[cc] add a dbglevel cmdline option to specify the DBG_XXX flags to set
David Douard <david.douard@logilab.fr>
parents:
11664
diff
changeset
|
321 |
set_debug('|'.join('DBG_'+ x.upper() for x in self['dbglevel'])) |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
322 |
init_cmdline_log_threshold(cwconfig, self['loglevel']) |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
323 |
|
11648
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
324 |
app = wsgi_application_from_cwconfig( |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
325 |
cwconfig, profile=self['profile'], |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
326 |
profile_output=self['profile-output'], |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
327 |
profile_dump_every=self['profile-dump-every'] |
9a112017974a
Add profiling options
Christophe de Vienne <christophe@unlish.com>
parents:
11647
diff
changeset
|
328 |
) |
11640
e45e4999dc98
Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents:
11639
diff
changeset
|
329 |
|
11652
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
330 |
host = cwconfig['interface'] |
e95725d7ce90
Allow to override config file options by cmdline arguments (closes #5724484)
David Douard <david.douard@logilab.fr>
parents:
11648
diff
changeset
|
331 |
port = cwconfig['port'] or 8080 |
11664
7567e99d6ed5
[ccplugin] get the cw repo from the wsgi app
Julien Cristau <julien.cristau@logilab.fr>
parents:
11663
diff
changeset
|
332 |
repo = app.application.registry['cubicweb.repository'] |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
333 |
try: |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
334 |
repo.start_looping_tasks() |
11640
e45e4999dc98
Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents:
11639
diff
changeset
|
335 |
waitress.serve(app, host=host, port=port) |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
336 |
finally: |
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
337 |
repo.shutdown() |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
338 |
if self._needreload: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
339 |
return 3 |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
340 |
return 0 |
11633
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
341 |
|
ffe4040cf4a2
Implements the 'pyramid' command.
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
342 |
CWCTL.register(PyramidStartHandler) |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
343 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
344 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
345 |
def live_pidfile(pidfile): # pragma: no cover |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
346 |
"""(pidfile:str) -> int | None |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
347 |
Returns an int found in the named file, if there is one, |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
348 |
and if there is a running process with that process id. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
349 |
Return None if no such process exists. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
350 |
""" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
351 |
pid = read_pidfile(pidfile) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
352 |
if pid: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
353 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
354 |
os.kill(int(pid), 0) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
355 |
return pid |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
356 |
except OSError as e: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
357 |
if e.errno == errno.EPERM: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
358 |
return pid |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
359 |
return None |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
360 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
361 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
362 |
def read_pidfile(filename): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
363 |
if os.path.exists(filename): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
364 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
365 |
with open(filename) as f: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
366 |
content = f.read() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
367 |
return int(content.strip()) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
368 |
except (ValueError, IOError): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
369 |
return None |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
370 |
else: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
371 |
return None |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
372 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
373 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
374 |
def _turn_sigterm_into_systemexit(): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
375 |
""" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
376 |
Attempts to turn a SIGTERM exception into a SystemExit exception. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
377 |
""" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
378 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
379 |
import signal |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
380 |
except ImportError: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
381 |
return |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
382 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
383 |
def handle_term(signo, frame): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
384 |
raise SystemExit |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
385 |
signal.signal(signal.SIGTERM, handle_term) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
386 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
387 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
388 |
class Monitor(object): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
389 |
""" |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
390 |
A file monitor and server stopper. |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
391 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
392 |
It is a simplified version of pyramid pserve.Monitor, with little changes: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
393 |
|
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
394 |
- The constructor takes extra_files, atexit, nomodules and filelist_path |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
395 |
- The process is stopped by auto-kill with signal SIGTERM |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
396 |
""" |
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
397 |
def __init__(self, poll_interval=1, extra_files=[], atexit=None, |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
398 |
nomodules=False, filelist_path=None): |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
399 |
self.module_mtimes = {} |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
400 |
self.keep_running = True |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
401 |
self.poll_interval = poll_interval |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
402 |
self.extra_files = extra_files |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
403 |
self.atexit = atexit |
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
404 |
self.nomodules = nomodules |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
405 |
self.filelist_path = filelist_path |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
406 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
407 |
def _exit(self): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
408 |
if self.atexit: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
409 |
self.atexit() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
410 |
os.kill(os.getpid(), signal.SIGTERM) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
411 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
412 |
def periodic_reload(self): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
413 |
while True: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
414 |
if not self.check_reload(): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
415 |
self._exit() |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
416 |
break |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
417 |
time.sleep(self.poll_interval) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
418 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
419 |
def check_reload(self): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
420 |
filenames = list(self.extra_files) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
421 |
|
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
422 |
if not self.nomodules: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
423 |
for module in list(sys.modules.values()): |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
424 |
try: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
425 |
filename = module.__file__ |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
426 |
except (AttributeError, ImportError): |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
427 |
continue |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
428 |
if filename is not None: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
429 |
filenames.append(filename) |
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
430 |
|
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
431 |
for filename in filenames: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
432 |
try: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
433 |
stat = os.stat(filename) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
434 |
if stat: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
435 |
mtime = stat.st_mtime |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
436 |
else: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
437 |
mtime = 0 |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
438 |
except (OSError, IOError): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
439 |
continue |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
440 |
if filename.endswith('.pyc') and os.path.exists(filename[:-1]): |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
441 |
mtime = max(os.stat(filename[:-1]).st_mtime, mtime) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
442 |
if not filename in self.module_mtimes: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
443 |
self.module_mtimes[filename] = mtime |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
444 |
elif self.module_mtimes[filename] < mtime: |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
445 |
print('%s changed; reloading...' % filename) |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
446 |
return False |
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
447 |
|
11638
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
448 |
if self.filelist_path: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
449 |
with open(self.filelist_path) as f: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
450 |
filelist = set((line.strip() for line in f)) |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
451 |
|
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
452 |
filelist.update(filenames) |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
453 |
|
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
454 |
with open(self.filelist_path, 'w') as f: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
455 |
for filename in filelist: |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
456 |
f.write('%s\n' % filename) |
12de153c0d0e
Auto-reload now survives failed reload
Christophe de Vienne <christophe@unlish.com>
parents:
11637
diff
changeset
|
457 |
|
11637
a9cde6a3394c
Implements auto-reload and daemon mode.
Christophe de Vienne <christophe@unlish.com>
parents:
11633
diff
changeset
|
458 |
return True |