def update_expiring_files(): """Updates file entries that expires in an hour.""" next_hour = datetime.utcnow() + timedelta(hours=1) unistorage = UnistorageClient(settings.UNISTORAGE_URL, settings.UNISTORAGE_ACCESS_TOKEN) for db_file in File.query.filter(File.unistorage_valid_until != None, File.unistorage_valid_until <= next_hour).all(): try: unistorage_file = unistorage.get_file(db_file.unistorage_resource_uri) if isinstance(unistorage_file, UnistoragePendingFile): raise UnexpectedFileException() if isinstance(unistorage_file, UnistorageRegularFile): db_file.unistorage_valid_until = None else: db_file.unistorage_valid_until = datetime.utcnow() + \ timedelta(seconds=unistorage_file.ttl) db_file.unistorage_url = unistorage_file.url db.session.add(db_file) db.session.commit() except (UnistorageError, UnistorageTimeout, UnexpectedFileException) as e: print str(e)