def get(self, request, format=None): """Return a list of all users.""" stats = GlobalStats() return Response({ "units": stats.all, "units_translated": stats.translated, "users": User.objects.count(), "changes": Change.objects.count(), "projects": Project.objects.count(), "components": Component.objects.count(), "translations": Translation.objects.count(), "languages": stats.languages, "checks": Check.objects.count(), "configuration_errors": ConfigurationError.objects.filter(ignored=False).count(), "suggestions": Suggestion.objects.count(), "celery_queues": get_queue_stats(), "name": settings.SITE_TITLE, })
def is_celery_queue_long(): stats = get_queue_stats() if stats.pop('search') > 10000: return True if stats.pop('translate', 0) > 1000: return True return any(stat > 50 for stat in stats.values())
def get(self, request, format=None): """ Return a list of all users. """ stats = GlobalStats() queues = get_queue_stats() return Response({ 'units': stats.all, 'units_translated': stats.translated, 'users': User.objects.count(), 'changes': Change.objects.count(), 'projects': Project.objects.count(), 'components': Component.objects.count(), 'translations': Translation.objects.count(), 'languages': stats.languages, 'checks': Check.objects.count(), 'suggestions': Suggestion.objects.count(), 'index_updates': queues.get('search', 0), 'celery_queue': queues.get('celery', 0), 'celery_memory_queue': queues.get('memory', 0), 'celery_notification_queue': queues.get('notification', 0), 'celery_queues': queues, 'name': settings.SITE_TITLE, })
def check_celery(app_configs, **kwargs): errors = [] if settings.CELERY_TASK_ALWAYS_EAGER: errors.append( Error( 'Celery is configured in the eager mode', hint=get_doc_url('admin/install', 'celery'), id='weblate.E005', )) elif settings.CELERY_BROKER_URL == 'memory://': errors.append( Critical( 'Celery is configured to store queue in local memory', hint=get_doc_url('admin/install', 'celery'), id='weblate.E026', )) else: stats = get_queue_stats() if stats['celery'] > 50 or stats['search'] > 10000: errors.append( Critical( 'The Celery tasks queue is too long, either the worker ' 'is not running or is too slow.', hint=get_doc_url('admin/install', 'celery'), id='weblate.E009', )) result = ping.delay() try: result.get(timeout=10, disable_sync_subtasks=False) except TimeoutError: errors.append( Critical( 'The Celery does not process tasks or is too slow ' 'in processing them.', hint=get_doc_url('admin/install', 'celery'), id='weblate.E019', )) except NotImplementedError: errors.append( Critical( 'The Celery is not configured to store results, ' 'CELERY_RESULT_BACKEND is probably not set.', hint=get_doc_url('admin/install', 'celery'), id='weblate.E020', )) heartbeat = cache.get('celery_heartbeat') if not heartbeat or time.time() - heartbeat > 600: errors.append( Critical( 'The Celery beats scheduler is not executing periodic tasks ' 'in a timely manner.', hint=get_doc_url('admin/install', 'celery'), id='weblate.C030', )) return errors
def performance(request): """Show performance tuning tips.""" if request.method == 'POST': return handle_dismiss(request) context = { 'checks': run_checks(include_deployment_checks=True), 'errors': ConfigurationError.objects.filter(ignored=False), 'queues': get_queue_stats().items(), 'menu_items': MENU, 'menu_page': 'performance', } return render(request, "manage/performance.html", context)
def performance(request): """Show performance tuning tips.""" if request.method == "POST": return handle_dismiss(request) context = { "checks": run_checks(include_deployment_checks=True), "errors": ConfigurationError.objects.filter(ignored=False), "queues": get_queue_stats().items(), "menu_items": MENU, "menu_page": "performance", } return render(request, "manage/performance.html", context)
def is_celery_queue_long(): """ Checks whether celery queue is too long. It does trigger if it is too long for at least one hour. This way peaks are filtered out, and no warning need be issued for big operations (for example site-wide autotranslation). """ from weblate.trans.models import Translation cache_key = "celery_queue_stats" queues_data = cache.get(cache_key, {}) # Hours since epoch current_hour = int(time.time() / 3600) test_hour = current_hour - 1 # Fetch current stats stats = get_queue_stats() # Update counters if current_hour not in queues_data: # Delete stale items for key in list(queues_data.keys()): if key < test_hour: del queues_data[key] # Add current one queues_data[current_hour] = stats # Store to cache cache.set(cache_key, queues_data, 7200) # Do not fire if we do not have counts for two hours ago if test_hour not in queues_data: return False # Check if any queue got bigger base = queues_data[test_hour] thresholds = defaultdict(lambda: 50) # Set the limit to avoid trigger on auto-translating all components # nightly. thresholds["translate"] = max(1000, Translation.objects.count() / 30) return any( stat > thresholds[key] and base.get(key, 0) > thresholds[key] for key, stat in stats.items() )
def performance(request): """Show performance tuning tips.""" if request.method == "POST": return handle_dismiss(request) checks = run_checks(include_deployment_checks=True) context = { "checks": [check for check in checks if not check.is_silenced()], "errors": ConfigurationError.objects.filter(ignored=False), "queues": get_queue_stats().items(), "menu_items": MENU, "menu_page": "performance", "web_encoding": [sys.getfilesystemencoding(), sys.getdefaultencoding()], "celery_encoding": cache.get("celery_encoding"), "database_latency": measure_database_latency(), "cache_latency": measure_cache_latency(), } return render(request, "manage/performance.html", context)
def check_celery(app_configs, **kwargs): errors = [] if settings.CELERY_TASK_ALWAYS_EAGER: errors.append( Error( 'Celery is configured in the eager mode', hint=get_doc_url('admin/install', 'celery'), id='weblate.E005', )) else: stats = get_queue_stats() if stats['celery'] > 50 or stats['search'] > 10000: errors.append( Critical( 'The Celery tasks queue is too long, either the worker ' 'is not running or is too slow.', hint=get_doc_url('admin/install', 'celery'), id='weblate.E009', )) result = ping.delay() try: result.get(timeout=10, disable_sync_subtasks=False) except TimeoutError: errors.append( Critical( 'The Celery does not process tasks or is too slow ' 'in processing them.', hint=get_doc_url('admin/install', 'celery'), id='weblate.E019', )) except NotImplementedError: errors.append( Critical( 'The Celery is not configured to store results, ' 'CELERY_RESULT_BACKEND is probably not set.', hint=get_doc_url('admin/install', 'celery'), id='weblate.E020', )) return errors
def is_celery_queue_long(): """ Checks whether celery queue is too long. It does trigger if it is too long for at least one hour. This way we filter out peaks and avoid flipping warning on big operations (eg. site-wide autotranslate). """ cache_key = "celery_queue_stats" queues_data = cache.get(cache_key, {}) # Hours since epoch current_hour = int(time.time() / 3600) test_hour = current_hour - 1 # Fetch current stats stats = get_queue_stats() # Update counters if current_hour not in queues_data: # Delete stale items for key in list(queues_data.keys()): if key < test_hour: del queues_data[key] # Add current one queues_data[current_hour] = stats # Store to cache cache.set(cache_key, queues_data, 7200) # Do not fire if we do not have counts for two hours ago if test_hour not in queues_data: return False # Check if any queue got bigger base = queues_data[test_hour] thresholds = defaultdict(lambda: 50) thresholds["translate"] = 1000 return any(stat > thresholds[key] and base.get(key, 0) > thresholds[key] for key, stat in stats.items())
def handle(self, *args, **options): for key, value in sorted(get_queue_stats().items()): self.stdout.write("{}: {}".format(key, value))
def handle(self, *args, **options): for key, value in sorted(get_queue_stats().items()): self.stdout.write(f"{key}: {value}")