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-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-09-30 14:46:29 +00:00
|
|
|
model = get_model(backend.model)
|
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-09-22 15:59:53 +00:00
|
|
|
data.update()
|
2014-07-11 14:48:46 +00:00
|
|
|
if not resource.disable_trigger:
|
2014-09-22 15:59:53 +00:00
|
|
|
if data.used < 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-09-22 15:59:53 +00:00
|
|
|
elif data.used < data.allocated:
|
2014-07-11 21:09:17 +00:00
|
|
|
op = Operation.create(backend, obj, Operation.RECOVERY)
|
2014-09-26 15:05:20 +00:00
|
|
|
operations.append(op)
|
2014-09-22 15:59:53 +00:00
|
|
|
# data = ResourceData.get_or_create(obj, resource)
|
|
|
|
# current = data.get_used()
|
|
|
|
# if not resource.disable_trigger:
|
|
|
|
# if data.used < data.allocated and current > data.allocated:
|
|
|
|
# op = Operation.create(backend, obj, Operation.EXCEED)
|
|
|
|
# operations.append(op)
|
|
|
|
# elif data.used > data.allocated and current < data.allocated:
|
|
|
|
# op = Operation.create(backend, obj, Operation.RECOVERY)
|
|
|
|
# operation.append(op)
|
|
|
|
# data.update(current=current)
|
2014-07-10 10:03:22 +00:00
|
|
|
Operation.execute(operations)
|