/usr/share/doc/python-cotyledon-doc/html/examples.html is in python-cotyledon-doc 1.6.8-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Examples — cotyledon documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Note about non posix support" href="non-posix-support.html" />
<link rel="prev" title="API" href="api.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="examples">
<h1>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">ΒΆ</a></h1>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Licensed under the Apache License, Version 2.0 (the "License"); you may</span>
<span class="c1"># not use this file except in compliance with the License. You may obtain</span>
<span class="c1"># a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an "AS IS" BASIS, WITHOUT</span>
<span class="c1"># WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the</span>
<span class="c1"># License for the specific language governing permissions and limitations</span>
<span class="c1"># under the License.</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">signal</span>
<span class="kn">import</span> <span class="nn">socket</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">oslo_config</span> <span class="kn">import</span> <span class="n">cfg</span>
<span class="kn">import</span> <span class="nn">cotyledon</span>
<span class="kn">from</span> <span class="nn">cotyledon</span> <span class="kn">import</span> <span class="n">_utils</span>
<span class="kn">from</span> <span class="nn">cotyledon</span> <span class="kn">import</span> <span class="n">oslo_config_glue</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">3</span><span class="p">:</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">socket</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_DGRAM</span><span class="p">)</span>
<span class="n">s</span><span class="o">.</span><span class="n">connect</span><span class="p">((</span><span class="s2">"127.0.0.1"</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">])))</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">"posix"</span><span class="p">:</span>
<span class="n">stream</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fdopen</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="s1">'w'</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">stream</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">makefile</span><span class="p">()</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">,</span> <span class="n">stream</span><span class="o">=</span><span class="n">stream</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
<span class="n">LOG</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s2">"cotyledon.tests.examples"</span><span class="p">)</span>
<span class="c1"># We don't want functional tests to wait for this:</span>
<span class="n">cotyledon</span><span class="o">.</span><span class="n">ServiceManager</span><span class="o">.</span><span class="n">_slowdown_respawn_if_needed</span> <span class="o">=</span> <span class="k">lambda</span> <span class="o">*</span><span class="n">args</span><span class="p">:</span> <span class="bp">True</span>
<span class="k">class</span> <span class="nc">FullService</span><span class="p">(</span><span class="n">cotyledon</span><span class="o">.</span><span class="n">Service</span><span class="p">):</span>
<span class="n">name</span> <span class="o">=</span> <span class="s2">"heavy"</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">worker_id</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">FullService</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">worker_id</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_shutdown</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">"</span><span class="si">%s</span><span class="s2"> init"</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">"</span><span class="si">%s</span><span class="s2"> run"</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_shutdown</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">terminate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">"</span><span class="si">%s</span><span class="s2"> terminate"</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_shutdown</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">reload</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">"</span><span class="si">%s</span><span class="s2"> reload"</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">LigthService</span><span class="p">(</span><span class="n">cotyledon</span><span class="o">.</span><span class="n">Service</span><span class="p">):</span>
<span class="n">name</span> <span class="o">=</span> <span class="s2">"light"</span>
<span class="k">class</span> <span class="nc">BuggyService</span><span class="p">(</span><span class="n">cotyledon</span><span class="o">.</span><span class="n">Service</span><span class="p">):</span>
<span class="n">name</span> <span class="o">=</span> <span class="s2">"buggy"</span>
<span class="n">graceful_shutdown_timeout</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">def</span> <span class="nf">terminate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">60</span><span class="p">)</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">"time.sleep done"</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">BadlyCodedService</span><span class="p">(</span><span class="n">cotyledon</span><span class="o">.</span><span class="n">Service</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">"so badly coded service"</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">OsloService</span><span class="p">(</span><span class="n">cotyledon</span><span class="o">.</span><span class="n">Service</span><span class="p">):</span>
<span class="n">name</span> <span class="o">=</span> <span class="s2">"oslo"</span>
<span class="k">class</span> <span class="nc">WindowService</span><span class="p">(</span><span class="n">cotyledon</span><span class="o">.</span><span class="n">Service</span><span class="p">):</span>
<span class="n">name</span> <span class="o">=</span> <span class="s2">"window"</span>
<span class="k">def</span> <span class="nf">on_terminate</span><span class="p">():</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">"master terminate hook"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_terminate2</span><span class="p">():</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">"master terminate2 hook"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_reload</span><span class="p">():</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">"master reload hook"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">example_app</span><span class="p">():</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">cotyledon</span><span class="o">.</span><span class="n">ServiceManager</span><span class="p">()</span>
<span class="n">p</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">FullService</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">service_id</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">LigthService</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="n">p</span><span class="o">.</span><span class="n">reconfigure</span><span class="p">(</span><span class="n">service_id</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">p</span><span class="o">.</span><span class="n">register_hooks</span><span class="p">(</span><span class="n">on_terminate</span><span class="p">,</span> <span class="n">on_reload</span><span class="p">)</span>
<span class="n">p</span><span class="o">.</span><span class="n">register_hooks</span><span class="p">(</span><span class="n">on_terminate2</span><span class="p">)</span>
<span class="n">p</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">buggy_app</span><span class="p">():</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">cotyledon</span><span class="o">.</span><span class="n">ServiceManager</span><span class="p">()</span>
<span class="n">p</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">BuggyService</span><span class="p">)</span>
<span class="n">p</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">oslo_app</span><span class="p">():</span>
<span class="n">conf</span> <span class="o">=</span> <span class="n">cfg</span><span class="o">.</span><span class="n">ConfigOpts</span><span class="p">()</span>
<span class="n">conf</span><span class="p">([],</span> <span class="n">project</span><span class="o">=</span><span class="s1">'openstack-app'</span><span class="p">,</span> <span class="n">validate_default_values</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">version</span><span class="o">=</span><span class="s2">"0.1"</span><span class="p">)</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">cotyledon</span><span class="o">.</span><span class="n">ServiceManager</span><span class="p">()</span>
<span class="n">oslo_config_glue</span><span class="o">.</span><span class="n">setup</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">conf</span><span class="p">)</span>
<span class="n">p</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">OsloService</span><span class="p">)</span>
<span class="n">p</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">window_sanity_check</span><span class="p">():</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">cotyledon</span><span class="o">.</span><span class="n">ServiceManager</span><span class="p">()</span>
<span class="n">p</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">LigthService</span><span class="p">)</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">_utils</span><span class="o">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">run</span><span class="p">)</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="n">os</span><span class="o">.</span><span class="n">kill</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">(),</span> <span class="n">signal</span><span class="o">.</span><span class="n">SIGTERM</span><span class="p">)</span>
<span class="n">t</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">badly_coded_app</span><span class="p">():</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">cotyledon</span><span class="o">.</span><span class="n">ServiceManager</span><span class="p">()</span>
<span class="n">p</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">BadlyCodedService</span><span class="p">)</span>
<span class="n">p</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="nb">globals</span><span class="p">()[</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]]()</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="api.html" title="previous chapter">API</a></li>
<li>Next: <a href="non-posix-support.html" title="next chapter">Note about non posix support</a></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/examples.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2017, Mehdi Abaakouk.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.7</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
|
<a href="_sources/examples.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>
|