You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 7.0.8.1生产环境Sidekiq连接Redis版本不匹配问题

Sidekiq启动失败:连接旧版本Redis问题

问题描述

生产环境部署的Rails 7.0.8.1应用,运行在Ubuntu 22.04服务器上,已安装Redis 7.2.5,但启动Sidekiq时,系统提示连接的是Redis 6.0.16,不符合Sidekiq 7.1.1要求的Redis 6.2.0及以上版本,导致启动失败。

环境配置

Systemd服务文件(/etc/systemd/system/sidekiq.service)

[Unit]
Description=Sidekiq
After=syslog.target network.target

[Service]
Type=simple
WorkingDirectory=/home/deploy/lipepay/current
ExecStart=/home/deploy/start_sidekiq.sh
User=deploy
Group=deploy
UMask=0002
RestartSec=15
Restart=always
Environment=REDIS_URL=redis://127.0.0.1:6379/0

[Install]
WantedBy=multi-user.target

启动脚本(/home/deploy/start_sidekiq.sh)

#!/bin/bash
# File: /home/deploy/start_sidekiq.sh

# Load Ruby version manager environment
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

# Set REDIS_URL
export REDIS_URL=redis://127.0.0.1:6379/0

# Navigate to application directory
cd /home/deploy/lipepay/current

# Start Sidekiq
exec /home/deploy/.rbenv/shims/bundle exec sidekiq -e production

Sidekiq初始化器(config/initializers/sidekiq.rb)

require 'sidekiq/cron'

if Rails.env.production?
  schedule_file = "config/schedule.yml"
  
  if File.exist?(schedule_file) && Sidekiq.server?
    yaml_jobs = YAML.load_file(schedule_file)
    existing_jobs = Sidekiq::Cron::Job.all.map(&:name)
    
    yaml_jobs.each do |name, details|
      unless existing_jobs.include?(name)
        Sidekiq::Cron::Job.create(name: name, cron: details['cron'], class: details['class'])
      end
    end
  end

  Sidekiq.configure_server do |config|
    config.redis = { url: ENV['REDIS_URL'] }
  end

  Sidekiq.configure_client do |config|
    config.redis = { url: ENV['REDIS_URL'] }
  end
end

错误日志

journalctl -u sidekiq输出:

Jun 26 17:34:27 ubuntu-2gb-hil-1 start_sidekiq.sh[348225]: You are connecting to Redis 6.0.16, Sidekiq requires Redis 6.2.0 or greater
Jun 26 17:34:27 ubuntu-2gb-hil-1 start_sidekiq.sh[348225]: /home/deploy/lipepay/shared/bundle/ruby/3.3.0/gems/sidekiq-7.1.1/lib/sidekiq/cli.rb:184:in `verify_sufficient_redis_version'

已采取的步骤

  • 检查Redis版本:

    redis-server --version
    # Output: Redis server v=7.2.5 sha=00000000:0 malloc=jemalloc-5.3.0 bits=64 build=d2f534f69a26fea
    
  • 在Rails控制台验证Redis连接版本:

    redis = Redis.new(url: ENV['REDIS_URL'])
    puts redis.info["redis_version"]
    # Output: 7.2.5
    
  • 检查Gemfile.lock中的Sidekiq依赖:

    grep sidekiq Gemfile.lock
    # Output:
    # sidekiq (7.1.1)
    # sidekiq-cron (1.10.1)
    
  • 查看Sidekiq服务状态:

    ● sidekiq.service - Sidekiq
         Loaded: loaded (/etc/systemd/system/sidekiq.service; enabled; vendor preset: enabled)
         Active: active (running) since Thu 2024-06-27 20:42:21 UTC; 8min ago
       Main PID: 805007 (ruby)
          Tasks: 11 (limit: 2252)
         Memory: 171.8M
            CPU: 3.620s
         CGroup: /system.slice/sidekiq.service
                 └─805007 "sidekiq 7.1.1 lipepay [0 of 5 busy]" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
    
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.015Z pid=805007 tid=hd1v INFO: Upgrade to Sidekiq Pro for mor>
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.015Z pid=805007 tid=hd1v INFO: Sidekiq 7.1.1 connecting to Re>
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.019Z pid=805007 tid=hd1v INFO: Cron Jobs - added job with nam>
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.021Z pid=805007 tid=hd1v INFO: Cron Jobs - added job with nam>
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.023Z pid=805007 tid=hd1v INFO: Cron Jobs - added job with nam>
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.025Z pid=805007 tid=hd1v INFO: Cron Jobs - added job with nam>
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.027Z pid=805007 tid=hd1v INFO: Cron Jobs - added job with nam>
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.029Z pid=805007 tid=hd1v INFO: Cron Jobs - added job with nam>
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.030Z pid=805007 tid=hd1v INFO: Cron Jobs - added job with nam>
    Jun 27 20:42:24 ubuntu-2gb-hil-1 start_sidekiq.sh[805007]: 2024-06-27T20:42:24.032Z pid=805007 tid=hd1v INFO: Cron Jobs - added job with nam>
    lines 1-20/20 (END)
    

内容的提问来源于stack exchange,提问作者Fillype Farias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 19:20:55