2014-07-09 16:17:43 +00:00
|
|
|
from celery import shared_task
|
2014-07-11 21:09:17 +00:00
|
|
|
from django.db.models.loading import get_model
|
2014-07-11 14:48:46 +00:00
|
|
|
from django.utils import timezone
|
2014-07-11 21:09:17 +00:00
|
|
|
|
2014-07-10 10:03:22 +00:00
|
|
|
from orchestra.apps.orchestration.models import BackendOperation as Operation
|
|
|
|
|
2014-07-09 16:17:43 +00:00
|
|
|
from .backends import ServiceMonitor
|
2014-07-11 21:09:17 +00:00
|
|
|
from .models import ResourceData, Resource
|
2014-07-09 16:17:43 +00:00
|
|
|
|
|
|
|
|
2014-07-10 10:03:22 +00:00
|
|
|
@shared_task(name='resources.Monitor')
|
|
|
|
def monitor(resource_id):
|
|
|
|
resource = Resource.objects.get(pk=resource_id)
|
|
|
|
|
|
|
|
# Execute monitors
|
|
|
|
for monitor_name in resource.monitors:
|
|
|
|
backend = ServiceMonitor.get_backend(monitor_name)
|
2014-07-11 21:09:17 +00:00
|
|
|
model = get_model(*backend.model.split('.'))
|
2014-07-10 10:03:22 +00:00
|
|
|
operations = []
|
|
|
|
# Execute monitor
|
|
|
|
for obj in model.objects.all():
|
|
|
|
operations.append(Operation.create(backend, obj, Operation.MONITOR))
|
|
|
|
Operation.execute(operations)
|
|
|
|
|
|
|
|
# Update used resources and trigger resource exceeded and revovery
|
|
|
|
operations = []
|
2014-07-11 21:09:17 +00:00
|
|
|
model = resource.content_type.model_class()
|
2014-07-10 10:03:22 +00:00
|
|
|
for obj in model.objects.all():
|
2014-07-11 21:09:17 +00:00
|
|
|
data = ResourceData.get_or_create(obj, resource)
|
2014-07-10 10:03:22 +00:00
|
|
|
current = data.get_used()
|
2014-07-11 14:48:46 +00:00
|
|
|
if not resource.disable_trigger:
|
|
|
|
if data.used < data.allocated and current > data.allocated:
|
2014-07-11 21:09:17 +00:00
|
|
|
op = Operation.create(backend, obj, Operation.EXCEED)
|
2014-07-11 14:48:46 +00:00
|
|
|
operations.append(op)
|
2014-07-11 21:09:17 +00:00
|
|
|
elif data.used > data.allocated and current < data.allocated:
|
|
|
|
op = Operation.create(backend, obj, Operation.RECOVERY)
|
2014-07-11 14:48:46 +00:00
|
|
|
operation.append(op)
|
2014-07-11 21:09:17 +00:00
|
|
|
data.used = current or 0
|
|
|
|
data.last_update = timezone.now()
|
2014-07-10 10:03:22 +00:00
|
|
|
data.save()
|
|
|
|
Operation.execute(operations)
|