Permalink
Browse files

Thumbnails now processed in the background

  • Loading branch information...
bamnet committed Jul 2, 2009
1 parent 41b7026 commit e4940ed37b09a5a9242cfd9b6ff5578529e30591
@@ -45,6 +45,7 @@ def create
respond_to do |format|
if @thumbnail.save
MiddleMan.worker(:thumbnail_worker).enq_queue_thumbnail(:args => {:thumbnail_id => @thumbnail.id}, :job_key => @thumbnail.id)
flash[:notice] = 'Thumbnail was successfully created.'
format.html { redirect_to(video_thumbnail_path(@thumbnail.video, @thumbnail)) }
format.xml { render :xml => @thumbnail, :status => :created, :location => @thumbnail }
View
@@ -9,18 +9,4 @@ class Thumbnail < ActiveRecord::Base
default_scope select_without_file_columns_for(:image)
def before_save
if self.time > self.video.duration
return false
end
tempfile = Tempfile.new(File.basename(self.video.asset_file_name, File.extname(self.video.asset_file_name)) + ".jpg")
command = "ffmpeg -i #{self.video.asset.path} -y -deinterlace -f image2 -ss #{self.time} #{tempfile.path}"
logger.info(command)
system command
if tempfile.size > 0
self.image = tempfile
else
return false
end
end
end
@@ -8,6 +8,11 @@
<%=h @thumbnail.timecode %>
</p>
<p>
<b>Status:</b>
<%=h @thumbnail.status %>
</p>
<%= image_tag @thumbnail.image.url %>
<br />
@@ -0,0 +1,9 @@
class AddStatusToThumbnails < ActiveRecord::Migration
def self.up
add_column :thumbnails, :status, :string
end
def self.down
remove_column :thumbnails, :status
end
end
View
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20090622171016) do
ActiveRecord::Schema.define(:version => 20090702150415) do
create_table "bdrb_job_queues", :force => true do |t|
t.text "args"
@@ -62,6 +62,7 @@
t.binary "image_file", :limit => 2147483647
t.binary "image_small_file", :limit => 2147483647
t.binary "image_thumb_file", :limit => 2147483647
t.string "status"
end
create_table "videos", :force => true do |t|
@@ -0,0 +1,37 @@
class ThumbnailWorker < BackgrounDRb::MetaWorker
set_worker_name :thumbnail_worker
pool_size 2
def create(args = nil)
logger.info("Creating thumbnail worker")
end
#Queue the request for a thumbnail
def queue_thumbnail(args)
thumbnail = Thumbnail.find(args[:thumbnail_id])
thumbnail.update_attributes({:status => "queued"})
thread_pool.defer(:generate,args)
end
#Generate a thumbnail
def generate(args = nil)
logger.info("Calling generate method for thumbnail #{args[:thumbnail_id]}.")
thumbnail = Thumbnail.find(args[:thumbnail_id])
if thumbnail.time > thumbnail.video.duration
thumbnail.update_attributes({:status => "failed"})
return false
end
thumbnail.update_attributes({:status => "active"})
tempfile = Tempfile.new(File.basename(thumbnail.video.asset_file_name, File.extname(thumbnail.video.asset_file_name)) + ".jpg")
command = "ffmpeg -i #{thumbnail.video.asset.path} -y -deinterlace -f image2 -ss #{thumbnail.time} #{tempfile.path}"
logger.info(command)
system command
if tempfile.size > 0
thumbnail.image = tempfile
thumbnail.status = "complete"
thumbnail.save
else
thumbnail.update_attributes({:status => "failed"})
return false
end
persistent_job.finish!
end
end

0 comments on commit e4940ed

Please sign in to comment.