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

40 lines
1.5 KiB
Python
Raw Normal View History

2014-07-09 16:17:43 +00:00
from celery import shared_task
2014-07-11 14:48:46 +00:00
from django.utils import timezone
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()
2014-07-11 14:48:46 +00:00
if not resource.disable_trigger:
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)
2014-07-10 10:03:22 +00:00
data.used = current
2014-07-11 14:48:46 +00:00
data.las_update = timezone.now()
2014-07-10 10:03:22 +00:00
data.save()
Operation.execute(operations)