From 38b55d769fb06b5370b3745f33a83ff3ea0c0b14 Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Thu, 31 May 2012 11:02:25 +0400 Subject: [PATCH] [refs #510] Add resque async workers --- .gitignore | 1 + Gemfile | 1 + Gemfile.lock | 11 +++++++++++ Rakefile | 6 ++++++ app/models/project.rb | 20 +++++++++++++++++--- 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index ecb8b8624..ef7fd18e7 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ public/downloads/* *.swp *.tmproj .sass-cache/ +dump.rdb diff --git a/Gemfile b/Gemfile index f4bcc710b..0b4ded308 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,7 @@ gem 'cancan', '~> 1.6.7' gem 'ancestry', '~> 1.2.5' gem 'paperclip', '~> 3.0.2' gem 'delayed_job_active_record', '~> 0.3.2' +gem 'resque' gem 'russian', '~> 0.6.0' gem 'highline', '~> 1.6.11' diff --git a/Gemfile.lock b/Gemfile.lock index 4b3b34f2f..50cbcd2a7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -235,6 +235,14 @@ GEM rdoc (3.12) json (~> 1.4) redcarpet (1.17.2) + redis (2.2.2) + redis-namespace (1.0.3) + redis (< 3.0.0) + resque (1.20.0) + multi_json (~> 1.0) + redis-namespace (~> 1.0.2) + sinatra (>= 0.9.2) + vegas (~> 0.1.2) rr (1.0.4) rspec (2.9.0) rspec-core (~> 2.9.0) @@ -303,6 +311,8 @@ GEM kgio (~> 2.6) rack raindrops (~> 0.7) + vegas (0.1.11) + rack (>= 1.0.0) warden (1.1.1) rack (>= 1.0) whenever (0.7.3) @@ -356,6 +366,7 @@ DEPENDENCIES rdiscount redcarpet (= 1.17.2) redhillonrails_core! + resque rr (~> 1.0.4) rspec-rails (~> 2.9.0) ruby-haml-js (~> 0.0.3) diff --git a/Rakefile b/Rakefile index 035ccba70..ae5e4f293 100644 --- a/Rakefile +++ b/Rakefile @@ -3,5 +3,11 @@ require File.expand_path('../config/application', __FILE__) require 'rake' +require 'resque/tasks' + +# This fixes connection fail with Postgres server on new fork: +task "resque:setup" => :environment do + Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection } +end Rosa::Application.load_tasks diff --git a/app/models/project.rb b/app/models/project.rb index c6501721b..61a0f10a9 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -41,12 +41,12 @@ class Project < ActiveRecord::Base after_create :attach_to_personal_repository after_create :create_git_repo - after_create {|p| p.delay(:queue => 'fork', :priority => 20).fork_git_repo unless is_root?} + after_create {|p| p.async(:fork_git_repo) unless is_root?} after_save :create_wiki after_destroy :destroy_git_repo after_destroy :destroy_wiki - after_save {|p| p.delay(:queue => 'import', :priority => 10).import_attached_srpm if p.srpm?} # should be after create_git_repo + after_save {|p| p.async(:import_attached_srpm) if p.srpm?} # should be after create_git_repo # after_rollback lambda { destroy_git_repo rescue true if new_record? } has_ancestry @@ -55,6 +55,20 @@ class Project < ActiveRecord::Base include Modules::Models::Owner + @queue = :fork_and_import + + # This will be called by a worker when a job needs to be processed + def self.perform(id, method, *args) + find(id).send(method, *args) + end + + # We can pass this any Repository instance method that we want to + # run later. + def async(method, *args) + Resque.enqueue(Project, id, method, *args) + end + + def to_param name end @@ -252,7 +266,7 @@ class Project < ActiveRecord::Base def create_git_repo if is_root? Grit::Repo.init_bare(path) - write_hook.delay(:queue => 'fork', :priority => 15) + async(:write_hook) end end