CC-3372: Improved pypo cache delete

-Done
This commit is contained in:
Martin Konecny 2012-03-05 19:02:46 -05:00
parent 249b743dd1
commit 9715de11f5
3 changed files with 38 additions and 51 deletions

View File

@ -400,29 +400,22 @@ class Application_Model_Schedule {
}
/**
* Returns array indexed by:
* "playlistId"/"playlist_id" (aliases to the same thing)
* "start"/"starts" (aliases to the same thing) as YYYY-MM-DD HH:MM:SS.nnnnnn
* "end"/"ends" (aliases to the same thing) as YYYY-MM-DD HH:MM:SS.nnnnnn
* "group_id"/"id" (aliases to the same thing)
* "clip_length" (for audio clips this is the length of the audio clip,
* for playlists this is the length of the entire playlist)
* "name" (playlist only)
* "creator" (playlist only)
* "file_id" (audioclip only)
* "count" (number of items in the playlist, always 1 for audioclips.
* Note that playlists with one item will also have count = 1.
* Returns an array of schedule items from cc_schedule table. Tries
* to return at least 3 items (if they are available). The parameters
* $p_startTime and $p_endTime specify the range. Schedule items returned
* do not have to be entirely within this range. It is enough that the end
* or beginning of the scheduled item is in the range.
*
*
* @param string $p_fromDateTime
* @param string $p_startTime
* In the format YYYY-MM-DD HH:MM:SS.nnnnnn
* @param string $p_toDateTime
* @param string $p_endTime
* In the format YYYY-MM-DD HH:MM:SS.nnnnnn
* @param boolean $p_playlistsOnly
* Retrieve playlists as a single item.
* @return array
* Returns null if nothing found
* Returns null if nothing found, else an array of associative
* arrays representing each row.
*/
public static function GetItems($p_currentDateTime, $p_toDateTime) {
public static function GetItems($p_startTime, $p_endTime) {
global $CC_CONFIG, $CC_DBC;
$baseQuery = "SELECT st.file_id AS file_id,"
@ -440,8 +433,8 @@ class Application_Model_Schedule {
." ON st.instance_id = si.id";
$predicates = " WHERE st.ends > '$p_currentDateTime'"
." AND st.starts < '$p_toDateTime'"
$predicates = " WHERE st.ends > '$p_startTime'"
." AND st.starts < '$p_endTime'"
." ORDER BY st.starts";
$sql = $baseQuery.$predicates;
@ -458,7 +451,7 @@ class Application_Model_Schedule {
$dt->add(new DateInterval("PT30M"));
$range_end = $dt->format("Y-m-d H:i:s");
$predicates = " WHERE st.ends > '$p_currentDateTime'"
$predicates = " WHERE st.ends > '$p_startTime'"
." AND st.starts < '$range_end'"
." ORDER BY st.starts"
." LIMIT 3";

View File

@ -315,12 +315,10 @@ class PypoFetch(Thread):
self.logger.debug("Pushing to pypo-push")
self.push_queue.put(media)
"""
TODO
# cleanup
try: self.cleanup()
try: self.cleanup(media)
except Exception, e: self.logger.error("%s", e)
"""
@ -446,31 +444,27 @@ class PypoFetch(Thread):
else:
self.logger.debug("try to download %s", media_item['uri'])
self.api_client.get_media(media_item['uri'], dst)
"""
Cleans up folders in cache_dir. Look for modification date older than "now - CACHE_FOR"
and deletes them.
"""
def cleanup(self):
offset = 3600 * int(config["cache_for"])
now = time.time()
for r, d, f in os.walk(self.cache_dir):
for dir in d:
try:
timestamp = calendar.timegm(time.strptime(dir, "%Y-%m-%d-%H-%M-%S"))
if (now - timestamp) > offset:
try:
self.logger.debug('trying to remove %s - timestamp: %s', os.path.join(r, dir), timestamp)
shutil.rmtree(os.path.join(r, dir))
except Exception, e:
self.logger.error("%s", e)
pass
else:
self.logger.info('sucessfully removed %s', os.path.join(r, dir))
except Exception, e:
self.logger.error(e)
def cleanup(self, media):
"""
Get list of all files in the cache dir and remove them if they aren't being used anymore.
Input dict() media, lists all files that are scheduled or currently playing. Not being in this
dict() means the file is safe to remove.
"""
cached_file_set = set(os.listdir(self.cache_dir))
scheduled_file_set = set()
for mkey in media:
media_item = media[mkey]
fileExt = os.path.splitext(media_item['uri'])[1]
scheduled_file_set.add(media_item["id"] + fileExt)
unneeded_files = cached_file_set - scheduled_file_set
self.logger.debug("Files to remove " + str(unneeded_files))
for file in unneeded_files:
self.logger.debug("Removing %s" % os.path.join(self.cache_dir, file))
os.remove(os.path.join(self.cache_dir, file))
def main(self):
# Bootstrap: since we are just starting up, we need to grab the

View File

@ -70,7 +70,7 @@ class PypoPush(Thread):
str_tnow_s = "%04d-%02d-%02d-%02d-%02d-%02d" % (tnow[0], tnow[1], tnow[2], tnow[3], tnow[4], tnow[5])
str_tcoming_s = "%04d-%02d-%02d-%02d-%02d-%02d" % (tcoming[0], tcoming[1], tcoming[2], tcoming[3], tcoming[4], tcoming[5])
for key in media:
for key in media.keys():
media_item = media[key]
item_start = media_item['start'][0:19]