Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Moving storage_expiration_seconds definition to child class
then we can change this value depends on implementation. Like thumbor
do.
  • Loading branch information
Andre Fonseca committed Nov 9, 2016
1 parent 25bffc8 commit 4df9b6e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tc_aws/aws/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ def is_expired(self, key):
:rtype: bool
"""
if key and self._get_error(key) is None and 'LastModified' in key:
expire_in_seconds = self.context.config.get('STORAGE_EXPIRATION_SECONDS', 3600)
expire_in_seconds = self.storage_expiration_seconds

# Never expire
if expire_in_seconds is None or expire_in_seconds == 0:
return False

timediff = datetime.now(tzutc()) - key['LastModified']

return timediff.seconds > self.context.config.get('STORAGE_EXPIRATION_SECONDS', 3600)
return timediff.seconds > expire_in_seconds
else:
#If our key is bad just say we're expired
return True
Expand Down
1 change: 1 addition & 0 deletions tc_aws/result_storages/s3_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, context):
"""
BaseStorage.__init__(self, context)
AwsStorage.__init__(self, context, 'TC_AWS_RESULT_STORAGE')
self.storage_expiration_seconds = context.config.get('RESULT_STORAGE_EXPIRATION_SECONDS', 3600)

@return_future
def put(self, bytes, callback=None):
Expand Down
1 change: 1 addition & 0 deletions tc_aws/storages/s3_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, context):
"""
BaseStorage.__init__(self, context)
AwsStorage.__init__(self, context, 'TC_AWS_STORAGE')
self.storage_expiration_seconds = context.config.get('STORAGE_EXPIRATION_SECONDS', 3600)

@return_future
def put(self, path, bytes, callback=None):
Expand Down
37 changes: 37 additions & 0 deletions vows/result_storage_vows.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#se!/usr/bin/python
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
from dateutil.tz import tzutc

from pyvows import Vows, expect

Expand Down Expand Up @@ -90,6 +92,41 @@ def should_have_proper_bytes(self, topic):
expect(topic.args[0].metadata).to_include('some-other-header')
expect(topic.args[0].content_type).to_equal(IMAGE_BYTES)

class ExpiredVows(Vows.Context):
class WhenExpiredEnabled(Vows.Context):
def topic(self):
return Storage(Context(config=Config(RESULT_STORAGE_EXPIRATION_SECONDS=3600)))

def should_check_invalid_key(self, topic):
expect(topic.is_expired(None)).to_be_true()
expect(topic.is_expired(False)).to_be_true()
expect(topic.is_expired(dict())).to_be_true()
expect(topic.is_expired({'Error': ''})).to_be_true()

def should_tell_when_not_expired(self, topic):
key = {
'LastModified': datetime.now(tzutc()),
'Body': 'foobar',
}
expect(topic.is_expired(key)).to_be_false()

def should_tell_when_expired(self, topic):
key = {
'LastModified': (datetime.now(tzutc()) - timedelta(seconds=3601)),
'Body': 'foobar',
}
expect(topic.is_expired(key)).to_be_true()

class WhenExpiredDisabled(Vows.Context):
def topic(self):
return Storage(Context(config=Config(RESULT_STORAGE_EXPIRATION_SECONDS=0)))

def should_not_tell_when_expired(self, topic):
key = {
'LastModified': (datetime.now(tzutc()) - timedelta(seconds=3601)),
'Body': 'foobar',
}
expect(topic.is_expired(key)).to_be_false()
class HandlesStoragePrefix(Vows.Context):
@mock_s3
def topic(self):
Expand Down

0 comments on commit 4df9b6e

Please sign in to comment.