connection closed by server error when getting accounts using coinbase-exchange websocket - ruby

I'm using the ruby coinbase-exchange gem, and I'm executing the code by copying and pasting it into irb.
This only happens when I'm running it as a websocket – when I just execute the code on its own, without the socket, it works fine.
When the following code executes,
accounts = Array.new(2, Array.new)
rest_api.accounts do |resp|
accounts = resp
end
this error is produced:
Coinbase::Exchange::APIError: GET https://api.exchange.coinbase.com/accounts?: connection closed by server
from /usr/local/lib/ruby/gems/2.3.0/gems/coinbase-exchange-0.1.2/lib/coinbase/exchange/adapters/em_http.rb:59:in `block in http_verb'
from /usr/local/lib/ruby/gems/2.3.0/gems/eventmachine-1.2.0.1/lib/em/deferrable.rb:158:in `set_deferred_status'
from /usr/local/lib/ruby/gems/2.3.0/gems/eventmachine-1.2.0.1/lib/em/deferrable.rb:198:in `fail'
from /usr/local/lib/ruby/gems/2.3.0/gems/em-http-request-1.1.5/lib/em-http/client.rb:123:in `on_error'
from /usr/local/lib/ruby/gems/2.3.0/gems/em-http-request-1.1.5/lib/em-http/client.rb:117:in `unbind'
from /usr/local/lib/ruby/gems/2.3.0/gems/em-http-request-1.1.5/lib/em-http/http_connection.rb:205:in `block in unbind'
from /usr/local/lib/ruby/gems/2.3.0/gems/em-http-request-1.1.5/lib/em-http/http_connection.rb:205:in `map'
from /usr/local/lib/ruby/gems/2.3.0/gems/em-http-request-1.1.5/lib/em-http/http_connection.rb:205:in `unbind'
from /usr/local/lib/ruby/gems/2.3.0/gems/em-http-request-1.1.5/lib/em-http/http_connection.rb:31:in `unbind'
from /usr/local/lib/ruby/gems/2.3.0/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:1481:in `event_callback'
from /usr/local/lib/ruby/gems/2.3.0/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:202:in `release_machine'
from /usr/local/lib/ruby/gems/2.3.0/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:202:in `ensure in run'
from /usr/local/lib/ruby/gems/2.3.0/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:205:in `run'
from (irb):79
from /usr/local/bin/irb:11:in `<main>'
What's the problem here? How do I get around it?

Related

Viewport not connecting in Ubuntu Server VM

I am migrating a Redmine plugin that implements user authentication via EWS. The plugin was tested on Redmine v4.x running on an Ubuntu Desktop VM and was working correctly (could tell if the given credentials were valid or not).
The main part of the plugin is:
require 'viewpoint'
include Viewpoint::EWS
defaultServer = 'https://company.mail.server/ews/exchange.asmx'
defaultEmailDomain = '#domain.com'
emailAddress = "username" + defaultEmailDomain
password = "password"
client = Viewpoint::EWSClient.new(defaultServer, emailAddress, password)
result = client
begin
client.folders
rescue
result = 'nope'
end
puts result
This code works on both the previous VM and my Windows machine, but on my new Ubuntu Server 22.04 VM returns 'nope'.
I also tried to write a version in Python using exchangelib, which worked on the new VM. IMO that indicates that the problem is not related to any network constrictions Ubuntu Server might have.
Any suggestions?
Update:
Following Richard's suggestion I saved the error message captured by rescue. After changing the error type to EwsError, I got the following:
/usr/lib/ruby/3.0.0/openssl/digest.rb:35:in `initialize': Digest initialization failed: initialization error (OpenSSL::Digest::DigestError)
from /usr/lib/ruby/3.0.0/openssl/digest.rb:35:in `block (3 levels) in <class:Digest>'
from /usr/lib/ruby/3.0.0/openssl/digest.rb:41:in `new'
from /usr/lib/ruby/3.0.0/openssl/digest.rb:41:in `block (3 levels) in <class:Digest>'
from /var/lib/gems/3.0.0/gems/rubyntlm-0.6.3/lib/net/ntlm.rb:149:in `ntlm_hash'
from /var/lib/gems/3.0.0/gems/rubyntlm-0.6.3/lib/net/ntlm.rb:162:in `ntlmv2_hash'
from /var/lib/gems/3.0.0/gems/rubyntlm-0.6.3/lib/net/ntlm/message/type2.rb:73:in `response'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient/auth.rb:563:in `block in get'
from /usr/lib/ruby/3.0.0/mutex_m.rb:78:in `synchronize'
from /usr/lib/ruby/3.0.0/mutex_m.rb:78:in `mu_synchronize'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient/auth.rb:537:in `get'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient/auth.rb:97:in `block in filter_request'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient/auth.rb:95:in `each'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient/auth.rb:95:in `filter_request'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient.rb:1231:in `block in do_get_block'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient.rb:1230:in `each'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient.rb:1230:in `do_get_block'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient.rb:1019:in `block in do_request'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient.rb:1133:in `protect_keep_alive_disconnected'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient.rb:1014:in `do_request'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient.rb:856:in `request'
from /var/lib/gems/3.0.0/gems/httpclient-2.8.3/lib/httpclient.rb:765:in `post'
from /var/lib/gems/3.0.0/gems/viewpoint-1.1.1/lib/ews/connection.rb:103:in `post'
from /var/lib/gems/3.0.0/gems/viewpoint-1.1.1/lib/ews/connection.rb:81:in `dispatch'
from /var/lib/gems/3.0.0/gems/viewpoint-1.1.1/lib/ews/soap/exchange_web_service.rb:212:in `do_soap_request'
from /var/lib/gems/3.0.0/gems/viewpoint-1.1.1/lib/ews/soap/exchange_data_services.rb:503:in `find_folder'
from /var/lib/gems/3.0.0/gems/viewpoint-1.1.1/lib/ews/folder_accessors.rb:45:in `folders'
from /home/jdredd/auth_source_ews/app/models/test.rb:27:in `<main>'
Additional info: the Windows test machine, where the user authentication runs succesfully, has the same gem versions as above. The only difference is the verion of Ruby itself (3.0.2p107 on Ubuntu, 3.1.2p20 on Windows).
Update:
After modifying openssl.cnf according to this diff enabling loading legacy providers, the plugin works now.
Try investigating the error message.
begin
client.folders
rescue StandardError => err
puts err
result = 'nope'
end
https://thoughtbot.com/blog/rescue-standarderror-not-exception

