Skip to content

Commit

Permalink
Resque run as daemon with script/resque
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Nov 4, 2011
1 parent 35c4d7f commit 279ec07
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.bundle
db/*.sqlite3
log/*.log
log/*.log*
tmp/**/*
.sass-cache/
public/assets/
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ gem "resque", "~> 1.19.0", :require => "resque/server"
gem "resque_mailer", '2.0.2'
gem "aws-ses", "~> 0.4.3"
gem 'mail_view', :git => 'git://github.com/37signals/mail_view.git'
gem "daemon-spawn", "~> 0.4.2"

group :assets do
gem 'sass-rails', " ~> 3.1.4"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.1.2)
daemon-spawn (0.4.2)
devise (1.4.5)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3)
Expand Down Expand Up @@ -275,6 +276,7 @@ DEPENDENCIES
cells (= 3.6.7)
chinese_pinyin (= 0.4.1)
coffee-rails (~> 3.1.1)
daemon-spawn (~> 0.4.2)
devise (= 1.4.5)
jquery-rails (= 1.0.16)
mail_view!
Expand Down
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This is source code of [Ruby China Group](http://ruby-china.org)
bundle update rails
rake assets:precompile
thin start -O -C config/thin.yml
./run_resque
./script/resque start
```

## Deploy
Expand Down
16 changes: 16 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,19 @@ require 'rake'
require 'resque/tasks'

RubyChina::Application.load_tasks

namespace :resque do
task :restart_workers => :environment do
pids = Array.new

Resque.workers.each do |worker|
pids << worker.to_s.split(/:/).second
end

if pids.size > 0
system("kill -QUIT #{pids.join(' ')}")
end

system("rm /var/run/god/resque-1.8.0*.pid")
end
end
6 changes: 5 additions & 1 deletion config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@
run "cd #{deploy_to}/current/; bundle install"
end

task :restart_resque, :roles => :web do
run "cd #{deploy_to}/current/; ./script/resque stop; ./script/resque start"
end

# 编译 assets
task :compile_assets, :roles => :web do
run "cd #{deploy_to}/current/; bundle exec rake assets:precompile"
end

after "deploy:symlink", :init_shared_path, :link_shared_config_yaml, :install_gems, :compile_assets
after "deploy:symlink", :init_shared_path, :link_shared_config_yaml, :install_gems, :restart_resque, :compile_assets

set :default_environment, {
'PATH' => "/home/ruby/.rvm/gems/ruby-1.9.2-p290-patch/bin:/home/ruby/.rvm/gems/ruby-1.9.2-p290-patch@global/bin:/home/ruby/.rvm/rubies/ruby-1.9.2-p290-patch/bin:/home/ruby/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games",
Expand Down
1 change: 0 additions & 1 deletion run_resque

This file was deleted.

33 changes: 33 additions & 0 deletions script/resque
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby

ENV['RAILS_ENV'] ||= 'development'
require File.expand_path('../../config/environment', __FILE__)

require 'daemon_spawn'

class ResqueWorkerDaemon < DaemonSpawn::Base
def start(args)
@worker = Resque::Worker.new('*') # Specify which queues this worker will process
@worker.verbose = 1 # Logging - can also set vverbose for 'very verbose'
@worker.work
end

def stop
@worker.try(:shutdown)
end
end


logs = Rails.root.join("log", "resque_workers.log").to_s
pids = Rails.root.join('tmp', 'pids', 'resque_workers.pid').to_s

ResqueWorkerDaemon.spawn!({
:processes => 2,
:log_file => logs,
:pid_file => pids,
:sync_log => true,
:working_dir => Rails.root,
:singleton => true
})

# RAILS_ENV=production VVERBOSE=1 QUEUE=* rake environment resque:work 2>&1 &

0 comments on commit 279ec07

Please sign in to comment.