blob: 3ccffee4117a0dae290e84c4191010b91ff25944 [file] [log] [blame]
Scott Baker7d018ca2014-01-23 15:41:45 -08001import os
2import sys
3import base64
4import traceback
5from collections import defaultdict
6from django.db.models import F, Q
Scott Baker86e132c2015-02-11 21:38:09 -08007from xos.config import Config
Scott Baker7d018ca2014-01-23 15:41:45 -08008from util.logger import Logger, logging
9from observer.syncstep import SyncStep
10from hpc.models import ServiceProvider, ContentProvider, CDNPrefix, OriginServer
11from core.models import *
12
13# hpclibrary will be in steps/..
14parentdir = os.path.join(os.path.dirname(__file__),"..")
15sys.path.insert(0,parentdir)
16
17from hpclib import HpcLibrary
18
19logger = Logger(level=logging.INFO)
20
21class GarbageCollector(SyncStep, HpcLibrary):
22# requested_interval = 86400
23 requested_interval = 0
24 provides=[]
25
26 def __init__(self, **args):
27 SyncStep.__init__(self, **args)
28 HpcLibrary.__init__(self)
29
30 def call(self, **args):
31 logger.info("running garbage collector")
32 try:
33 self.gc_originservers()
34 self.gc_cdnprefixes()
35 self.gc_contentproviders()
36 self.gc_serviceproviders()
37 except:
38 traceback.print_exc()
39
40 def gc_onev(self, ps_class, ps_idField, onev_className, onev_idField):
41 # get the CMI's objects
42 onev_objs = self.client.onev.ListAll(onev_className)
43
44 # get the data model's objects,
45 ps_objs = ps_class.objects.filter(enacted__isnull=False)
46 ps_ids = [str(getattr(x,ps_idField,None)) for x in ps_objs]
47
48 # for each onev object, if it's id does not exist in a data model
49 # object, then delete it.
50 for onev_obj in onev_objs:
51 onev_id = onev_obj[onev_idField]
52 if str(onev_id) not in ps_ids:
53 logger.info("garbage collecting %s %s" % (onev_className, str(onev_id)))
54 self.client.onev.Delete(onev_className, onev_id)
55
56 def gc_originservers(self):
57 self.gc_onev(OriginServer, "origin_server_id", "OriginServer", "origin_server_id")
58
59 def gc_cdnprefixes(self):
60 self.gc_onev(CDNPrefix, "cdn_prefix_id", "CDNPrefix", "cdn_prefix_id")
61
62 def gc_contentproviders(self):
63 self.gc_onev(ContentProvider, "content_provider_id", "ContentProvider", "content_provider_id")
64
65 def gc_serviceproviders(self):
66 self.gc_onev(ServiceProvider, "service_provider_id", "ServiceProvider", "service_provider_id")
67