blob: cee52690e9317deeccc8dc6a1d1f18074f361c41 [file] [log] [blame]
Scott Bakerb453eef2013-05-19 16:20:09 -07001import datetime
2import os
3import sys
Scott Baker76a840e2015-02-11 21:38:09 -08004#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
Scott Bakerb453eef2013-05-19 16:20:09 -07005import 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