Scott Baker | b453eef | 2013-05-19 16:20:09 -0700 | [diff] [blame] | 1 | import datetime |
| 2 | import os |
| 3 | import sys |
| 4 | #os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
| 5 | import time |
| 6 | import django.utils |
| 7 | from core.models.sliver import Sliver |
| 8 | from core.models.reservation import Reservation, ReservedResource |
| 9 | |
| 10 | class ReservationAgent: |
| 11 | |
| 12 | def run(self): |
| 13 | while True : |
| 14 | slivers = {} |
| 15 | |
| 16 | tNow = django.utils.timezone.now() |
| 17 | print "Processing reservations, tNow is %s" % tNow |
| 18 | reservations = Reservation.objects.filter(startTime__lte = tNow) |
| 19 | for reservation in reservations: |
| 20 | print " Processing reservation %s" % reservation |
| 21 | if reservation.endTime <= tNow: |
| 22 | print " deleting expired reservation" |
| 23 | reservation.delete() |
| 24 | for reservedResource in reservation.reservedResources.all(): |
| 25 | sliver_resources = slivers.get(reservedResource.sliver.id, {}) |
| 26 | sliver_resources[reservedResource.resource.name] = reservedResource.quantity |
| 27 | slivers[reservedResource.sliver.id] = sliver_resources |
| 28 | |
| 29 | print "Sliver reservation set" |
| 30 | for (sliverid, sliver_resources) in slivers.items(): |
| 31 | print " sliver", sliverid, |
| 32 | for (name, value) in sliver_resources.items(): |
| 33 | print str(name)+":", value, |
| 34 | print |
| 35 | |
| 36 | print "Updating slivers" |
| 37 | for sliver in Sliver.objects.all(): |
| 38 | sliver_resv = slivers.get(sliver.id, {}) |
| 39 | numberCores = sliver_resv.get("numberCores", 0) |
| 40 | if numberCores != sliver.numberCores: |
| 41 | print "sliver %s setting numberCores to %s" % (sliver.name, numberCores) |
| 42 | sliver.numberCores = numberCores |
| 43 | sliver.save() |
| 44 | |
| 45 | print "sleep" |
| 46 | time.sleep(7) |
| 47 | |
| 48 | |
| 49 | if __name__ == '__main__': |
| 50 | ReservationAgent().run() |
| 51 | |