added model stuff for mix
This commit is contained in:
parent
b083f15304
commit
3abde183b7
|
|
@ -58,6 +58,7 @@ require "jam_ruby/models/user_favorite"
|
|||
require "jam_ruby/models/search"
|
||||
require "jam_ruby/models/recording"
|
||||
require "jam_ruby/models/recorded_track"
|
||||
require "jam_ruby/models/mix"
|
||||
|
||||
include Jampb
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
# FIXME:
|
||||
# Need to pass in the JSON spec for the mix and put that in the migration.
|
||||
|
||||
module JamRuby
|
||||
class Mix < ActiveRecord::Base
|
||||
MAX_MIX_TIME = 7200 # 2 hours
|
||||
|
||||
def self.schedule(recording_id, user_id, description, spec)
|
||||
# This would have made it so you couldn't have more than one mix of a recording+owner
|
||||
#raise unless self.where(:recording_id => recording_id, :owner_id => user_id).size == 0
|
||||
recording = Recording.find(recording_id)
|
||||
raise if recording.nil?
|
||||
raise if recording.owner_id != user_id
|
||||
|
||||
mix = Mix.new
|
||||
mix.recording_id = recording_id
|
||||
mix.owner_id = user_id
|
||||
mix.description = description
|
||||
mix.spec = spec
|
||||
mix.save
|
||||
|
||||
mix
|
||||
end
|
||||
|
||||
def self.next(mix_server)
|
||||
# First check if there are any mixes started so long ago that we want to re-run them
|
||||
Mix.where("completed_at IS NULL AND started_at < ?", Time.now - MAX_MIX_TIME).each do |mix|
|
||||
mix.started_at = nil
|
||||
mix.mix_server = nil
|
||||
mix.save
|
||||
end
|
||||
|
||||
mix = Mix.where(:started_at => nil).limit(1).first
|
||||
return nil if mix.nil?
|
||||
|
||||
mix.started_at = Time.now
|
||||
mix.mix_server = mix_server
|
||||
mix.save
|
||||
|
||||
mix
|
||||
end
|
||||
|
||||
|
||||
def finish(url)
|
||||
self.completed_at = Time.now
|
||||
self.url = url
|
||||
save
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Mix do
|
||||
before do
|
||||
@user = FactoryGirl.create(:user)
|
||||
@connection = FactoryGirl.create(:connection, :user => @user)
|
||||
@instrument = FactoryGirl.create(:instrument, :description => 'a great instrument')
|
||||
@track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument)
|
||||
@music_session = FactoryGirl.create(:music_session, :creator => @user, :musician_access => true)
|
||||
@music_session.connections << @connection
|
||||
@music_session.save
|
||||
@recording = Recording.start(@music_session.id, @user)
|
||||
@recording.stop
|
||||
@mix = Mix.schedule(@recording.id, @user.id, "description", "{}")
|
||||
end
|
||||
|
||||
it "should create a mix for a user's recording properly" do
|
||||
@mix.recording_id.should == @recording.id
|
||||
@mix.owner_id.should == @user.id
|
||||
@mix.description.should == "description"
|
||||
@mix.spec.should == "{}"
|
||||
@mix.url.should be_nil
|
||||
@mix.mix_server.should be_nil
|
||||
@mix.started_at.should be_nil
|
||||
@mix.completed_at.should be_nil
|
||||
end
|
||||
|
||||
it "should fail to create a mix if the userid doesn't own the recording" do
|
||||
@user2 = FactoryGirl.create(:user)
|
||||
expect { Mix.schedule(@recording.id, @user2.id) }.to raise_error
|
||||
end
|
||||
|
||||
it "should fail if the recording doesn't exist" do
|
||||
expect { @mix2 = Mix.schedule("bad_recording_id", @user.id) }.to raise_error
|
||||
end
|
||||
|
||||
it "should return a mix when the cron asks for it" do
|
||||
this_mix = Mix.next("server")
|
||||
this_mix.id.should == @mix.id
|
||||
@mix.reload
|
||||
@mix.started_at.should_not be_nil
|
||||
@mix.mix_server.should == "server"
|
||||
@mix.completed_at.should be_nil
|
||||
end
|
||||
|
||||
it "should record when a mix has finished" do
|
||||
Mix.find(@mix.id).finish("http://blah")
|
||||
@mix.reload
|
||||
@mix.completed_at.should_not be_nil
|
||||
@mix.url.should == "http://blah"
|
||||
end
|
||||
|
||||
it "should re-run a mix if it was started a long time ago" do
|
||||
this_mix = Mix.next("server")
|
||||
@mix.reload
|
||||
@mix.started_at -= 1000000
|
||||
@mix.save
|
||||
this_mix = Mix.next("server")
|
||||
this_mix.id.should == @mix.id
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue