@@ -85,71 +85,3 @@ def server(ctx):
8585
8686 from mfr .server .app import serve
8787 serve ()
88-
89- @task
90- def export_cache_clean (ctx , extension = '' ):
91- cache_clean ('export/' , extension )
92-
93- @task
94- def render_cache_clean (ctx , extension = '' ):
95- cache_clean ('render/' , extension )
96-
97- def cache_clean (folder , extension ):
98- # NOTE: Manually install gevent & pyrax, no need for it to be depenency just for this method.
99-
100- from gevent import monkey
101- from gevent .pool import Pool
102- from gevent import Timeout
103-
104- monkey .patch_all ()
105-
106- import six
107- import pyrax
108- import logging
109- from mfr .server import settings
110-
111- # Monkey patch pyrax for python 3 compatibility.
112- def _add_details (self , info ):
113- """
114- Takes the dict returned by the API call and sets the
115- corresponding attributes on the object.
116- """
117- for (key , val ) in six .iteritems (info ):
118- if six .PY2 and isinstance (key , six .text_type ):
119- key = key .encode (pyrax .get_encoding ())
120- elif isinstance (key , bytes ):
121- key = key .decode ("utf-8" )
122- setattr (self , key , val )
123- pyrax .resource .BaseResource ._add_details = _add_details
124-
125- # WARNING: We are using provider specific functions to enumerate files to quickly
126- # purge the cache, which can contain hundreds of thousands of objects. Thus
127- # asserting the provider, we will need to update if we move providers.
128- assert settings .CACHE_PROVIDER_NAME == 'cloudfiles'
129-
130- logging .captureWarnings (True )
131-
132- pyrax .set_setting ('identity_type' , 'rackspace' )
133- pyrax .set_setting ('verify_ssl' , True )
134- pyrax .set_credentials (settings .CACHE_PROVIDER_CREDENTIALS ['username' ], settings .CACHE_PROVIDER_CREDENTIALS ['token' ])
135-
136- cf = pyrax .connect_to_cloudfiles (region = settings .CACHE_PROVIDER_CREDENTIALS ['region' ].upper (), public = True )
137- container = cf .get_container (settings .CACHE_PROVIDER_SETTINGS ['container' ])
138-
139- def delete_object (obj ):
140- # added timeout of 5 seconds just in case
141- with Timeout (5 , False ):
142- try :
143- print (obj )
144- obj .delete ()
145- except Exception as ex :
146- print (ex )
147-
148- pool = Pool (100 )
149- objects = container .get_objects (prefix = folder , limit = 5000 , marker = '' )
150- while objects :
151- for obj in objects :
152- if obj .name .endswith (extension ):
153- pool .spawn (delete_object , obj )
154- objects = container .get_objects (prefix = folder , limit = 5000 , marker = objects [- 1 ].name )
155- pool .join ()
0 commit comments