Skip to content

Commit

Permalink
Use Resque instead of DelayedJob
Browse files Browse the repository at this point in the history
https://trello.com/c/adsxS8Sv/197-use-resque

* Create JobQueue to abstract queue choice
  • Loading branch information
salbertson committed Apr 21, 2014
1 parent 7a61c26 commit 95edf32
Show file tree
Hide file tree
Showing 42 changed files with 365 additions and 221 deletions.
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ source 'https://rubygems.org'

ruby '2.0.0'

gem 'active_model_serializers'
gem 'angularjs-rails'
gem 'bourbon'
gem 'coffee-rails'
gem 'dalli'
gem 'delayed_job_active_record'
gem 'faraday-http-cache'
gem 'font-awesome-rails'
gem 'haml-rails'
Expand All @@ -19,6 +19,9 @@ gem 'octokit'
gem 'omniauth-github'
gem 'pg'
gem 'rails', '4.0.4'
gem 'resque', '~> 1.22.0'
gem 'resque-retry'
gem 'resque-sentry'
gem 'rubocop'
gem 'sass-rails', '~> 4.0.2'
gem 'sentry-raven'
Expand Down
40 changes: 34 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ GEM
erubis (~> 2.7.0)
rack (~> 1.5.2)
rack-test (~> 0.6.2)
active_model_serializers (0.8.1)
activemodel (>= 3.0)
activemodel (4.0.4)
activesupport (= 4.0.4)
builder (~> 3.1.0)
Expand Down Expand Up @@ -61,11 +63,6 @@ GEM
debugger-ruby_core_source (~> 1.3.1)
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.3.1)
delayed_job (4.0.0)
activesupport (>= 3.0, < 4.1)
delayed_job_active_record (4.0.0)
activerecord (>= 3.0, < 4.1)
delayed_job (>= 3.0, < 4.1)
diff-lcs (1.2.4)
dotenv (0.9.0)
erubis (2.7.0)
Expand Down Expand Up @@ -144,6 +141,8 @@ GEM
polyglot (0.3.4)
powerpack (0.0.9)
rack (1.5.2)
rack-protection (1.5.3)
rack
rack-test (0.6.2)
rack (>= 1.0)
rails (4.0.4)
Expand All @@ -167,6 +166,24 @@ GEM
rainbow (2.0.0)
raindrops (0.12.0)
rake (10.1.1)
redis (3.0.7)
redis-namespace (1.4.1)
redis (~> 3.0.4)
resque (1.22.0)
multi_json (~> 1.0)
redis-namespace (~> 1.0)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
resque-retry (1.0.0)
resque (>= 1.10.0)
resque-scheduler (>= 1.9.9)
resque-scheduler (2.2.0)
redis (>= 3.0.0)
resque (>= 1.20.0, < 1.25)
rufus-scheduler (~> 2.0)
resque-sentry (1.2.0)
resque (>= 1.18.0)
sentry-raven (>= 0.4.6)
rspec-core (2.14.7)
rspec-expectations (2.14.3)
diff-lcs (>= 1.1.3, < 2.0)
Expand All @@ -185,6 +202,8 @@ GEM
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.4)
ruby-progressbar (1.4.2)
rufus-scheduler (2.0.24)
tzinfo (>= 0.3.22)
safe_yaml (0.9.7)
sass (3.2.17)
sass-rails (4.0.2)
Expand All @@ -201,6 +220,10 @@ GEM
uuidtools
shoulda-matchers (2.4.0)
activesupport (>= 3.0.0)
sinatra (1.4.5)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
slop (3.5.0)
sprockets (2.11.0)
hike (~> 1.2)
Expand All @@ -227,6 +250,8 @@ GEM
rack
raindrops (~> 0.7)
uuidtools (2.1.4)
vegas (0.1.11)
rack (>= 1.0.0)
webmock (1.15.2)
addressable (>= 2.2.7)
crack (>= 0.3.2)
Expand All @@ -237,6 +262,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers
angularjs-rails
bourbon
capybara (~> 2.1.0)
Expand All @@ -245,7 +271,6 @@ DEPENDENCIES
dalli
database_cleaner
debugger
delayed_job_active_record
factory_girl_rails
faraday-http-cache
font-awesome-rails
Expand All @@ -262,6 +287,9 @@ DEPENDENCIES
pg
rails (= 4.0.4)
rails_12factor
resque (~> 1.22.0)
resque-retry
resque-sentry
rspec-rails (>= 2.14)
rubocop
sass-rails (~> 4.0.2)
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: bundle exec rake jobs:work
resque: env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=8 bundle exec rake resque:work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
App.controller 'ReposController', ['$scope', '$timeout', 'Repo', 'Sync', ($scope, $timeout, Repo, Sync) ->
App.controller 'ReposController', ['$scope', '$timeout', 'Repo', 'Sync', 'User', ($scope, $timeout, Repo, Sync, User) ->
loadRepos = ->
$scope.syncingRepos = false

Expand All @@ -15,8 +15,8 @@ App.controller 'ReposController', ['$scope', '$timeout', 'Repo', 'Sync', ($scope
$scope.sync()

pollSyncStatus = ->
successfulSync = (results) ->
if results.length > 0
successfulSync = (user) ->
if user.refreshing_repos
pollSyncStatus()
else
loadRepos()
Expand All @@ -25,8 +25,8 @@ App.controller 'ReposController', ['$scope', '$timeout', 'Repo', 'Sync', ($scope
pollSyncStatus()

getSyncs = ->
syncs = Sync.query()
syncs.$promise.then(successfulSync, failedSync)
user = User.get()
user.$promise.then(successfulSync, failedSync)

$timeout getSyncs, 3000

Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/services/user.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
App.factory 'User', ['$resource', ($resource) ->
$resource '/user'
]
33 changes: 9 additions & 24 deletions app/controllers/builds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ class BuildsController < ApplicationController
skip_before_filter :verify_authenticity_token, only: [:create]
skip_before_filter :authenticate, only: [:create]

HIGH_PRIORITY = 1
LOW_PRIORITY = 2

def create
Delayed::Job.enqueue(build_job, priority: priority)
JobQueue.push(build_job_class, payload.data)
head :ok
end

Expand All @@ -17,33 +14,21 @@ def force_https?
false
end

def build_job
BuildJob.new(build_runner)
end

def build_runner
BuildRunner.new(payload)
end

def payload
Payload.new(event_data)
end

def event_data
JSON.parse(params[:payload] || request.raw_post)
end

def ignore_confirmation_pings
if event_data.key?('zen')
if payload.ping?
head :ok
end
end

def priority
def build_job_class
if payload.changed_files < ENV['CHANGED_FILES_THRESHOLD'].to_i
HIGH_PRIORITY
SmallBuildJob
else
LOW_PRIORITY
LargeBuildJob
end
end

def payload
@payload ||= Payload.new(params[:payload] || request.raw_post)
end
end
19 changes: 5 additions & 14 deletions app/controllers/repo_syncs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
class RepoSyncsController < ApplicationController
respond_to :json

def index
syncs = Delayed::Job.uncached do
Delayed::Job.where(<<-SQL)
handler like '%RepoSynchronizationJob%'
and handler like '%user_id: #{current_user.id}%'
and failed_at IS NULL
SQL
end

respond_with syncs
end

def create
sync_job = RepoSynchronizationJob.new(current_user.id, session[:github_token])
Delayed::Job.enqueue(sync_job)
JobQueue.push(
RepoSynchronizationJob,
current_user.id,
session[:github_token]
)
head 201
end
end
7 changes: 7 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class UsersController < ApplicationController
respond_to :json

def show
respond_with current_user
end
end
20 changes: 0 additions & 20 deletions app/jobs/build_job.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/jobs/buildable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'resque'

module Buildable
def perform(payload_data)
payload = Payload.new(payload_data)
build_runner = BuildRunner.new(payload)
build_runner.run
rescue Resque::TermException
Resque.enqueue(self, payload_data)
end
end
8 changes: 8 additions & 0 deletions app/jobs/large_build_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'octokit'

class LargeBuildJob
extend Retryable
extend Buildable

@queue = :low
end
13 changes: 0 additions & 13 deletions app/jobs/monitorable.rb

This file was deleted.

16 changes: 13 additions & 3 deletions app/jobs/repo_synchronization_job.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
class RepoSynchronizationJob < Struct.new(:user_id, :github_token)
include Monitorable
class RepoSynchronizationJob
extend Retryable

def perform
@queue = :high

def self.before_enqueue(user_id, github_token)
user = User.find(user_id)
user.update_attribute(:refreshing_repos, true)
end

def self.perform(user_id, github_token)
user = User.find(user_id)
synchronization = RepoSynchronization.new(user, github_token)
synchronization.start
user.update_attribute(:refreshing_repos, false)
rescue Resque::TermException
Resque.enqueue(self, user_id, github_token)
end
end
9 changes: 9 additions & 0 deletions app/jobs/retryable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'resque-retry'

module Retryable
extend Resque::Plugins::Retry

@retry_limit = 10
@retry_delay = 120
@fatal_exceptions = [Octokit::NotFound, Octokit::Unauthorized]
end
8 changes: 8 additions & 0 deletions app/jobs/small_build_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'octokit'

class SmallBuildJob
extend Retryable
extend Buildable

@queue = :medium
end
Loading

0 comments on commit 95edf32

Please sign in to comment.