From 3abde183b745e45b14decf6c533ccf409c8dabc7 Mon Sep 17 00:00:00 2001 From: Mike Slemmer Date: Fri, 12 Apr 2013 15:36:05 -0700 Subject: [PATCH] added model stuff for mix --- lib/jam_ruby.rb | 1 + lib/jam_ruby/models/mix.rb | 51 +++++++++++++++++++++++++ spec/jam_ruby/models/mix_spec.rb | 64 ++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 lib/jam_ruby/models/mix.rb create mode 100644 spec/jam_ruby/models/mix_spec.rb diff --git a/lib/jam_ruby.rb b/lib/jam_ruby.rb index 2bcc3471e..6f9063e1b 100644 --- a/lib/jam_ruby.rb +++ b/lib/jam_ruby.rb @@ -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 diff --git a/lib/jam_ruby/models/mix.rb b/lib/jam_ruby/models/mix.rb new file mode 100644 index 000000000..a05e4e23d --- /dev/null +++ b/lib/jam_ruby/models/mix.rb @@ -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 diff --git a/spec/jam_ruby/models/mix_spec.rb b/spec/jam_ruby/models/mix_spec.rb new file mode 100644 index 000000000..5e7753762 --- /dev/null +++ b/spec/jam_ruby/models/mix_spec.rb @@ -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 + +