Skip to content

Commit

Permalink
Delay to post feed to facebook with Delayed::Job
Browse files Browse the repository at this point in the history
  • Loading branch information
mataki committed Apr 25, 2011
1 parent 51ea3d7 commit ab9a198
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -30,6 +30,7 @@ gem 'json' # used rest-graph
gem "amazon-ecs"
gem "carrierwave"
gem "fog"
gem "delayed_job"

# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Expand Up @@ -42,6 +42,10 @@ GEM
carrierwave (0.5.3)
activesupport (~> 3.0)
columnize (0.3.2)
daemons (1.1.3)
delayed_job (2.1.4)
activesupport (~> 3.0)
daemons
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
Expand Down Expand Up @@ -137,6 +141,7 @@ PLATFORMS
DEPENDENCIES
amazon-ecs
carrierwave
delayed_job
fog
haml
haml-rails
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/mangas_controller.rb
Expand Up @@ -17,7 +17,7 @@ def create
end
begin
@manga.save!
@manga.post_link_to_facebook
@manga.delay.post_link_to_facebook
redirect_to mangas_url, :notice => "Successfully created manga."
rescue ActiveRecord::RecordInvalid => e
redirect_to new_manga_path(:query => params[:query]), :notice => @manga.errors.full_messages
Expand Down
1 change: 1 addition & 0 deletions config/initializers/delaeyd_job_config.rb
@@ -0,0 +1 @@
Delayed::Worker.delay_jobs = !(Rails.env.development? or Rails.env.test?)
21 changes: 21 additions & 0 deletions db/migrate/20110425102025_create_delayed_jobs.rb
@@ -0,0 +1,21 @@
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.zone.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

add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
end

def self.down
drop_table :delayed_jobs
end
end
17 changes: 16 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,22 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110418163704) do
ActiveRecord::Schema.define(:version => 20110425102025) do

create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
t.integer "attempts", :default => 0
t.text "handler"
t.text "last_error"
t.datetime "run_at"
t.datetime "locked_at"
t.datetime "failed_at"
t.string "locked_by"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"

create_table "mangas", :force => true do |t|
t.string "title"
Expand Down
5 changes: 5 additions & 0 deletions script/delayed_job
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
Delayed::Command.new(ARGV).daemonize

0 comments on commit ab9a198

Please sign in to comment.