Ruby
Rails
Resque
Heroku
AdventCalendar

Rails3.2.8 + Resque + Heroku(Redis To Go)

More than 5 years have passed since last update.


参考リンク


Redisインストール


ターミナル

brew install redis

----------
==> Downloading http://redis.googlecode.com/files/redis-2.4.15.tar.gz
######################################################################## 100.0%
==> make -C /private/tmp/homebrew-redis-2.4.15-bg22/redis-2.4.15/src CC=/usr/bin/gcc-4.2
==> Caveats
If this is your first install, automatically load on login with:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/redis/2.4.15/homebrew.mxcl.redis.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

If this is an upgrade and you already have the homebrew.mxcl.redis.plist loaded:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
cp /usr/local/Cellar/redis/2.4.15/homebrew.mxcl.redis.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

To start redis manually:
redis-server /usr/local/etc/redis.conf

To access the server:
redis-cli
==> Summary
/usr/local/Cellar/redis/2.4.15: 9 files, 576K, built in 19 seconds
----------


Redisサーバ起動


ターミナル

redis-server /usr/local/etc/redis.conf

----------
[1648] 20 Sep 12:14:06 * Server started, Redis version 2.4.15
[1648] 20 Sep 12:14:06 * The server is now ready to accept connections on port 6379
[1648] 20 Sep 12:14:06 - 0 clients connected (0 slaves), 922304 bytes in use
----------


Resqueインストール


Gemfile

gem 'resque', require: 'resque/server'



ターミナル

bundle install



Redis/Resque設定作成

vi config/initializers/resque.rb


config/initializers/resque.rb

Resque.redis = 'localhost:6379'



Rakeタスク追加

vi lib/tasks/resque.rake


lib/tasks/resque.rake

require "resque/tasks"

task "resque:setup" => :environment



キュー追加

※Controller、Modelなど

Resque.enqueue( SnippetHighlighter, @snippet.id )


worker作成


app/workers/snippet_highlighter.rb

class SnippetHighlighter

@queue = :snippets_queue

def self.perform(snippet_id)
snippet = Snippet.find(snippet_id)
uri = URI.parse('http://pygments.appspot.com/')
request = Net::HTTP.post_form(uri, {'lang' =>
snippet.language, 'code' => snippet.plain_code})
snippet.update_attribute(:highlighted_code, request.body)
end
end



Rakeタスク実行

※起動しておくと発生したキューを順次実行していく(「QUEUE=queue_name」でキャッチするキューを指定する、全ての場合は「QUEUE='*'」)


ターミナル

bundle exec rake resque:work QUEUE=snippets_queue



ターミナル

bundle exec rake resque:work QUEUE='*'



ジョブ管理画面起動


ターミナル

resque-web



ResqueのWebインターフェイスをRailsアプリケーションへ組み込む


config/routes.rb

MyApp::Application.routes.draw do

mount Resque::Server, at: "/resque"

# 〜
end


http://localhost:3000/resque


Devise認証用


config/routes.rb

MyApp::Application.routes.draw do

authenticate :admin do
mount Resque::Server, at: "/resque"
end
end


BASIC認証用


config/initializers/resque_auth.rb

Resque::Server.use(Rack::Auth::Basic) do |user, password|

password == "secret"
end



Heroku - Redis To Go


Redis To Goアドオン追加


ターミナル

heroku addons:add redistogo:nano



Redis To Go用設定追加

Redis To Goアドレス確認


ターミナル

heroku config | grep "REDISTOGO_URL"



config/initializers/resque.rb

if Rails.env.development?

Resque.redis = 'localhost:6379'
else
Resque.redis = ENV['REDISTOGO_URL']
end


Heroku上Rakeタスク実行


ターミナル

heroku run bundle exec rake resque:work QUEUE='*'



「PG::Error: SSL error: decryption failed or bad record mac」エラーが発生した場合


lib/tasks/resque.rake

require "resque/tasks"

task "resque:setup" => :environment do
ENV['QUEUE'] = '*'
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
end