Permalink
Browse files

State changes working manually in do_download.

  • Loading branch information...
1 parent f4f77a3 commit 614586b60d794e9fd33843b51c076c42ced90c22 dave committed Oct 28, 2010
Showing with 37 additions and 7 deletions.
  1. +2 −0 lib/enigmamachine.rb
  2. +1 −0 lib/enigmamachine/download_queue.rb
  3. +34 −7 lib/enigmamachine/models/video.rb
View
2 lib/enigmamachine.rb
@@ -95,6 +95,8 @@ class EnigmaMachine < Sinatra::Base
# main Sinatra/thin thread once the periodic timer is added.
#
configure do
+ puts "resetting downloading videos"
+ Video.reset_downloading_videos
Video.reset_encoding_videos
Thread.new do
until EM.reactor_running?
View
1 lib/enigmamachine/download_queue.rb
@@ -17,6 +17,7 @@ def download_next_video
if Video.waiting_for_download.count > 0 && Video.downloading.count == 0
video = Video.waiting_for_download.first
begin
+ puts "downloading video #{video.id}"
video.download!
rescue Exception => ex
# don't do anything just yet, until we set up logging properly.
View
41 lib/enigmamachine/models/video.rb
@@ -55,6 +55,10 @@ class Video
transition :from => :encoding, :to => :unencoded
end
+ event :reset_download do
+ transition :from => :downloading, :to => :waiting_for_download
+ end
+
end
# Validations
@@ -125,6 +129,13 @@ def self.reset_encoding_videos
end
end
+ def self.reset_downloading_videos
+ Video.downloading.each do |video|
+ puts "resetting video #{video.id}"
+ video.reset_download!
+ end
+ end
+
private
# Validation checks for files - we want to ensure that the video file exists,
@@ -181,9 +192,9 @@ def do_encode
#
def ffmpeg(task)
current_task_index = encoder.encoding_tasks.index(task)
- movie = FFMPEG::Movie.new(file)
+ movie = FFMPEG::Movie.new(file_to_encode)
encoding_operation = proc {
- movie.transcode(file + task.output_file_suffix, task.command)
+ movie.transcode(file_to_encode + task.output_file_suffix, task.command)
}
completion_callback = proc {|result|
if task == encoder.encoding_tasks.last
@@ -224,20 +235,24 @@ def notify_complete
def do_download
self.state = "downloading"
self.save!
+
+ FileUtils.rm(file_to_encode, :force => true)
+ puts "creating #{File.dirname(file_to_encode)}"
+ FileUtils.mkdir_p(File.dirname(file_to_encode))
http = EventMachine::HttpRequest.new(file).get :timeout => 10
http.stream do |data|
- filename = File.basename(URI.parse(file).path)
- outfile = File.join(Dir.pwd, "downloads", self.id.to_s, filename)
- File.open(outfile, 'a') {|f| f.write(data) }
+ File.open(file_to_encode, 'a') {|f| f.write(data) }
end
http.callback do
- download_complete!
+ self.state = "unencoded"
+ self.save!
end
http.errback do
- download_error!
+ self.state = "download_error"
+ self.save!
end
end
@@ -248,5 +263,17 @@ def local?
return true
end
+ # If the file is local, this just returns its location. If it's not local,
+ # it builds a path to the file based on a standard location and returns that.
+ #
+ def file_to_encode
+ if local?
+ return file
+ else
+ filename = File.basename(URI.parse(file).path)
+ return File.join(Dir.pwd, "downloads", self.id.to_s, filename)
+ end
+ end
+
end

0 comments on commit 614586b

Please sign in to comment.