0002-misc-extract-wait_operationals-function-60897.patch
hobo/environment/management/commands/cook.py | ||
---|---|---|
20 | 20 |
import os |
21 | 21 |
import string |
22 | 22 |
import subprocess |
23 |
import sys |
|
24 |
import time |
|
25 | 23 | |
26 | 24 |
from django.contrib.auth.models import User |
27 | 25 |
from django.contrib.contenttypes.models import ContentType |
... | ... | |
50 | 48 |
Wcs, |
51 | 49 |
Welco, |
52 | 50 |
) |
51 |
from hobo.environment.utils import wait_operationals |
|
53 | 52 |
from hobo.environment.validators import validate_service_url |
54 | 53 |
from hobo.multitenant.middleware import TenantMiddleware |
55 | 54 |
from hobo.profile.models import AttributeDefinition |
... | ... | |
79 | 78 |
self.verbosity = kwargs.get('verbosity') |
80 | 79 |
self.timeout = kwargs.get('timeout') |
81 | 80 |
self.permissive = kwargs.get('permissive') |
81 |
self.terminal_width = 0 |
|
82 | 82 |
if self.verbosity > 1: |
83 | 83 |
try: |
84 | 84 |
self.terminal_width = int(subprocess.check_output(['tput', 'cols']).strip()) |
... | ... | |
119 | 119 |
services = [] |
120 | 120 |
for service_class in AVAILABLE_SERVICES: |
121 | 121 |
services.extend(service_class.objects.all()) |
122 | ||
123 |
t0 = time.time() |
|
124 |
i = 0 |
|
125 |
last_service_url = None |
|
126 |
last_notification = t0 |
|
127 |
while len(services) > 0: |
|
128 |
if time.time() - last_notification > 15: |
|
129 |
last_notification = time.time() |
|
130 |
notify_agents(None) |
|
131 |
for service in services[:]: |
|
132 |
if service.last_operational_success_timestamp: |
|
133 |
services.remove(service) |
|
134 |
continue |
|
135 |
service.check_operational() |
|
136 |
if len(services) == 0: |
|
137 |
break |
|
138 |
if self.verbosity == 1: |
|
139 |
sys.stderr.write('.') |
|
140 |
elif self.verbosity > 1: |
|
141 |
if last_service_url != services[0].base_url: |
|
142 |
last_service_url = services[0].base_url |
|
143 |
i = 0 |
|
144 |
elif i == (self.terminal_width - len(services[0].base_url) - 25): |
|
145 |
i = 0 |
|
146 |
i += 1 |
|
147 |
sys.stderr.write('[2K\rWaiting for %s ' % services[0].base_url) |
|
148 |
sys.stderr.write('%5ds ' % (timeout - (time.time() - t0))) |
|
149 |
sys.stderr.write('.' * i) |
|
150 |
sys.stderr.flush() |
|
151 |
time.sleep(0.5) |
|
152 |
if time.time() - t0 > timeout: |
|
153 |
if self.verbosity: |
|
154 |
sys.stderr.write('\n') |
|
155 |
raise CommandError('timeout waiting for %s' % ', '.join([x.base_url for x in services])) |
|
122 |
wait_operationals(services, timeout, self.verbosity, self.terminal_width, notify_agents) |
|
156 | 123 | |
157 | 124 |
def create_hobo(self, url, primary=False, title=None, slug=None, **kwargs): |
158 | 125 |
if connection.get_tenant().schema_name == 'public': |
hobo/environment/utils.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU Affero General Public License |
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
import hashlib |
|
17 |
import sys |
|
18 |
import time |
|
18 | 19 | |
19 | 20 |
from django.conf import settings |
21 |
from django.core.management.base import CommandError |
|
20 | 22 |
from django.db import connection, transaction |
21 | 23 |
from django.urls import reverse |
22 | 24 |
from django.utils.encoding import force_text |
... | ... | |
203 | 205 |
for key, value in fields.items(): |
204 | 206 |
setattr(obj, key, value) |
205 | 207 |
obj.save() |
208 | ||
209 | ||
210 |
def wait_operationals(services, timeout, verbosity, terminal_width, notify_agents_func): |
|
211 |
t0 = time.time() |
|
212 |
i = 0 |
|
213 |
last_service_url = None |
|
214 |
last_notification = t0 |
|
215 |
while len(services) > 0: |
|
216 |
if time.time() - last_notification > 15: |
|
217 |
last_notification = time.time() |
|
218 |
notify_agents_func(None) |
|
219 |
for service in services[:]: |
|
220 |
if service.last_operational_success_timestamp: |
|
221 |
services.remove(service) |
|
222 |
continue |
|
223 |
service.check_operational() |
|
224 |
if len(services) == 0: |
|
225 |
break |
|
226 |
if verbosity == 1: |
|
227 |
sys.stderr.write('.') |
|
228 |
elif verbosity > 1: |
|
229 |
if last_service_url != services[0].base_url: |
|
230 |
last_service_url = services[0].base_url |
|
231 |
i = 0 |
|
232 |
elif i == (terminal_width - len(services[0].base_url) - 25): |
|
233 |
i = 0 |
|
234 |
i += 1 |
|
235 |
sys.stderr.write('\rWaiting for %s ' % services[0].base_url) |
|
236 |
sys.stderr.write('%5ds ' % (timeout - (time.time() - t0))) |
|
237 |
sys.stderr.write('.' * i) |
|
238 |
sys.stderr.flush() |
|
239 |
time.sleep(0.5) |
|
240 |
if time.time() - t0 > timeout: |
|
241 |
if verbosity: |
|
242 |
sys.stderr.write('\n') |
|
243 |
raise CommandError('timeout waiting for %s' % ', '.join([x.base_url for x in services])) |
tests/test_cook.py | ||
---|---|---|
179 | 179 |
# already operational |
180 | 180 |
obj1.last_operational_success_timestamp = 'some date' |
181 | 181 |
obj2.last_operational_success_timestamp = 'some date' |
182 |
command.terminal_width = 80 |
|
182 | 183 |
command.wait_operationals(2) |
183 | 184 |
assert True |
184 | 185 | |
185 |
- |