jam-cloud/lib/jam_ruby/models/mix.rb

52 lines
1.3 KiB
Ruby

# 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