Slow Ruby DataMapper.setup - ruby

I found that my Sinatra application was so freaking slow, only happens on VMWare image on i3, i don't know why, it doesn't happened on same VMWare image on AMD APU, the line that causes the slowness is:
DataMapper.setup(:default, 'postgres://myuser:mypassword#127.0.0.1/mydbname')
it tooks almost 40-45 seconds to run that line wnen on VMWare image on i3, and i don't know why, is there any way to overcome this? this happens on Pry/IRB, on Ruby and JRuby.
It doesn't happened when using active_record or psql command line:
ActiveRecord::Base.establish_connection( adapter: 'postgresql', host: '127.0.0.1', database: 'mydbname', username: 'mypassword', password: 'mypassword', port: 5432 )
Ruby version: ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]
PostgreSQL version: postgres (PostgreSQL) 9.3.1
JRuby version: jruby 1.7.5 (1.9.3p392) 2013-10-07 74e9291 on OpenJDK Client VM 1.7.0_40-b60 [linux-i386]

this command solves my problem:
echo 127.0.0.1 `cat /etc/hostname` >> /etc/hosts

Related

`selenium-webdriver` ruby gem cannot connect with chromedriver on Ubuntu 14.04

I'm running Ubuntu 16.04 and I'm trying to run a headless Chrome browser in ruby with chromedriver.
I've installed chromedriver on Ubuntu using these instructions and then I run this via the ruby irb console:
require 'selenium-webdriver'
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
#driver = Selenium::WebDriver.for(:chrome, options: options)
Traceback (most recent call last):
10: from /home/weefee/.rvm/rubies/ruby-2.5.1/bin/irb:11:in `<main>'
9: from (irb):5
8: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver.rb:86:in `for'
7: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/driver.rb:44:in `for'
6: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/driver.rb:44:in `new'
5: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/chrome/driver.rb:44:in `initialize'
4: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:69:in `start'
3: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/socket_lock.rb:39:in `locked'
2: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:72:in `block in start'
1: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:142:in `connect_until_stable'
Selenium::WebDriver::Error::WebDriverError (unable to connect to chromedriver 127.0.1.1:9515)
Any idea on how to fix it? A few notes:
Some people seem to encounter issues with rbenv and the shim that it sets up. I'm not using rbenv at all, so that's irrelevant here.
The above works when I try it on my OSx laptop. Of course there I can easily install chromedriver with brew install chromedriver and it just seems to work
Alternatively, I uninstalled chromedriver and re-installed it using the chromedriver-helper gem. Still the same result.
I've been tearing my hair out for a while on this - any help would be appreciated. Thanks!
UPDATE
I dug deeper into the source of the selenium-webdriver gem to see exactly what it was doing when trying to connect to the chromedriver process.
I was able to replicate the following in my ruby console on the server using the same commands the selenium-webdriver gem uses:
#
# Start the Chromedriver Process
#
require 'childprocess'
process = ChildProcess.build(*["/usr/local/bin/chromedriver", "--port=9515"])
process.leader = true
process.alive? #=> false
process.start
process.alive? #=> true
#
# Create a Socket connection to 127.0.0.1:9515
#
require 'socket'
require 'selenium-webdriver'
host = Selenium::WebDriver::Platform.localhost #=> "127.0.1.1"
port = Integer(Selenium::WebDriver::Chrome::Service::DEFAULT_PORT) #=> 9515
timeout = 5
# Create and connect to socket
addr = Socket.getaddrinfo(host, port, Socket::AF_INET, Socket::SOCK_STREAM)
sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(port, addr[0][3])
# First need to rescue the writable error and then connect again
# to get the actual error. No idea why but even the official
# rubydocs use this pattern: https://apidock.com/ruby/Socket/connect_nonblock
begin
sock.connect_nonblock(sockaddr)
rescue IO::WaitWritable
IO.select(nil, [sock], nil, 5)
sock.connect_nonblock(sockaddr)
end
#=> Errno::ECONNREFUSED (Connection refused - connect(2) for 127.0.1.1:9515)
So it seems the core error is that the socket refuses to connect on that (local) address and port, even though chromedriver is very much running on that port.
I don't know much about sockets at all - is this a common error?
Thanks!
I have tried it on the Ubuntu 16.04 (updated) and ruby which comes with the system ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu].
From what I guess is that you are using wrong, old (in the guide there is version 2.26 which will not work with current chrome), chromedriver, which is not compatible with the current stable chrome.
The current stable chrome is Unpacking google-chrome-stable (71.0.3578.98-1) ... and so you need to get chromedriver as close as possible to the chrome version.
To get complete list of chromedrivers' versions click here.
In my case that would be a 71.0.3578.80 version:
wget -N http://chromedriver.storage.googleapis.com/71.0.3578.80/chromedriver_linux64.zip
You can then continue as shown at the instructions.
Then you will get working selenium-webdriver:
irb
irb(main):001:0> require 'selenium-webdriver'
=> true
irb(main):003:0> options = Selenium::WebDriver::Chrome::Options.new
=> #<Selenium::WebDriver::Chrome::Options:0x00000002ee6db0 #args=#<Set: {}>, #binary=nil, #prefs={}, #extensions=[], #options={}, #emulation={}, #encoded_extensions=[]>
irb(main):004:0> options.add_argument('--headless')
=> #<Set: {"--headless"}>
irb(main):005:0> #driver = Selenium::WebDriver.for(:chrome, options: options)
=> #<Selenium::WebDriver::Chrome::Driver:0x..f95c429ee62a3a152 browser=:chrome>
Note: If you have issues installing ffi install libffi-dev via apt-get.
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb stable main" >> /etc/apt/sources.list.d/google.list'
sudo apt-get update
sudo apt-get --only-upgrade install google-chrome-stable
This will keep your version up to date and available. I have had this solve similar problems that were encountered during CI testing. You should be able to setup your #driver afterwards.
from this line:
Selenium::WebDriver::Error::WebDriverError (unable to connect to chromedriver 127.0.1.1:9515)
it suggests you are trying to connect to 127.0.1.1, while your localhost should be 127.0.0.1, could you check your configuration?
All the provided solutions were great recommendations, but ultimately tukan's answer led me down the path of carefully checking versions.
The setup I had didn't work on Ubuntu 14.04, which is about 5 years old at the time of this writing. I re-built the whole thing on an Ubuntu 16.04 machine and it worked perfectly.
I don't know what causes the error, but it seems to be something at the operating system level.

Could not load 'active_record/connection_adapters/postgresql_adapter'

I have a Sinatra app using Postgres via Activerecord. I'm using RVM on a Mac and a number of gems including:
activerecord
activesupport
When I run the server (thin), I get an error:
Could not load 'active_record/connection_adapters/postgresql_adapter'.
Make sure that the adapter in config/database.yml is valid. If you use
an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add
the necessary adapter gem to the Gemfile.
The adapter is set to postgresql in the config, and I've checked my RVM and paths: My Gem install path is set properly, and I can see the appropriate gems installed properly, and the specific adapter is there too, so really not sure what could be happening or how to further troubleshoot this?
Using Ruby 2.0.0-p598, activerecord and activesupport 4.1.4
--
Further debugging attempts:
ruby -v
ruby 2.0.0p598 (2014-11-13 revision 48408) [x86_64-darwin14.0.0]
which ruby
/Users/user/.rvm/rubies/ruby-2.0.0-p598/bin/ruby
gem env (relevant parts):
EXECUTABLE DIRECTORY: /Users/user/.rvm/gems/ruby-2.0.0-p598/bin
GEM PATHS:
/Users/user/.rvm/gems/ruby-2.0.0-p598
/Users/user/.rvm/gems/ruby-2.0.0-p598#global
SHELL PATH:
/Users/user/.rvm/gems/ruby-2.0.0-p598/bin
/Users/user/.rvm/gems/ruby-2.0.0-p598#global/bin
/Users/user/.rvm/rubies/ruby-2.0.0-p598/bin
ls $GEM_HOME/gems
activerecord-4.1.4
activesupport-4.1.4
Really at a loss for what else I can look into
database.yml
development:
adapter: postgresql
database: example_db
username: example_user
password: example_password
host: localhost
A next debugging step I took was to try using rvm to install a whole new version of ruby. This worked, so I used rvm to completely remove my old version of ruby with rvm remove 2.0.0-p598 and reinstall it again rvm install 2.0.0-p598.
I still have no idea what may have happened, but perhaps somehow something in the gem caches/builds were corrupted, so if anyone runs into a weird error like this where everything is in place but still doesn't work, try clearing everything with rvm and reinstalling.

mysql2 adapter issue: libruby.so.1.8: cannot open shared object file: No such file or directory

I note that there have been many other similar questions, but my context seems to be different.
Error
The following is the error I am getting, trying to run/restart a Rails Application (Redmine). Redmine was running fine, but there have been some server updates by the shared host, thus seeing these problems.
mysql2 is listed in the gem/bundle list. mysql2.so exists in the location it is looking for, and has the required (executable) permissions. Thank you any suggestions in addressing this issue.
Error message:
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (libruby.so.1.8: cannot open shared object file: No such file or directory - /home/test/gems/gems/mysql2-0.3.11/lib/mysql2/mysql2.so)
Exception class:
LoadError
Enviornment
# ruby -v
ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-linux]
# rails -v
Rails 3.2.6
# gem -v
1.8.23
# mysql --version
mysql Ver 14.14 Distrib 5.5.34, for Linux (x86_64) using readline 5.1
#gem list
...
mysql2 (0.3.11)
...
#bundle show
...
mysql2 (0.3.11)
...
#vim database.yml
production:
adapter: mysql2
database: database_name
host: localhost
username: dbuser
password: dbpwd
encoding: utf8
Answer to this question solved my issue.
gem uninstall mysql2
bundle install

Failing deploy with RVM + unicorn

I'm trying to setup a Sinatra App with unicorn.
After following this example
and adjusting it to my needs, I've gotten this error:
<my_path_name>/vendor/bundle/ruby/1.9.1/gems/kgio-2.8.0/lib/kgio.rb:21:in 'require': cannot load such file -- kgio_ext (LoadError)
I'm using rvm 1.18.5, ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-linux] and the latest release of unicorn 4.5.0 which pulls kgio 2.8.0
The OS is Red Hat Enterprise Linux Server release 5.6.
I've also tried with ruby-1.9.3-p125.
Sounds like a similar problem here, maybe try one of those ideas.
Did the gem install go smoothly?

Connecting to SQL-Azure in rails using tiny_TDS: [BUG] Segmentation fault

We're trying to get rails to talk to a sqlserver db on Azure, we install freeTDS with openssl and libiconv:
./configure --prefix=/usr/local --with-libiconv-prefix=DIR --with-openssl=DIR
make
make install
We then add tiny_TDS and activerecord-sqlserver-adapter to the gemfile:
gem 'tiny_tds'
gem 'activerecord-sqlserver-adapter'
$ bundle install
Configure the database:
development:
adapter: sqlserver
host: xxxxxxx.database.windows.net
mode: DBLIB
port: 1433
database: xxxxx
username: xxxxxxxx
password: x
azure: true
Run the server:
$ rails s
Everything works great up until this point, but as soon as you visit the site ruby crashes.
steven#jenny:~/irr$ rails server -p 3001
=> Booting WEBrick
=> Rails 3.0.4 application starting in development on http://0.0.0.0:3001
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-07-24 15:37:00] INFO WEBrick 1.3.1
[2012-07-24 15:37:00] INFO ruby 1.9.2 (2012-02-14) [x86_64-linux]
[2012-07-24 15:37:00] INFO WEBrick::HTTPServer#start: pid=7586 port=3001
/home/steven/.rvm/gems/ruby-1.9.2-p318/gems/tiny_tds-0.5.1/lib/tiny_tds/client.rb:68: [BUG] Segmentation fault
ruby 1.9.2p318 (2012-02-14 revision 34678) [x86_64-linux]
-- control frame ----------
c:0048 p:---- s:0232 b:0232 l:000231 d:000231 CFUNC :connect
c:0047 p:0429 s:0228 b:0228 l:000227 d:000227 METHOD /home/steven/.rvm/gems/ruby-1.9.2-p318/gems/tiny_tds-0.5.1/lib/tiny_tds/client.rb:68
We've tried several versions of ruby: 1.9.2-p318, 1.9.2-p320, 1.9.3-p125. Same error with all of them.
This is on ubuntu 11.10 using rvm and FreeTDS-0.9.1.
Any Ideas on a workaround?
Have I made an error somewhere along the way?
Edit
Output from IRB:
require 'tiny_tds'
client = TinyTds::Client.new(:username =>'XXXXX#XXXXXXX.database.windows.net', :password => 'XXXXXXX', :host => 'XXXXXX.database.windows.net', :mode => 'DBLIB', :azure => 'true')
SystemStackError: stack level too deep from /home/martinr/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!!
It seems to be a bug with ruby itself:
/home/martinr/.rvm/gems/ruby-1.9.2-p320/gems/tiny_tds-0.5.1/lib/tiny_tds/client.rb:68: [BUG] Segmentation fault (core dumped)
You should just do a simple IRB prompt and do something like this mentioned on the TinyTDS read me.
require 'tiny_tds'
client = TinyTds::Client.new(...)
Fill in your connection params. Simple stuff like username, password, and host. Let me know how things go.
https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/Using-Azure
https://github.com/rails-sqlserver/tiny_tds#using-tinytds-with-azure
The error seems to be caused by having a username username#servername
When you remove the #severname part, ruby no longer crashes

Resources