Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add delayed_job

  • Loading branch information...
commit 317576d582375805bf0be8102c3452450859cc7c 1 parent ad153cc
@flyerhzm flyerhzm authored
View
3  Gemfile
@@ -16,6 +16,7 @@ gem 'oa-core'
gem 'twitter-bootstrap-rails'
gem 'gravtastic'
gem 'octokit'
+gem 'delayed_job'
# Gems used only for assets and not required
# in production environments by default.
@@ -44,6 +45,8 @@ group :develop do
gem 'rb-fsevent'
gem 'guard'
gem 'guard-bundler'
+ gem 'guard-rails'
+ gem 'guard-livereload'
end
group :test do
View
15 Gemfile.lock
@@ -69,11 +69,17 @@ GEM
nokogiri (>= 1.5.0)
daemons (1.1.4)
database_cleaner (0.6.7)
+ delayed_job (2.1.4)
+ activesupport (~> 3.0)
+ daemons
devise (1.4.9)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
diff-lcs (1.1.3)
+ em-websocket (0.3.5)
+ addressable (>= 2.1.1)
+ eventmachine (>= 0.12.9)
erubis (2.7.0)
eventmachine (0.12.10)
execjs (1.2.9)
@@ -99,6 +105,12 @@ GEM
guard-bundler (0.1.3)
bundler (>= 1.0.0)
guard (>= 0.2.2)
+ guard-livereload (0.3.1)
+ em-websocket (>= 0.2.0)
+ guard (>= 0.4.0)
+ multi_json (~> 1.0.3)
+ guard-rails (0.0.3)
+ guard (>= 0.2.2)
guard-rspec (0.5.5)
guard (>= 0.8.4)
guard-spork (0.3.2)
@@ -276,6 +288,7 @@ DEPENDENCIES
cucumber
cucumber-rails
database_cleaner
+ delayed_job
devise
factory_girl
factory_girl_rails
@@ -283,6 +296,8 @@ DEPENDENCIES
growl
guard
guard-bundler
+ guard-livereload
+ guard-rails
guard-rspec
guard-spork
haml
View
1  app/models/user.rb
@@ -54,4 +54,5 @@ def sync_repositories
)
end
end
+ handle_asynchronously :sync_repositories
end
View
21 db/migrate/20111126070046_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
View
17 db/schema.rb
@@ -11,7 +11,22 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111125035808) do
+ActiveRecord::Schema.define(:version => 20111126070046) 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 "repositories", :force => true do |t|
t.string "url"
View
5 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
View
3  spec/models/user_spec.rb
@@ -1,6 +1,8 @@
require 'spec_helper'
describe User do
+ include DelayedJobSpecHelper
+
it { should have_many(:user_repositories) }
it { should have_many(:repositories).through(:user_repositories) }
@@ -10,6 +12,7 @@
stub_request(:get, "https://api.github.com/user/repos").to_return(:body => repos)
@user = Factory(:user)
+ work_off
end
it "should sync user repositories" do
View
9 spec/spec_helper.rb
@@ -44,6 +44,15 @@
DatabaseCleaner.clean
end
end
+
+ module DelayedJobSpecHelper
+ def work_off
+ Delayed::Job.all.each do |job|
+ job.payload_object.perform
+ job.destroy
+ end
+ end
+ end
end
Spork.each_run do
Please sign in to comment.
Something went wrong with that request. Please try again.