如何从发条的 ruby 脚本初始化 rails 5.1 环境

How to initialize rails 5.1 environment from ruby script for clockwork

我需要从任意脚本初始化 rails 以便我可以使用 Clockwork

访问一些 ActiveRecord 对象

更新:仅当 require 'clockwork'require_relative './config/environment' 都需要时才会出现此问题。

他们建议这样初始化:

# rails-root-directory/my-file.rb
require 'clockwork'
require_relative './config/boot'
require_relative './config/environment'

遗憾的是 require_relative './config/environment' 似乎包含了 openssl 两次左右,导致了一大堆警告,最后是一个错误(下图)

我是 运行 rails 5.1,ruby 4.2.1 在 MacOS High Sierra 上使用 rbenv

不确定这是否是初始化 rails 的正确方法(谷歌搜索似乎是这样说的)或者这是否只是我的设置问题。请帮忙!

/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:18: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:24: warning: already initialized constant OpenSSL::Cipher::AES
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:24: warning: previous definition of AES was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:18: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:24: warning: already initialized constant OpenSSL::Cipher::CAST5
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:24: warning: previous definition of CAST5 was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:18: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:24: warning: already initialized constant OpenSSL::Cipher::BF
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:24: warning: previous definition of BF was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:18: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:24: warning: already initialized constant OpenSSL::Cipher::DES
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:24: warning: previous definition of DES was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:18: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:24: warning: already initialized constant OpenSSL::Cipher::IDEA
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:24: warning: previous definition of IDEA was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:18: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:24: warning: already initialized constant OpenSSL::Cipher::RC2
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:24: warning: previous definition of RC2 was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:18: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:24: warning: already initialized constant OpenSSL::Cipher::RC4
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:24: warning: previous definition of RC4 was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:18: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:24: warning: already initialized constant OpenSSL::Cipher::RC5
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:24: warning: previous definition of RC5 was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:28: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:33: warning: already initialized constant OpenSSL::Cipher::AES128
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:33: warning: previous definition of AES128 was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:28: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:33: warning: already initialized constant OpenSSL::Cipher::AES192
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:33: warning: previous definition of AES192 was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:28: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:33: warning: already initialized constant OpenSSL::Cipher::AES256
/Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/openssl-2.0.5/lib/openssl/cipher.rb:33: warning: previous definition of AES256 was here
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:64: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:64: warning: constant OpenSSL::Cipher::Cipher is deprecated
/Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:64:in `<class:Cipher>': superclass mismatch for class Cipher (TypeError)
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:16:in `<module:OpenSSL>'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl/cipher.rb:15:in `<top (required)>'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl.rb:17:in `require'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/2.4.0/openssl.rb:17:in `<top (required)>'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.4/lib/active_support/key_generator.rb:2:in `require'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.4/lib/active_support/key_generator.rb:2:in `<top (required)>'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/application.rb:4:in `require'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/application.rb:4:in `<top (required)>'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails.rb:12:in `require'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails.rb:12:in `<top (required)>'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/all.rb:1:in `require'
    from /Users/james/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/all.rb:1:in `<top (required)>'
    from /Users/james/sites/mergefreezev2/config/application.rb:3:in `require'
    from /Users/james/sites/mergefreezev2/config/application.rb:3:in `<top (required)>'
    from /Users/james/sites/mergefreezev2/config/environment.rb:2:in `require_relative'
    from /Users/james/sites/mergefreezev2/config/environment.rb:2:in `<top (required)>'
    from clock.rb:4:in `require_relative'
    from clock.rb:4:in `<main>'

Gem文件(rails 5.1 默认 gem 除外)

gem 'omniauth'
gem 'omniauth-github'
gem 'httparty'
gem 'resque'
gem 'resque-web', require: 'resque_web'
gem 'chronic'
gem 'tzinfo'
gem 'clockwork'
gem 'geoip'

本地Gem列表:

$ gem list

*** LOCAL GEMS ***

actioncable (5.1.4, 5.1.3)
actionmailer (5.1.4, 5.1.3)
actionpack (5.1.4, 5.1.3)
actionview (5.1.4, 5.1.3)
activejob (5.1.4, 5.1.3)
activemodel (5.1.4, 5.1.3)
activerecord (5.1.4, 5.1.3)
activesupport (5.1.4, 5.1.3)
addressable (2.5.2)
arel (8.0.0)
bcrypt (3.1.11)
bigdecimal (default: 1.3.0)
bindex (0.5.0)
builder (3.2.3)
bundler (1.16.1, 1.15.4)
byebug (9.1.0)
capybara (2.17.0, 2.16.1, 2.15.1)
childprocess (0.8.0, 0.7.1)
chronic (0.10.2)
clockwork (2.0.2)
coderay (1.1.2)
coffee-rails (4.2.2)
coffee-script (2.4.1)
coffee-script-source (1.12.2)
commonjs (0.2.7)
concurrent-ruby (1.0.5)
crass (1.0.3)
devise (4.3.0)
did_you_mean (1.1.0)
domain_name (0.5.20170404)
erubi (1.7.0, 1.6.1)
et-orbi (1.0.8)
execjs (2.7.0)
faraday (0.13.1, 0.12.2)
ffi (1.9.18)
font-awesome-sass (4.7.0)
geoip (1.6.3)
globalid (0.4.1, 0.4.0)
hashie (3.5.7, 3.5.6)
http-cookie (1.0.3)
httparty (0.15.6)
i18n (0.9.1, 0.8.6)
io-console (default: 0.4.6)
jbuilder (2.7.0)
jquery-rails (4.3.1)
json (default: 2.0.2)
jwt (1.5.6)
less (2.6.0)
less-rails (2.8.0)
listen (3.1.5)
loofah (2.1.1, 2.0.3)
mail (2.7.0, 2.6.6)
mailgun_rails (0.9.0)
method_source (0.9.0, 0.8.2)
mime-types (3.1)
mime-types-data (3.2016.0521)
mini_mime (1.0.0, 0.1.4)
mini_portile2 (2.3.0, 2.2.0)
minitest (5.11.1)
mono_logger (1.1.0)
multi_json (1.12.2, 1.12.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
mustermann (1.0.1)
mysql2 (0.4.10, 0.4.9)
net-telnet (0.1.1)
netrc (0.11.0)
nio4r (2.2.0, 2.1.0)
nokogiri (1.8.1, 1.8.0)
oauth2 (1.4.0)
octokit (4.7.0)
omniauth (1.8.1, 1.6.1)
omniauth-github (1.3.0, 1.1.2)
omniauth-google-oauth2 (0.5.2)
omniauth-oauth2 (1.5.0, 1.4.0, 1.3.1)
omniauth-slack (2.3.0)
openssl (2.0.5, default: 2.0.3)
orm_adapter (0.5.0)
power_assert (0.4.1)
pry (0.11.3, 0.10.4)
psych (default: 2.2.2)
public_suffix (3.0.1, 3.0.0)
puma (3.11.0, 3.10.0)
rack (2.0.3)
rack-protection (2.0.0)
rack-test (0.8.2, 0.7.0, 0.6.3)
rails (5.1.4, 5.1.3)
rails-dom-testing (2.0.3)
rails-html-sanitizer (1.0.3)
railties (5.1.4, 5.1.3)
rake (12.3.0, 12.0.0)
rb-fsevent (0.10.2)
rb-inotify (0.9.10)
rdoc (default: 5.0.0)
redis (4.0.1)
redis-namespace (1.6.0)
responders (2.4.0)
resque (1.27.4)
resque-scheduler (4.3.1)
resque-scheduler-web (1.1.0)
resque-web (0.0.12)
rest-client (2.0.2)
ruby_dep (1.5.0)
rubyzip (1.2.1)
rufus-scheduler (3.4.2)
sass (3.5.5, 3.5.4, 3.5.3, 3.5.1)
sass-listen (4.0.0)
sass-rails (5.0.7, 5.0.6)
sawyer (0.8.1)
selenium-webdriver (3.8.0, 3.7.0, 3.5.2, 3.5.1)
sinatra (2.0.0)
slop (3.6.0)
spring (2.0.2)
spring-watcher-listen (2.0.1)
sprockets (3.7.1)
sprockets-rails (3.2.1)
sqlite3 (1.3.13)
test-unit (3.2.3)
thor (0.20.0)
thread_safe (0.3.6)
tilt (2.0.8)
turbolinks (5.0.1)
turbolinks-source (5.0.3)
twitter-bootstrap-rails (4.0.0)
tzinfo (1.2.4, 1.2.3)
tzinfo-data (1.2017.3)
uglifier (4.1.2, 4.1.0, 3.2.0)
unf (0.1.4)
unf_ext (0.0.7.4)
vegas (0.1.11)
warden (1.2.7)
web-console (3.5.1)
websocket-driver (0.6.5)
websocket-extensions (0.1.3, 0.1.2)
xmlrpc (0.2.1)
xpath (3.0.0, 2.1.0)

问题是我试图同时加载 openssl gem 的两个版本。

执行 gem list 显示:openssl (2.0.5, default: 2.0.3)

恢复到默认版本解决了问题:

gem uninstall openssl -v 2.0.5
gem install openssl -v 2.0.3