Zxing connection refused on production - ruby-on-rails-3

I'm using ZXing to decode my qr codes, it works fine on development, but on production after deploy it just works for five minutes or so and only if I restart the server, after that it just doesn't work, going through the log, this is what comes out:
ActionView::Template::Error (druby://127.0.0.1:51876 - #<Errno::ECONNREFUSED: Connection refused - connect(2)>):
1: <% provide(:title, "Updating...") %>
2: <input />
3: <% if (ZXing.decode "public/assets/#{current_user.user_name.downcase}_bdd.jpg").nil? %>
4: <% flash.now[:notice] = "BDD invalid, change it!" %>
5: <div class="actions_cambiar_bdd">
app/views/users/change_folios.html.erb:3:in
`_app_views_users_change_folios_html_erb___206747123981808960_69821004639000'
I don't know what is going on, looking at it, I think that it cannot works its way out to my server that's why the connection refused, but the weird thing is that it works fine for a couple of minutes after the server is restarted, after that, nothing, I hope anyone can help me out. Thanks for your time.
EDIT:
Apparently after a while the port gets blocked or busy, i guess that is something to do with how zxing manage the port assign, here are some files of it:
EDIT 2
client.rb
require 'socket'
require 'drb'
module ZXing
BIN = File.expand_path('../../../bin/zxing', __FILE__)
class Client
def self.new
port = ENV['ZXING_PORT'] || find_available_port
setup_drb_server(port) unless ENV['ZXING_PORT'] && responsive?(port)
DRbObject.new_with_uri("druby://127.0.0.1:#{port}")
end
private
def self.setup_drb_server(port)
remote_client = IO.popen("#{ZXing::BIN} #{port}")
sleep 0.5 until responsive?(port)
at_exit { Process.kill(:INT, remote_client.pid) }
end
def self.responsive?(port)
socket = TCPSocket.open('127.0.0.1', port)
true
rescue Errno::ECONNREFUSED
false
ensure
socket.close if socket
end
def self.find_available_port
server = TCPServer.new('127.0.0.1', 0)
server.addr[1]
ensure
server.close if server
end
end
end
I kind of get answered on github by one of the guys in the zxing project:
https://github.com/ecin/zxing.rb/issues/6
Shortly he gave me the advice to export from my code the ZXING_PORT const, but it did not solve it, the number on the ZXING_PORT gets the same treatment, it works for a couple of minutes, and then the same mistake, only this time with the ZXING_PORT
ActionView::Template::Error (druby://127.0.0.1:ZXING_PORT - #<Errno::ECONNREFUSED: Connection refused - connect(2)>):
I'm considering the options to either somehow "reserve" the port or socket so no process can take it or find a way to reload the gem on the production environment on every request.
Which option should i take? Which one is the more appropied or the more plausible?

Ok, so i finally got it to work, i put the code on boot.rb, just before the gems are loaded
require 'rubygems'
# Set up gems listed in the Gemfile.
ENV['PORT_NUMBER'] = "4333"
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

Related

How to use ActionCable with redis server hosted on redislabs.com?

I am trying to set up a rails app with actioncable, where it's using a redis database hosted on app.redislabs.com. I am having trouble getting the connection to successfully initiate, even though I can connect to a localhost redis server fine.
If I use the url redis://myredisurl, I get the following errors: Connection reset by peer (Errno::ECONNRESET) followed by Connection lost (ECONNRESET) (Redis::ConnectionError).
From a google search, the only thing I could find was possibly that the redis server wants an ssl connection. So I tried the url rediss://myredisurl, and that gives an SSL error: SSL_connect returned=1 errno=0 state=error: wrong version number (OpenSSL::SSL::SSLError)
The development section of cable.yml looks like this:
development: &development
adapter: redis
password: <%= ENV['REDIS_PASSWORD'] || '' %>
db: <%= ENV['REDIS_DB'] || '' %>
url: <%= ENV['REDIS_HOST'] || 'redis://127.0.0.1' %>
port: <%= ENV['REDIS_PORT'] || '6379' %>
ssl: true
ssl_params:
verify_mode: <%= OpenSSL::SSL::VERIFY_NONE %>
I tried with & without ssl: true. With & without ssl_params. I'm still unable to get the connection to work, and I don't know what I'm missing.
Of note, I am able to connect to the hosted redis with redis-cli, so I know it's reachable.
I am using Rails 5.2.7, and it is not possible to upgrade.
This turned out to be a corporate network firewall issue - not an issue with our app setup.

increase idle timeout for gremlin console AWS Neptune

I am on ubuntu 18, I have the gremlin console always opened, after I :remote console, I run some query, then if I keep idle for like 3 mins, the connection then got dropped, then I have to exit the current connection and reconnect, which is quite annoying.
Is there a way to increase the idle timeout
basically I need to type in these command again and again.....
bin/gremlin.sh
:remote connect tinkerpop.server conf/neptune-remote.yaml
:remote console
It looks like what you are looking for is the keepAliveInterval in Settings.
https://github.com/apache/tinkerpop/blob/6083dc4fcb214df64be72483f8779d81e73c0fac/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Settings.java#L319-L324
This key is wired inside connectionPool:
https://github.com/apache/tinkerpop/blob/6083dc4fcb214df64be72483f8779d81e73c0fac/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Settings.java#L236-L237
So, as ashoksl# pointed out in a comment already, you need to include this setting inside your remotttte YAML conf under connectionPool.
Someething like:
host: localhost
port: 8182
connectionPool: {
enableSsl: true
..
..
keepAliveInterval: 30000
}
You might also want to look at this logic of server-clint ping pong for additional knobs.
https://github.com/apache/tinkerpop/blob/ad8d663ffd957df3724c7aa8fe8bb4f893d76557/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/handler/OpSelectorHandler.java#L101-L110
Hope this helps.

apache + mod_perl + couchbase = occasional connection problems

We use couchbase as session storage for mod_perl scripts. To avoid delays on clients caused by waiting for a new connection we do preconnect to couchbase on child_init apache stage. So during apache restart / new child creation it connects to couchbase automatically and later use that connection during apche child lifetime.
Generally everything works fine, but sometimes we got the following errors during that preconnection:
Couldn't connect: 0x13 (Operation not supported) at /perl/lib64/perl5/Couchbase/Bucket.pm line 38.
Usually it appears during apache restart and on several (or dozens) of childs, and almost never on one child only. Usually restarting apache again solves the problem.
What can cause such a problems? Is it a problem with code / server configuration / couchbase server itself?
May be it caused somehow with a lot of reconnections at the same time? Some ulimits stuff / or selinux restrictions?
UPD: versions
OS:
Centos 6, 2.6.32-358.2.1.el6.x86_64
libcouchbase:
libcouchbase-devel.x86_64 2.4.7-1.el6
libcouchbase2-core.x86_64 2.4.7-1.el6
libcouchbase2-libevent.x86_64 2.4.7-1.el6
couchbase server:
2.2.0 community edition (build-837)
SDK:
perl (Couchbase::Core v2.0.2)
connection code (isolated & simplified):
# in mod_perl environment
use Couchbase;
use Couchbase::Bucket;
use Couchbase::Document;
use Apache2::ServerUtil ();
my $cb = undef;
# connection handler, initialized once, used during apache child lifetime
sub connect_couchbase_on_child_init {
my ($child_pool, $s) = #_;
my $dsn = 'couchbase://192.168.0.1,192.168.0.2/my_bucket_name?detailed_errcodes=1';
eval { $cb = Couchbase::Bucket->new($dsn); };
# here we get the occasional warnings during apache restarts
if ($#) { warn "COUCHBASE CONNECTION ERROR! $#"; $cb = undef; }
return Apache2::Const::OK;
}
Apache2::ServerUtil->server->push_handlers(PerlChildInitHandler => \&connect_couchbase_on_child_init);
# in request handlers it used with the following calls (only if connected):
# $doc = Couchbase::Document->new($key);
# $cb->get($doc);
# ...
# $cb->replace($doc);
# ...
# $cb->insert($doc);
# ...
# $cb->remove($doc);
Because you are using server 2.2.0 and because this seems to happen when you are connecting many clients at once, my theory is that you are receiving the last error from the server. The current client bootstrap process attempts using bootstrap over memcached (which is only supported from version >= 2.5.0 of the server), that fails and it attempts to use 'terse' bootstrapping (again, only supported on >= 2.5.0 of the server) and finally 'classic' HTTP (which is available on all versions).
Add the following options to your DSN/connection string to cut out some of the steps for your server. Note that should you ever upgrade to >= 2.5 these options should be removed:
bootstrap_on=http Does not try memcached bootstrap
http_urlmode=2 Uses the pre-2.5 style of bootstrapping by default
These two options will not necessarily fix your issue, but they will at least cut out some of the initial connection time, and perhaps show a clearer reason for the error (you can also set LCB_LOGLEVEL=5 in the environment to get actual logging).
In your case, the connection string would be:
couchbase://192.168.0.1,192.168.0.2/my_bucket_name?detailed_errcodes=1&bootstrap_on=http&http_urlmode=2

Rails - Curb SSLCA error? - Curl::Err::SSLCACertificateError

When I update my Rails gems I find this errors (only in production, in development environment working good):
Curl::Err::SSLCACertificateError
Seems that is an SSL Certificate Authority Error, but why only in production is not working? And what can I do to resolve the problem?
My curb gems is v.0.8.3 with rails 3.2.8
(the mayor update was the rake, that now is v.10.0.2, but I don't know if this influence the good working of the curb gem).
FYI, this is the code that raise the error:
loginData = { :login => "myuser", :password => "mypass" }
loginJson = ActiveSupport::JSON.encode(loginData)
req = Curl::Easy.http_post("https://mysite.com", loginJson
) do |curl|
curl.headers['Content-type'] = 'application/json'
end
It sounds like you're using a self-signed SSL certificate on the server, which is fine. I do the same thing for internal services. You'll just want to make your client aware of the custom SSL certificate as well, that way it knows that it can be trusted.
Something like this should do the trick:
req = Curl::Easy.http_post("https://mysite.com", loginJson) do |curl|
curl.headers['Content-type'] = 'application/json'
curl.cacert = "/path/to/ca.crt"
curl.cert = "/path/to/cert.pem"
end
Of course, you'll probably want to extract these string constants into a config file.
I find an alternative solution, when I request a "curl" I put this additional propriety:
curl.ssl_verify_peer = false
I don't know if in term of security is the best solution, but for the moment it works...

Is it possible to start resque-web listening to a redis socket?

I have disabled redis listening to port 6379 and enabled the websocket. It works wonderfully from my application, but when I launch resque-web it keeps listening trough network interface and fails with message:
Can't connect to Redis! (redis://127.0.0.1:6379/0)
Someone knows if it's possible to make resque-web use the socket instead of the network?
Thanks in advance
I've been reading resque-web's code and I realized that it internally loads any path you provide as parameter to the command. So I have created a plain ruby script that connects to Redis with redis gem and then assigns this instance to Resque.redis:
Just created a file called 'resque-web-hack.rb':
require 'redis'
require 'resque'
$redis = Redis.new(:path => '/tmp/redis.sock')
Resque.redis = $redis
And then used it like this:
$ resque-web /path/to/my/file/resque-web-hack.rb
It's just a hack, but it works for me by now...
I just fixed same problem :) So here is the solution
In my ./config/resque.yml I have this line
development: /tmp/redis.sock
This is my RAILS_ROOT/config/initializers/resque.rb
rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env = ENV['RAILS_ENV'] || 'development'
resque_config = YAML.load_file(rails_root + '/config/resque.yml')
if resque_config[rails_env] =~ /^\// # using unix socket
Resque.redis = Redis.new(:path => resque_config[rails_env])
else
Resque.redis = resque_config[rails_env]
end