module JamRuby class Mix < ActiveRecord::Base include S3ManagerMixin MAX_MIX_TIME = 7200 # 2 hours before_destroy :delete_s3_files self.primary_key = 'id' belongs_to :recording, :class_name => "JamRuby::Recording", :inverse_of => :mixes def self.schedule(recording, manifest) raise if recording.nil? raise if manifest.nil? mix = Mix.new mix.recording = recording mix.manifest = manifest.to_json mix.save mix.url = construct_filename(recording.id, mix.id) if mix.save Resque.enqueue(AudioMixer, mix.id, mix.sign_put) end mix end def errored(reason, detail) self.error_reason = reason self.error_detail = detail self.error_count = self.error_count + 1 save end def finish(length, md5) self.completed_at = Time.now self.length = length self.md5 = md5 self.completed = true save end def s3_url s3_manager.s3_url(filename) end def is_completed completed end def sign_url(expiration_time = 120) # expire link in 1 minute--the expectation is that a client is immediately following this link s3_manager.sign_url(filename, {:expires => expiration_time, :response_content_type => 'audio/ogg', :secure => false}) end def sign_put(expiration_time = 3600 * 24) s3_manager.sign_url(filename, {:expires => expiration_time, :content_type => 'audio/ogg', :secure => false}, :put) end private def delete_s3_files s3_manager.delete(filename) end def filename # construct a path for s3 Mix.construct_filename(self.recording.id, self.id) end def self.construct_filename(recording_id, id) raise "unknown ID" unless id "recordings/#{recording_id}/mix-#{id}.ogg" end end end