blob: 0fe406b86df6fe095a4a04b66dc20aeb35a6134b [file] [log] [blame]
Scott Bakerb453eef2013-05-19 16:20:09 -07001import datetime
2import os
3import sys
4#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
5import time
6import django.utils
7from core.models.sliver import Sliver
8from core.models.reservation import Reservation, ReservedResource
9
10class 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 Bhatia13d2db92014-11-11 21:47:45 -050024 for reservedResource in reservation.reservedresources.all():
Scott Bakerb453eef2013-05-19 16:20:09 -070025 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
49if __name__ == '__main__':
50 ReservationAgent().run()
51