| OLD | NEW |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Utilities for operating on instances.""" | 5 """Utilities for operating on instances.""" |
| 6 | 6 |
| 7 import json | 7 import json |
| 8 import logging | 8 import logging |
| 9 | 9 |
| 10 from google.appengine.ext import ndb | 10 from google.appengine.ext import ndb |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 if instance and not instance.cataloged: | 369 if instance and not instance.cataloged: |
| 370 if not utils.enqueue_task( | 370 if not utils.enqueue_task( |
| 371 '/internal/queues/delete-drained-instance', | 371 '/internal/queues/delete-drained-instance', |
| 372 'delete-drained-instance', | 372 'delete-drained-instance', |
| 373 params={ | 373 params={ |
| 374 'key': instance.key.urlsafe(), | 374 'key': instance.key.urlsafe(), |
| 375 }, | 375 }, |
| 376 ): | 376 ): |
| 377 logging.warning( | 377 logging.warning( |
| 378 'Failed to enqueue task for Instance: %s', instance.key) | 378 'Failed to enqueue task for Instance: %s', instance.key) |
| 379 | |
| 380 | |
| 381 def count_instances(): | |
| 382 """Counts the number of Instance entities in the datastore. | |
| 383 | |
| 384 Returns: | |
| 385 A 2-tuple of (orphaned, total). | |
| 386 Orphaned instances are instances no longer referred to by their parent. | |
| 387 """ | |
| 388 total = 0 | |
| 389 orphaned = 0 | |
| 390 for instance in models.Instance.query(): | |
| 391 total += 1 | |
| 392 instance_group_manager = instance.instance_group_manager.get() | |
| 393 if (not instance_group_manager | |
| 394 or instance.key not in instance_group_manager.instances): | |
| 395 logging.warning('Found orphaned Instance: %s', instance.key) | |
| 396 orphaned += 1 | |
| 397 logging.info('Orphaned Instances: %s/%s', orphaned, total) | |
| 398 return orphaned, total | |
| OLD | NEW |