Dedicated spec for testing cleaner. Some cleanup/renaming.

This commit is contained in:
Steven Miers 2014-12-15 23:57:57 -06:00
parent ff42a84598
commit 795cb6f536
5 changed files with 58 additions and 4 deletions

View File

@ -31,7 +31,7 @@ ALTER TABLE jam_track_rights
ADD COLUMN length INTEGER NOT NULL DEFAULT 0,
ADD COLUMN download_count INTEGER NOT NULL DEFAULT 0,
ADD COLUMN signed BOOLEAN NOT NULL DEFAULT FALSE,
ADD COLUMN downloaded BOOLEAN NOT NULL DEFAULT FALSE,
ADD COLUMN downloaded_since_sign BOOLEAN NOT NULL DEFAULT FALSE,
ADD COLUMN created_at timestamp without time zone NOT NULL,
ADD COLUMN updated_at timestamp without time zone NOT NULL,
ALTER COLUMN jam_track_id TYPE BIGINT USING 0,

View File

@ -50,6 +50,7 @@ module JamRuby
jam_track_right.url.store!(File.open(output_jkz))
jam_track_right.signed=true
jam_track_right.downloaded_since_sign=false
jam_track_right.save!
end
end # mktmpdir

View File

@ -36,7 +36,7 @@ module JamRuby
end
def self.ready_to_clean
JamTrackRight.where("downloaded=TRUE AND updated_at <= ?", 5.minutes.ago).limit(1000)
JamTrackRight.where("downloaded_since_sign=? AND updated_at <= ?", true, 5.minutes.ago).limit(1000)
end
@ -45,7 +45,7 @@ module JamRuby
# we would verify their rights (can_download?), and generates a URL in response to the click so that they can download
# but the url is short lived enough so that it wouldn't be easily shared
def sign_url(expiration_time = 120)
s3_manager.sign_url(self[:url], {:expires => expiration_time, :response_content_type => 'jkz', :secure => false})
s3_manager.sign_url(self[:url], {:expires => expiration_time, :secure => false})
end
def delete_s3_files

View File

@ -20,7 +20,7 @@ module JamRuby
end
def self.perform
JamTrackRight.downloaded.each do |jam_track_right|
JamTrackRight.ready_to_clean.each do |jam_track_right|
@@log.debug("deleting files for jam_track_right #{jam_track_right.id}")
jam_track_right.delete_s3_files
end

View File

@ -0,0 +1,53 @@
require 'spec_helper'
describe JamTracksCleaner do
include UsesTempFiles
include CarrierWave::Test::Matchers
RIGHT_NAME = 'abc.jkz'
in_directory_with_file(RIGHT_NAME)
before (:all) do
@user = FactoryGirl.create(:user)
@jam_track = FactoryGirl.create(:jam_track)
original_storage = JamTrackRightUploader.storage = :fog
end
after(:all) do
JamTrackRightUploader.storage = @original_storage
end
before(:each) do
content_for_file('abc')
end
it "should clean" do
jam_track_right = JamTrackRight.create(:user=>@user, :jam_track=>@jam_track)
jam_track_right.signed=true
jam_track_right
jam_track_right.url.store!(File.open(RIGHT_NAME))
jam_track_right.downloaded_since_sign=true
jam_track_right.save!
jam_track_right[:url].should == jam_track_right.store_dir + '/' + jam_track_right.filename
jam_track_right.reload
# Should exist after uploading:
url = jam_track_right[:url]
s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)
url.should_not be_nil
s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)
s3.exists?(jam_track_right[:url]).should be_true
JamRuby::JamTracksCleaner.perform
s3.exists?(url).should be_true
s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)
jam_track_right.update_attribute("updated_at", 6.minutes.ago)
# But not after running cleaner job:
JamRuby::JamTracksCleaner.perform
s3.exists?(url).should be_false
end
end