From e889f2366065fdca3a0f09c3364717fa8fa07c69 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 10 Feb 2014 23:36:10 +0400 Subject: [PATCH] Configure resque-scheduler gem --- Gemfile | 4 ++- Gemfile.lock | 25 +++++++++++---- Rakefile | 1 + app/controllers/api/v1/jobs_controller.rb | 1 + app/models/build_list.rb | 6 +++- config/initializers/resque.rb | 2 ++ lib/recipes/resque.rb | 39 +++++++++++++++++++++++ 7 files changed, 70 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index f4848b1d3..fe6643670 100644 --- a/Gemfile +++ b/Gemfile @@ -18,7 +18,9 @@ gem 'paperclip', '~> 3.3.1' gem 'resque', '~> 1.24' gem 'resque-status', '~> 0.3.3' gem 'resque_mailer', '~> 2.2' -gem 'perform_later', '~> 1.3.0' # should be after resque_mailer +gem 'resque-scheduler' +# gem 'perform_later', '~> 1.3.0' # should be after resque_mailer +gem 'perform_later', git: 'git://github.com/KensoDev/perform_later.git' # should be after resque_mailer gem 'russian', '~> 0.6.0' gem 'highline', '~> 1.6.11' gem 'state_machine' diff --git a/Gemfile.lock b/Gemfile.lock index 1217a7b9e..4fb742a89 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,13 @@ +GIT + remote: git://github.com/KensoDev/perform_later.git + revision: afd40794aa7101c8e73e9f105a064dd9bec47e31 + specs: + perform_later (1.3.0) + rails (>= 3.0) + redis + resque (> 1.25) + resque-scheduler + GIT remote: git://github.com/rosa-abf/grack.git revision: 020be3fef3fb308b9d214252522aa5945bf6584a @@ -259,10 +269,6 @@ GEM activesupport (>= 3.0.0) cocaine (~> 0.4.0) mime-types - perform_later (1.3.0) - rails (~> 3.0) - redis - resque pg (0.14.1) polyglot (0.3.3) posix-spawn (0.3.8) @@ -307,7 +313,7 @@ GEM rdoc (3.12.2) json (~> 1.4) redcarpet (2.2.2) - redis (3.0.6) + redis (3.0.7) redis-actionpack (3.2.4) actionpack (~> 3.2.0) redis-rack (~> 1.4.4) @@ -335,6 +341,10 @@ GEM redis-namespace (~> 1.2) sinatra (>= 0.9.2) vegas (~> 0.1.2) + resque-scheduler (2.5.1) + redis (>= 3.0.0) + resque (~> 1.25) + rufus-scheduler (~> 2.0) resque-status (0.3.3) redisk (>= 0.2.1) resque (~> 1.19) @@ -364,6 +374,8 @@ GEM rubypython (0.5.3) blankslate (>= 2.1.2.3) ffi (~> 1.0.7) + rufus-scheduler (2.0.24) + tzinfo (>= 0.3.22) russian (0.6.0) i18n (>= 0.5.0) rvm-capistrano (1.5.1) @@ -493,7 +505,7 @@ DEPENDENCIES omniauth-github omniauth-google-oauth2 paperclip (~> 3.3.1) - perform_later (~> 1.3.0) + perform_later! pg (~> 0.14.0) puma rack-throttle @@ -506,6 +518,7 @@ DEPENDENCIES redhillonrails_core! redis-rails resque (~> 1.24) + resque-scheduler resque-status (~> 0.3.3) resque_mailer (~> 2.2) rest-client (~> 1.6.6) diff --git a/Rakefile b/Rakefile index ae5e4f293..31c005551 100644 --- a/Rakefile +++ b/Rakefile @@ -4,6 +4,7 @@ require File.expand_path('../config/application', __FILE__) require 'rake' require 'resque/tasks' +require 'resque_scheduler/tasks' # This fixes connection fail with Postgres server on new fork: task "resque:setup" => :environment do diff --git a/app/controllers/api/v1/jobs_controller.rb b/app/controllers/api/v1/jobs_controller.rb index f3a9ef152..cd91a7469 100644 --- a/app/controllers/api/v1/jobs_controller.rb +++ b/app/controllers/api/v1/jobs_controller.rb @@ -16,6 +16,7 @@ class Api::V1::JobsController < Api::V1::BaseController if current_user.system? if task = (Resque.pop('rpm_worker_default') || Resque.pop('rpm_worker')) @build_list = BuildList.where(id: task['args'][0]['id']).first + @build_list.delayed_add_job_to_abf_worker_queue end end diff --git a/app/models/build_list.rb b/app/models/build_list.rb index d00131977..f1242996f 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -258,7 +258,6 @@ class BuildList < ActiveRecord::Base later :publish, queue: :clone_build later :add_job_to_abf_worker_queue, queue: :clone_build - HUMAN_CONTAINER_STATUSES = { WAITING_FOR_RESPONSE => :waiting_for_publish, BUILD_PUBLISHED => :container_published, BUILD_PUBLISH => :container_publish, @@ -524,6 +523,11 @@ class BuildList < ActiveRecord::Base ) end + def delayed_add_job_to_abf_worker_queue + now_add_job_to_abf_worker_queue if status == BUILD_PENDING + end + later :delayed_add_job_to_abf_worker_queue, delay: 60, queue: :clone_build + protected def create_container diff --git a/config/initializers/resque.rb b/config/initializers/resque.rb index f3e8d36bb..f2c69e5c3 100644 --- a/config/initializers/resque.rb +++ b/config/initializers/resque.rb @@ -1,5 +1,7 @@ require 'resque/server' require 'resque/status_server' +require 'resque_scheduler' +require 'resque_scheduler/server' ::ResqueServer = ::Resque::Server # need for CanCan diff --git a/lib/recipes/resque.rb b/lib/recipes/resque.rb index 4e2952f54..97f03ca7d 100644 --- a/lib/recipes/resque.rb +++ b/lib/recipes/resque.rb @@ -3,15 +3,18 @@ Capistrano::Configuration.instance(:must_exist).load do namespace :resque do task :start do start_workers + invoke "resque:scheduler:start" end task :stop do stop_workers + invoke "resque:scheduler:stop" end task :restart do stop_workers start_workers + invoke "resque:scheduler:restart" end def rails_env @@ -36,5 +39,41 @@ Capistrano::Configuration.instance(:must_exist).load do ].join(',') run "cd #{fetch :current_path} && COUNT=#{workers_count} QUEUE=#{queue} #{rails_env} BACKGROUND=yes bundle exec rake resque:workers" end + + def remote_file_exists?(full_path) + 'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip + end + + namespace :scheduler do + + desc "See current scheduler status" + task :status do + pid = "#{fetch :current_path}/tmp/pids/scheduler.pid" + if remote_file_exists?(pid) + info capture(:ps, "-f -p $(cat #{pid}) | sed -n 2p") + end + end + + desc "Starts resque scheduler with default configs" + task :start do + pid = "#{fetch :current_path}/tmp/pids/scheduler.pid" + run "cd #{fetch :current_path} && #{rails_env} PIDFILE=#{pid} BACKGROUND=yes VERBOSE=1 MUTE=1 bundle exec rake resque:scheduler" + end + + desc "Stops resque scheduler" + task :stop do + pid = "#{fetch :current_path}/tmp/pids/scheduler.pid" + if remote_file_exists?(pid) + run "cd #{fetch :current_path} && kill -s QUIT $(cat #{pid}); rm #{pid}" + end + end + + task :restart do + invoke "resque:scheduler:stop" + invoke "resque:scheduler:start" + end + end + end + end