Download multiple XML files

I'm writing an application to parse XML. I have to obtain data from one XML file, and then in a loop I have to open another XML file.
The code looks like this:
$doc = Nokogiri::XML(open('myxmladress'))
$doc.xpath('//job').each do |job|
if job.xpath('name').text.include?('joe')
$doc2 = Nokogiri::XML(open('myxmladress_for_joe'))
end
end
I believe that I cannot have multiple HTTP connections open.
Can I simply download the whole file instead of using
$doc Nokogiri::XML(open('myxmladress'))
or is there any way to close the Nokogiri HTTP connection?
What is more I'm downloading it by https.
My error:
in `open_http': 500 Server Error (OpenURI::HTTPError)
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_open'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:212:in `block in open_loop'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:210:in `catch'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:717:in `open'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:35:in `open'
from jenkins_auth.rb:97:in `block (2 levels) in combine_partial_results'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:187:in `block in each'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `upto'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `each'
from jenkins_auth.rb:89:in `block in combine_partial_results'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:187:in `block in each'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `upto'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `each'
from jenkins_auth.rb:86:in `combine_partial_results'
from jenkins_auth.rb:130:in `get_tests_for_job'
from jenkins_auth.rb:137:in `<main>'
You are using OpenUri to fetch a document from a URL. I am pretty sure that this doesn't leave any open connection, but reads the document into an IO like object, kind of like a file.
Your problem seems to be that the server has had an internal error.

ruby: ECONNREFUSED from http.rb

I am trying to parse an rss feed with Ruby. This is my (working) code, with the actual feed url:
include 'rss'
myfeed = RSS::Parser.parse('http://allmusicnews.altervista.org/blog/feed/', false)
If I run it from my development computer (Mac OS 10.9), all works fine. If I run it from my server (Debian 7), all i get is:
/home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:879:in `initialize': Connection refused - connect(2) for "allmusicnews.altervista.org" port 80 (Errno::ECONNREFUSED)
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:879:in `open'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:879:in `block in connect'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/timeout.rb:74:in `timeout'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:878:in `connect'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:852:in `start'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:318:in `open_http'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:736:in `buffer_open'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:211:in `block in open_loop'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:209:in `catch'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:209:in `open_loop'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:150:in `open_uri'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:716:in `open'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:724:in `read'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rss/parser.rb:103:in `normalize_rss'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rss/parser.rb:89:in `initialize'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rss/parser.rb:76:in `new'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rss/parser.rb:76:in `parse'
This does not happen with other rss feeds. example: feeds.blogo.it/soundsblog/it seems working well on both platforms.
I am using the same Ruby version (2.2.0), installed with RVM on both the computers. Same gems too.
Any clue, at least about what to look for?
ps: it may seem something related to connection timeout, but as I run my script suddenly I see the error message above, there's no idle waiting time...
Please ignore my question. I was shooting to the wrong target.
Actually I'm afraid to have some blacklistish problem with my production machine. I cannot connect to altervista.org server in any way (lynx, telnet), I keep getting "connection refused error", so there's nothing to do with my Ruby code...

