django-orchestra-test/orchestra/apps/resources/tasks.py

39 lines
1.4 KiB
Python
Raw Normal View History

2014-07-09 16:17:43 +00:00
from celery import shared_task
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-10 10:03:22 +00:00
from .models import MonitorData
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)
model = backend.model
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 = []
model = resource.model
for obj in model.objects.all():
data = MonitorData.get_or_create(obj, resource)
current = data.get_used()
if data.used < data.allocated and current > data.allocated:
op = Operation.create(backend, data.content_object, Operation.EXCEED)
operations.append(op)
elif res.used > res.allocated and current < res.allocated:
op = Operation.create(backend, data.content_object, Operation.RECOVERY)
operation.append(op)
data.used = current
data.las_update = datetime.now()
data.save()
Operation.execute(operations)