misc: extract wait_operationals function (#60897)
This commit is contained in:
parent
cf15e711e6
commit
85bd9f6b15
|
@ -20,8 +20,6 @@ import json
|
|||
import os
|
||||
import string
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
@ -50,6 +48,7 @@ from hobo.environment.models import (
|
|||
Wcs,
|
||||
Welco,
|
||||
)
|
||||
from hobo.environment.utils import wait_operationals
|
||||
from hobo.environment.validators import validate_service_url
|
||||
from hobo.multitenant.middleware import TenantMiddleware
|
||||
from hobo.profile.models import AttributeDefinition
|
||||
|
@ -79,6 +78,7 @@ class Command(BaseCommand):
|
|||
self.verbosity = kwargs.get('verbosity')
|
||||
self.timeout = kwargs.get('timeout')
|
||||
self.permissive = kwargs.get('permissive')
|
||||
self.terminal_width = 0
|
||||
if self.verbosity > 1:
|
||||
try:
|
||||
self.terminal_width = int(subprocess.check_output(['tput', 'cols']).strip())
|
||||
|
@ -119,40 +119,7 @@ class Command(BaseCommand):
|
|||
services = []
|
||||
for service_class in AVAILABLE_SERVICES:
|
||||
services.extend(service_class.objects.all())
|
||||
|
||||
t0 = time.time()
|
||||
i = 0
|
||||
last_service_url = None
|
||||
last_notification = t0
|
||||
while len(services) > 0:
|
||||
if time.time() - last_notification > 15:
|
||||
last_notification = time.time()
|
||||
notify_agents(None)
|
||||
for service in services[:]:
|
||||
if service.last_operational_success_timestamp:
|
||||
services.remove(service)
|
||||
continue
|
||||
service.check_operational()
|
||||
if len(services) == 0:
|
||||
break
|
||||
if self.verbosity == 1:
|
||||
sys.stderr.write('.')
|
||||
elif self.verbosity > 1:
|
||||
if last_service_url != services[0].base_url:
|
||||
last_service_url = services[0].base_url
|
||||
i = 0
|
||||
elif i == (self.terminal_width - len(services[0].base_url) - 25):
|
||||
i = 0
|
||||
i += 1
|
||||
sys.stderr.write('[2K\rWaiting for %s ' % services[0].base_url)
|
||||
sys.stderr.write('%5ds ' % (timeout - (time.time() - t0)))
|
||||
sys.stderr.write('.' * i)
|
||||
sys.stderr.flush()
|
||||
time.sleep(0.5)
|
||||
if time.time() - t0 > timeout:
|
||||
if self.verbosity:
|
||||
sys.stderr.write('\n')
|
||||
raise CommandError('timeout waiting for %s' % ', '.join([x.base_url for x in services]))
|
||||
wait_operationals(services, timeout, self.verbosity, self.terminal_width, notify_agents)
|
||||
|
||||
def create_hobo(self, url, primary=False, title=None, slug=None, **kwargs):
|
||||
if connection.get_tenant().schema_name == 'public':
|
||||
|
|
|
@ -14,9 +14,11 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import hashlib
|
||||
import sys
|
||||
import time
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.management.base import CommandError
|
||||
from django.db import connection, transaction
|
||||
from django.urls import reverse
|
||||
from django.utils.encoding import force_text
|
||||
|
@ -203,3 +205,39 @@ def import_parameters(parameters):
|
|||
for key, value in fields.items():
|
||||
setattr(obj, key, value)
|
||||
obj.save()
|
||||
|
||||
|
||||
def wait_operationals(services, timeout, verbosity, terminal_width, notify_agents_func):
|
||||
t0 = time.time()
|
||||
i = 0
|
||||
last_service_url = None
|
||||
last_notification = t0
|
||||
while len(services) > 0:
|
||||
if time.time() - last_notification > 15:
|
||||
last_notification = time.time()
|
||||
notify_agents_func(None)
|
||||
for service in services[:]:
|
||||
if service.last_operational_success_timestamp:
|
||||
services.remove(service)
|
||||
continue
|
||||
service.check_operational()
|
||||
if len(services) == 0:
|
||||
break
|
||||
if verbosity == 1:
|
||||
sys.stderr.write('.')
|
||||
elif verbosity > 1:
|
||||
if last_service_url != services[0].base_url:
|
||||
last_service_url = services[0].base_url
|
||||
i = 0
|
||||
elif i == (terminal_width - len(services[0].base_url) - 25):
|
||||
i = 0
|
||||
i += 1
|
||||
sys.stderr.write('\rWaiting for %s ' % services[0].base_url)
|
||||
sys.stderr.write('%5ds ' % (timeout - (time.time() - t0)))
|
||||
sys.stderr.write('.' * i)
|
||||
sys.stderr.flush()
|
||||
time.sleep(0.5)
|
||||
if time.time() - t0 > timeout:
|
||||
if verbosity:
|
||||
sys.stderr.write('\n')
|
||||
raise CommandError('timeout waiting for %s' % ', '.join([x.base_url for x in services]))
|
||||
|
|
|
@ -179,6 +179,7 @@ def test_wait_operationals(db, monkeypatch):
|
|||
# already operational
|
||||
obj1.last_operational_success_timestamp = 'some date'
|
||||
obj2.last_operational_success_timestamp = 'some date'
|
||||
command.terminal_width = 80
|
||||
command.wait_operationals(2)
|
||||
assert True
|
||||
|
||||
|
|
Loading…
Reference in New Issue