简体   繁体   中英

docker + rails + redis - rescue workers are not running

I've created a docker environment that creates 3 images: rails, postgresql and redis. It has been working quite well but I have found that my redis image does not seem to have any workers running.

Docker Info

My docker-compose.yml is as follows

web:  
  build: .
  command: bundle exec unicorn -p 3000 -c config/unicorn.rb
  volumes:
    - .:/fitmo
    - ../fitmo-core:/fitmo-core
  ports:
    - "3000:3000"
  links:
    - db
    - redis
  environment:
    - REDIS_URL=redis://redis:6379

db:
  build: ./db/docker-files
  ports:
    - "5432"

redis:  
  image: redis:2.8
  ports:
    - "6379"

Resque Config

require 'resque'
require 'resque-scheduler'
require 'resque_scheduler/server'
require 'appsignal/integrations/resque'
require 'yaml'

if Rails.env.test?
  require 'mock_redis'
  $redis = MockRedis.new
else
  uri = URI.parse(ENV['REDIS_URL'])
  $redis = Redis.new(host: uri.host, port: uri.port, password: uri.password)
end

Resque.redis = $redis
Resque.schedule = YAML.load_file(File.join(Rails.root, 'config/resque_schedule.yml'))

Resque.before_fork do
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

Resque.after_fork do
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

module Resque

  def queued_jobs
    queues = Hash.new
    Resque.queues.each do |queue|
      jobs = []
      Resque.peek(queue, 0, 5000).each do |job|
        jobs.push({klass: job['class'], args: job['args']})
      end
      queues[queue] = jobs
    end
    queues
  end

  def queued?(klass, *args)
    queued_jobs.select do |job|
      job[:klass] == klass.to_s && job[:args] == args
    end.any?
  end

  def enqueue_once(klass, *args)
    enqueue(klass, *args) unless queued?(klass, *args)
  end

end

Ruby Gems

The following are the relevant gems being used. The commented out versions are the latest versions of the gems but I don't think this is an issue as the current versions are working fine in Heroku environments. I've also include rescue mailer and scheduler for thoroughness. They are not in play for the question

gem 'redis',            '3.0.7'         #'~> 3.2.0'
gem 'resque',           '~> 1.25.2'     #'~> 1.25.2'
gem 'resque_mailer',    '~> 1.0.1'      #'~> 2.2.7'
gem 'resque-scheduler', '~> 2.5.5'      #'~> 4.0.0'

Testing Info

I have confirmed that redis is accessible from the web container by telnetting to host redis on port 6379. I've also output the value of Rescue.info which shows that there are items queued but now workers running

resque info: {:pending=>1, :processed=>0, :queues=>2, :workers=>0, :working=>0, :failed=>0, :servers=>["redis://redis:6379/0"], :environment=>"development"}

I'm stuck at this point as I'm not sure how to get the workers running. Any suggestions?

I was able to solve this by including a new image to be started based on the web image. My new docker-compose.yml file is as follows (added new worker image):

web:  
  build: .
  command: bundle exec unicorn -p 3000 -c config/unicorn.rb
  volumes:
    - .:/fitmo
    - ../fitmo-core:/fitmo-core
  ports:
    - "3000:3000"
  links:
    - db
    - redis
  environment:
    - REDIS_URL=redis://redis:6379

worker:
  build: .
  command: bundle exec rake environment resque:work QUEUE=*
  volumes:
    - .:/fitmo
    - ../fitmo-core:/fitmo-core
  links:
    - db
    - redis
  environment:
    - REDIS_URL=redis://redis:6379

db:
  build: ./db/docker-files
  ports:
    - "5432"

redis:  
  image: redis:latest
  ports:
    - "6379"

There is a subsequent issue if you have AppSignal gem installed and you are extending your jobs with "extend Appsignal::Integrations::ResquePlugin" The issue is that the ResquePlugin tries to write a socket to a tmp folder and Docker doesn't allow it.

I'm sure there is a Docker configuration to allow the socket to be written but I have instead created a development section in my appsignal.yml file and set the active flag to false. The latest version of the Appsignal gem still attempts to write to the socket even when active = false. Version 0.13.0 of the gem (to be released tomorrow) should have a fix for this in place

appsignal.yml

production:
  api_key: "xxxxxxxxxxxxxxxxxxxxxxxx"
  active: true
  slow_request_threshold: 200
staging:
  api_key: "xxxxxxxxxxxxxxxxxxxxxxxx"
  active: true
  slow_request_threshold: 200
development:
  api_key: "xxxxxxxxxxxxxxxxxxxxxxxx"
  active: false
  slow_request_threshold: 200

在resque的github页面https://github.com/resque/resque上 ,它提到你需要运行bin/resque work来开始轮询队列和启动worker。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM