def __call__(self): """Provides ability to execute the task in a consistent manner.""" try: _start = timeutil.utcnow() self.execute() self.media_file.total_time = timeutil.delta_seconds( _start, timeutil.utcnow()) except Exception: self.media_file.error_msg = traceback.format_exc() self.gen_files.append(self.media_file) return self.gen_files
def clean_up(self): """Cleans up data within tables. Periodically check for data no longer needed (fully processed, invalid, or deleted) and then remove from the database. """ LOG.debug('starting perform_db_cleanup...') lpd = self.get_appstate('last_purge_date') last_purge_date = lpd.value if lpd else None LOG.debug('last_purge_date: %s', last_purge_date) if last_purge_date is None: LOG.info('First running...setting last purge date to now') # never been purged so need to set an initial date (today) self.save_appstate(models.AppState('last_purge_date', timeutil.utcnow())) return if timeutil.is_older_than(last_purge_date, timeutil.ONE_WEEK): LOG.debug('last purge > 1 week ago....ready for some clean up.') # ready to start purge process.... torrents = list(self.get_torrents_eligible_for_purging()) if torrents: LOG.debug('found torrents eligible for purging: %s', len(torrents)) # perform database backup self.backup() LOG.debug('purging media associated with torrents....') qfilter = { 'in': {'torrent_id': [tor.torrent_id for tor in torrents]} } self.delete_medias(qfilter) qfilter = { 'in': {'id': [tor.torrent_id for tor in torrents]} } self.bulk_save_torrents({'purged': True}, qfilter) # now reset the last purge date to now. self.save_appstate(models.AppState('last_purge_date', timeutil.utcnow())) else: # no torrents eligible for purging LOG.debug('no torrents found eligible for purging...') # done deleting mediafiles; now delete any torrents that were # found to be missing from filesystem thereby no longer in # need of caching. torrents_to_delete = [] for torrent in self.get_torrents_eligible_for_removal(): if not os.path.exists(os.path.join( self.impl.conf.torrent.torrent_path, torrent.name)): # actual torrent file no longer exists so we can safely # delete the torrent from cache torrents_to_delete.append(torrent.torrent_id) if torrents_to_delete: LOG.debug('found torrents eligible for removal: %s', len(torrents_to_delete)) self.delete_torrents({'in': {'id': torrents_to_delete}}) LOG.debug('perform_db_cleanup completed')
def test_is_soon(self): expires = timeutil.utcnow() + datetime.timedelta(minutes=5) self.assertFalse(timeutil.is_soon(expires, 120)) self.assertTrue(timeutil.is_soon(expires, 300)) self.assertTrue(timeutil.is_soon(expires, 600))
def test_is_newer_than(self): _future = timeutil.advance_time_seconds(timeutil.utcnow(), 10) self.assertTrue(timeutil.is_newer_than(_future, 5)) self.assertFalse(timeutil.is_newer_than(_future, 15))
def test_advance_time_seconds(self): _cur = timeutil.utcnow() _future = timeutil.advance_time_seconds(_cur, 60) self.assertThat(_future, matchers.GreaterThan(_cur))
def test_advance_time_delta(self): _cur = timeutil.utcnow() _future = timeutil.advance_time_delta(_cur, datetime.timedelta(seconds=60)) self.assertThat(_future, matchers.GreaterThan(_cur))