resque + resque-schedulerをHerokuで動かしたメモ

#gem追加
```lang:Gemfile
gem 'resque'
gem 'resque-scheduler'
```
bundle install

#rake task作成
`bundle exec rails g task resque`

```lang:lib/tasks/resque.rake
require 'resque/tasks'
require 'resque/scheduler/tasks'

namespace :resque do
  task :preload => :environment
  task :setup do
    require 'resque'
  end

  task :setup_schedule => :setup do
    require 'resque-scheduler'

    # If you want to be able to dynamically change the schedule,
    # uncomment this line.  A dynamic schedule can be updated via the
    # Resque::Scheduler.set_schedule (and remove_schedule) methods.
    # When dynamic is set to true, the scheduler process looks for
    # schedule changes and applies them on the fly.
    # Note: This feature is only available in >=2.0.0.
    Resque::Scheduler.dynamic = true

    # The schedule doesn't need to be stored in a YAML, it just needs to
    # be a hash.  YAML is usually the easiest.
    # Resque.schedule = YAML.load_file('resque_schedule.yml')

    # If your schedule already has +queue+ set for each job, you don't
    # need to require your jobs.  This can be an advantage since it's
    # less code that resque-scheduler needs to know about. But in a small
    # project, it's usually easier to just include you job classes here.
    # So, something like this:
    # require 'jobs'
  end

  task :scheduler_setup => :setup_schedule
end
```

#resqueの管理画面用routing追加
```lang:routes.rb
mount Resque::Server.new, at: "/resque"
```

#job作成
```lang:app/jobs/resque_sample_job.rb
class ResqueSampleJob
  @queue = :resque_sample_job
  def self.perform(text)
    path = File.expand_path("log/resque_sample.log", Rails.root)
    File.open(path, 'a') do |f|
      f.puts text
    end
  end
end
```

#redis設定
```lang:config/initializers/redis.rb
require 'resque-scheduler'
require 'resque/scheduler/server'
if ENV["REDISCLOUD_URL"]
  $redis = Resque.redis = Redis.new(:url => ENV["REDISCLOUD_URL"])
else
  $redis = Resque.redis = Redis.new(host: "localhost", port: "6379")
end
```
#ローカルで確認
1.redis立ち上げ
`$ redis-server`
2.resque立ち上げ
`$ QUEUE=* rake environment resque:work`
3.scheduler立ち上げ
`$ DYNAMIC_SCHEDULE=true rake environment resque:scheduler`
4.ログを監視
`$ tail -f log/resque_sample.log`
4.rails console からコマンド実行しそれぞれログ確認
`Resque.enqueue(ResqueSampleJob, "すぐに実行")`
`Resque.enqueue_at(1.minutes.from_now, ResqueSampleJob, "1分後に実行")`
`Resque.set_schedule("sample_job", { class: "ResqueSampleJob", cron: '* * * * *', args: "1分ごとに実行"})`


#Herokuで実行
##gemにunicorn追加
```lang:Gemfile
group :production do
  # Use unicorn as the app server
  gem 'unicorn', '~> 4.6.2'
end
```
##unicornの設定ファイル作成
```lang:config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true


before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
      ActiveRecord::Base.connection.disconnect!

  if defined?(Resque)
    Resque.redis.quit
    Rails.logger.info('Disconnected from Redis')
  end
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  if defined?(Resque)
    Rails.logger.info('Connected to Redis')
  end
end
```
##Procfile作成
```lang:Procfile
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
resque: env TERM_CHILD=1 QUEUE=* rake environment resque:work
scheduler: DYNAMIC_SCHEDULE=true rake environment resque:scheduler
```

##Herokuにredis cloudアドオン追加
`heroku addons:add rediscloud`

##各プロセス立ち上げ
`heroku scale web=1 resque=1 scheduler=1`

##ローカルと同様に確認
省略

#参考
http://blog.notsobad.jp/post/93023071256/resque-heroku
http://feministy.io/deploying-rails-41-apps-with-resque-to-heroku
http://stackoverflow.com/questions/22468622/scheduled-dynamic-resque-scheduler-never-launches-worker
http://blog.madoro.org/mn/56
http://blog.madoro.org/mn/17
http://kakakakakku.hatenablog.com/entry/2014/05/11/134840