Scott Baker | b453eef | 2013-05-19 16:20:09 -0700 | [diff] [blame] | 1 | import datetime |
| 2 | import os |
| 3 | import sys |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 4 | #os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
Scott Baker | b453eef | 2013-05-19 16:20:09 -0700 | [diff] [blame] | 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() |
Sapan Bhatia | 13d2db9 | 2014-11-11 21:47:45 -0500 | [diff] [blame] | 24 | for reservedResource in reservation.reservedresources.all(): |
Scott Baker | b453eef | 2013-05-19 16:20:09 -0700 | [diff] [blame] | 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 | |