capistrano deploy error - newbie

I'm new to ruby and currently having issues deploying with capistrano. below the errors I am getting.
cap aborted!
Operation timed out - connect(2)
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:67:in `initialize'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:67:in `open'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:67:in `block in initialize'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:67:in `initialize'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh.rb:200:in `new'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh.rb:200:in `start'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:156:in `ssh'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:109:in `block in _execute'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:106:in `tap'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:106:in `_execute'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:54:in `execute'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/abstract.rb:75:in `within'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/capistrano-3.0.0/lib/capistrano/tasks/git.rake:44:in `block (3 levels) in <top (required)>'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:42:in `instance_exec'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:42:in `run'
/Users/stefydu/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => git:create_release => git:update
(See full trace by running task with --trace)
Not a solution, but an explanation with workaround:
Capistrano 3, and with it sshkit, doesn't reuse existing SSH connections, but start a new one for every task (see https://github.com/capistrano/sshkit/issues/34).
Some servers, respectively their firewalls, seem to block new connection requests if they come to fast or if there are too many open connections.
Solutions:
Fix SSHKit/Capistrano to reuse connections
Configure your server/firewall to allow tons of parallel ssh sessions (for a simple rails app about 15)
use my hack below (Disclaimer: no, don't use it):
We're going to extend (monkeypatch) the execute method in gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb. That can be done via the Capfile:
# Overwrite execute method to avoid timeouts
class SSHKit::Backend::Netssh
def execute(*args)
_execute(*args).success?
rescue Errno::ETIMEDOUT
#tries ||= 0
raise "Nope, no way" if #tries > 5
puts "Timeout. yeah. Try it again (#{#tries})"
sleep 5
#tries += 1
execute(*args)
end
end
This gives the server a chance to breathe...
Capistrano uses a connection pool for SSH now:
https://github.com/capistrano/sshkit/pull/70

Jumpstart Labs MicroBlogger unable to connect

I'm following the Jumpstart Labs MicroBlogger tutorial and I'm running into an issue where I'm unable to connect to twitter. I am getting the following errors:
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:799:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (Twitter::Error::ClientError)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:799:in `block in connect'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:799:in `connect'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:744:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:1284:in `request'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.8/lib/faraday/adapter/net_http.rb:75:in `perform_request'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.8/lib/faraday/adapter/net_http.rb:38:in `call'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.8/lib/faraday/response.rb:8:in `call'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.8/lib/faraday/response.rb:8:in `call'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.8/lib/faraday/response.rb:8:in `call'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.8/lib/faraday/request/url_encoded.rb:14:in `call'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.8/lib/faraday/request/multipart.rb:13:in `call'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/twitter-4.8.1/lib/twitter/request/multipart_with_file.rb:14:in `call'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.8/lib/faraday/connection.rb:253:in `run_request'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.8/lib/faraday/connection.rb:118:in `post'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/twitter-4.8.1/lib/twitter/client.rb:108:in `request'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/twitter-4.8.1/lib/twitter/client.rb:72:in `post'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/twitter-4.8.1/lib/twitter/api/utils.rb:82:in `object_from_response'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/twitter-4.8.1/lib/twitter/api/tweets.rb:129:in `update'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/jumpstart_auth-0.3.0/lib/jumpstart_auth.rb:32:in `update'
from C:/Users/Steve/RubymineProjects/MicroBlogger/micro_blogger.rb:12:in `tweet'
from C:/Users/Steve/RubymineProjects/MicroBlogger/micro_blogger.rb:18:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
This is my code so far:
require 'jumpstart_auth'
class MicroBlogger
attr_reader :client
def initialize
puts "Initializing"
#client = JumpstartAuth.twitter
end
def tweet(message)
#client.update(message)
end
end
blogger = MicroBlogger.new
blogger.tweet("hey")
As indicated in the error message, the SSL library couldn't verify the SSL certificate returned from the Twitter server. In this case, it is most likely because no certificate authorities were installed for you.
The details and solution are available here:
https://gist.github.com/fnichol/867550#file-readme-md
I suggest using the "boring" method, since it does not involve running code copied off the internet (instructions in case the link disappears, but better to read info at link):
Download http://curl.haxx.se/ca/cacert.pem
Save it to somewhere like C:\RailsInstaller\cacert.pem
Run set SSL_CERT_FILE=C:\RailsInstaller\cacert.pem to set the location for the current shell
Run your script in the same shell
If you have installed ruby directly to windows you can copy the cacert.pem file to the Ruby200dir(eg. c:\ruby200) and run the set SSL_CERT_FILE=[path to the cert file] command from the command prompt.
Keep in mind that this will require you to add the SSL cert for every boot of your computer

Resources