简体   繁体   中英

Using Resque-Scheduler with Docker-Compose - Error connecting to Redis on localhost:6379

I'm trying to run my rails app, resque and redis. Using foreman etc my setup works fine but when I try to run it in Docker using docker-compose I get:

app_1       | 21:03:13 resque.1 | Redis::CannotConnectError: Error connecting to Redis on redis://redis:6379 (SocketError)
app_1       | 21:03:13 resque.1 | Original Exception (Redis::CannotConnectError): Error connecting to Redis on redis://redis:6379 (SocketError)

My Dockerfile looks like:

FROM ruby:2.6.3-alpine

RUN apk update && apk add bash build-base libxml2-dev libxslt-dev postgresql postgresql-dev

VOLUME ["/code"]
WORKDIR /code

My Docker Compose YML looks like:

version: '3'
services:
  app:
    build: .
    command:
      - /bin/bash
      - -c
      - |
        bundle check || bundle install
        bundle exec rake db:migrate; rake db:seed
        bundle exec foreman start
    volumes:
      - ./:/code
    ports:
      - "5000:5000"
    depends_on:
      - redis
    environment:
      REDIS_URL: redis://redis/5
  redis:
    image: redis:5.0.5-alpine

My procfile:

web:       bundle exec puma -C config/puma.rb
resque:    bundle exec rake resque:work QUEUE=*
scheduler: bundle exec rake resque:scheduler

my config/initializers/resque.rb

redis_url = ENV["REDIS_URL"] || "redis://localhost:6379"

Redis.current = Redis.new(url: redis_url)
Resque.redis = Redis.current

and my lib/tasks/resque.rake

require "resque/tasks"
require "resque/scheduler/tasks"

task "resque:preload" => :environment
namespace :resque do
  task :setup do
    require "resque"

  end

  task setup_schedule: :setup do
    require "resque-scheduler"

    Resque.schedule = YAML.load_file(Rails.root.join("config","schedule.yml"))
  end

  task scheduler: :setup_schedule
end

UPDATE

The problem appears to be specifically with Resque-Scheduler. If I remove that from my procfile I can start my app fine with Docker-Compose and I can see perform resque jobs and see them running.

However the scheduler works fine when started with manually or with foreman (not using containers) so I'd like to be able to get it working here also.

From the top of my head a see few things:

version: '3'
services:
  app:
    build: .
    command:
      - /bin/bash
      - -c
      - |
        bundle check || bundle install
        bundle exec rake db:migrate; rake db:seed
        bundle exec foreman start
    volumes:
      - ./:/code
    ports:
      - "5000:5000"
    depends_on:
      - redis
    environment:
      REDIS_URL: redis://redis --> try better redis://cache
    links:
      - redis
  redis:
    image: redis:5.0.5-alpine

Got a clue from this answer here in the end: Docker-compose - Redis at 0.0.0.0 instead of 127.0.0.1

It seems I needed to set my Resque redis connection in the setup task as well as in the initializer. Adding Resque.redis = Redis.current into my resque.rake setup task finally got it all working:

require "resque/tasks"
require "resque/scheduler/tasks"

task "resque:preload" => :environment
namespace :resque do
  task :setup do
    require "resque"

    Resque.redis = Redis.current
  end

  task setup_schedule: :setup do
    require "resque-scheduler"

    Resque.schedule = YAML.load_file(Rails.root.join("config","schedule.yml"))
  end

  task scheduler: :setup_schedule
end

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