Skip to content

Commit

Permalink
Delayed_Job & Whenever replaces Backgroundrb
Browse files Browse the repository at this point in the history
  • Loading branch information
anupnivargi committed Aug 25, 2009
1 parent c9e67ca commit 36803d5
Show file tree
Hide file tree
Showing 156 changed files with 1,457 additions and 5,557 deletions.
4 changes: 2 additions & 2 deletions app/controllers/audios_controller.rb
Expand Up @@ -9,8 +9,8 @@ class AudiosController < ApplicationController
acts_as_item do
#Filter calling the encoder method of ConverterWorker with parameters
after :create, :update do
@current_object.update_attributes(:state=>"uploaded")
MiddleMan.worker(:converter_worker).async_newthread(:arg=>{:type=>"audio", :id => @current_object.id, :enc=>"mp3"})
Delayed::Job.enqueue(EncodingJob.new({:type=>"audio", :id => @current_object.id, :enc=>"mp3"}))
#MiddleMan.worker(:converter_worker).async_newthread(:arg=>{:type=>"audio", :id => @current_object.id, :enc=>"mp3"})
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/newsletters_controller.rb
Expand Up @@ -35,8 +35,8 @@ def send_to_a_group
QueuedMail.add("UserMailer","send_newsletter", args, 0)
end
end
MiddleMan.worker(:cronjob_worker).async_newthread
redirect_to (current_workspace ? workspace_path(current_workspace.id)+newsletter_path(@newsletter) : newsletter_path(@newsletter))
Delayed::Job.enqueue(NewsletterJob.new)
redirect_to(current_workspace ? workspace_path(current_workspace.id)+newsletter_path(@newsletter) : newsletter_path(@newsletter))
end
end

Expand Down
4 changes: 1 addition & 3 deletions app/controllers/videos_controller.rb
Expand Up @@ -9,9 +9,7 @@ class VideosController < ApplicationController
acts_as_item do
#Filter calling the encoder method of ConverterWorker with parameters
after :create, :update do
#Call the encoder method of ConverterWorker with parameters
@current_object.update_attributes(:state=>"uploaded")
MiddleMan.worker(:converter_worker).async_newthread(:arg=>{:type=>"video", :id => @current_object.id, :enc=>"flv"})
Delayed::Job.enqueue(EncodingJob.new({:type=>"video", :id => @current_object.id, :enc=>"flv"}))
end
end

Expand Down
15 changes: 15 additions & 0 deletions app/models/feed_source.rb
Expand Up @@ -116,6 +116,21 @@ def remove_expired_feed_items
end
end


def self.update_feed_source
logger.info "#{Time.now} : Updating Feed Sources ..."
FeedSource.all.each do |s|
begin
s.import_latest_items
rescue
logger.info " #{Time.now} : Error updating Feed Source #{s.id}"
end
#logger.info "Removing Expired Feed Items"
#s.remove_expired_feed_items
end
logger.info "#{Time.now} : Updated Feed sources"
end

# To implement
#
#
Expand Down
17 changes: 0 additions & 17 deletions config/backgroundrb.yml

This file was deleted.

9 changes: 9 additions & 0 deletions config/schedule.rb
@@ -0,0 +1,9 @@
every 2.minutes do
runner "FeedSource.update_feed_source"
end

every 3.minutes do
rake "xapian:update_index"
end


27 changes: 0 additions & 27 deletions db/migrate/20090119100609_create_backgroundrb_queue_table.rb

This file was deleted.

20 changes: 20 additions & 0 deletions db/migrate/20090820132534_create_delayed_jobs.rb
@@ -0,0 +1,20 @@
class CreateDelayedJobs < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, :force => true do |table|
table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
table.text :handler # YAML-encoded string of the object that will do work
table.text :last_error # reason for last failure (See Note below)
table.datetime :run_at # When to run. Could be Time.now for immediately, or sometime in the future.
table.datetime :locked_at # Set when a client is working on this object
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
table.string :locked_by # Who is working on this object (if locked)
table.timestamps
end

end

def self.down
drop_table :delayed_jobs
end
end
58 changes: 58 additions & 0 deletions lib/encoding_job.rb
@@ -0,0 +1,58 @@
class EncodingJob < Struct.new(:args)

def perform
object=args[:type].classify.constantize.find_by_id(args[:id])
success = system(convert_media(args[:type], object, args[:enc]))
if success && $?.exitstatus == 0
object.update_attributes(:state=>"encoded")
if args[:type]=="video"
i=1
j=2
pic=4
while i<=pic do
system(thumbnail(i,j,object))
i=i+1
j=j*3
end
end
else
object.update_attributes(:state=>"encoding_error")
end

end

# Method to convert the media to desired media (Default MP3 for Audio and FLV for Video)
def convert_media(type, object, enc)
media = File.join(File.dirname(object.media_type.path), "#{type}.#{enc}")
File.open(media, 'w')
if object.media_type.content_type.include?("audio/mpeg") || object.media_type.content_type.include?("video/x-flash-video") || object.media_type.content_type.include?("video/x-flv")
command=<<-end_command
cp #{ object.media_type.path } #{media}
end_command
command.gsub!(/\s+/, " ")
elsif object.media_type.content_type.include?("video/3gpp")
command = <<-end_command
ffmpeg -i #{ object.media_type.path } #{object.codec_3gp} #{ media }
end_command
command.gsub!(/\s+/, " ")
else
command = <<-end_command
ffmpeg -i #{ object.media_type.path } #{object.codec} #{ media }
end_command
command.gsub!(/\s+/, " ")
end
end

# Create Thumbnails for Video File on Particular Intervals
def thumbnail(i,j,object)
thumb = File.join(File.dirname(object.media_type.path), "#{i.to_s}.png")
File.open(thumb, 'w')
command=<<-end_command
ffmpeg -itsoffset -#{(i*j).to_s} -i #{File.dirname(object.media_type.path)}/video.flv -vcodec png -vframes 1 -an -f rawvideo -s 470x320 -y #{thumb}
end_command
command.gsub!(/\s+/, " ")
end
end



15 changes: 15 additions & 0 deletions lib/newsletter_job.rb
@@ -0,0 +1,15 @@
class NewsletterJob

def perform
command=<<-end_command
ruby script/runner QueuedMail.send_email
end_command
command.gsub!(/\s+/, " ")
if system(command)
p "#{Time.now} : Newsletter sending success#{ $?.exitstatus == 0 ? '' : ', but exit status equal to '+exitstatus.to_s }"
else
p "#{Time.now} : Newsletter sending failed"
end
end
end

110 changes: 0 additions & 110 deletions lib/workers/converter_worker.rb

This file was deleted.

59 changes: 0 additions & 59 deletions lib/workers/cronjob_worker.rb

This file was deleted.

0 comments on commit 36803d5

Please sign in to comment.