I am using Redis Objects with Redis To Go on Heroku. I have a counter on a model, like this:
class Performance < ActiveRecord::Base
include Redis::Objects
counter :tickets_sold, start: 0
end
Accessing this value from Heroku console is working great as well.
irb(main):002:0> Performance.last.tickets_sold.value
Performance Load (3.9ms) SELECT `performances`.* FROM `performances` ORDER BY `performances`.`id` DESC LIMIT 1
=> 0
I confirmed that Redis.current is present:
irb(main):003:0> Redis.current
=> # Redis client v2.2.2 connected to redis://ray.redistogo.com:9023/0 (Redis v2.4.11)
However, accessing the same counter from a template on the website runs into a Errno::ECONNREFUSED error.
Connection refused - Unable to connect to Redis on 127.0.0.1:6379
Why is it trying to connect to the local Redis url? Inspecting Redis.current on the website is also failing with the connection error above. Considering that the same command is working just fine from the Heroku console, I'm a little puzzled as to what's going on here. I hope someone has seen this before and knows how to solve it...
Related
I have a code in Ruby (as a Logstash plugin) in which I connect to Redis and get information like what is suggested in (How to use redis inside logstash filter?) as below:
require "redis"
$redis = Redis.new(host: "REDIS_HOST", port: REDIS_PORT, db: REDIS_DB)
#my_data = $redis.hget("DATA_KEY", "HASH_FIELD").to_i
Everything works OK generally; however, I want to know:
1- Which possible errors might be faced by the code while connecting to Redis or getting data (e.g. Redis is unreachable or DATA_KEY or HASH_FIELD does not exist)
2- How should we handle such errors?
If I were using ActiveRecord, my puma config would establish the database connection as described in Heroku's guide Deploying Rails Applications with the Puma Web Server.
on_worker_boot do
ActiveRecord::Base.establish_connection
end
However, I am using ROM (Ruby Object Mapper).
I tried omitting the on_worker_boot block, but (predictably) the database connection is either not established, or is not established correctly, and the following error is raised.
PG::ConnectionBad: PQconsumeInput() SSL error: decryption failed or bad record mac
I've read the ROM Setup Guide but did not see anything relevant.
How do I establish the ROM connection in puma?
The solution seems to be to disconnect the gateway connection.
on_worker_boot do
ROM.env.gateways[:default].connection.disconnect
end
Read the docs, searched the stack, but I can't find a very simple
function: How do I check if the connection has been established or not?
establishing DB connection with
db = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
From time to time I forget to launch the mongodb database and then the driver tries to connect and well... does not say's a lot about the connection itself.
Is there some kind of .isConnected? method or anything to find
the connection status of current Mongo::Client instance?
Thanks in advance
Version 1.x of the Mongo ruby driver did expose a connected? method on Mongo::Connection, but it's been moved in v2 to live on Mongo::Server, however its meaning has changed somewhat.
Here's an example of using this method:
mongo = Mongo::Client.new('mongodb://localhost/mycollection')
#=> #<Mongo::Client:0x70279909414900 cluster=localhost:27017>
mongo.cluster.servers.first.connected?
#=> true
In v2 of the Mongo ruby driver, it will raise an exception when trying to use an invalid connection, for example, Mongo::Error::NoServerAvailable, based on the connection parameters about timeouts. For your purposes of simply checking whether the connection has been established, maybe something like:
mongo = Mongo::Client.new('mongodb://localhost/mycollection')
mongo.list_databases => # raise error or return an array
This question has already been asked here https://stackoverflow.com/questions/30283837/logstash-unable-to-send-output-to-elasticsearch but I can't see the actual answer. Can someone advise if there is a solution to this please as I am suffering the same problem.
I believe this is a system parameter causing the problem as I've run the same config on a different machine and it works fine. Anyone know what this might be?
I've looked everywhere to find an answer but can only find a firewall issue (so tried outside the firewall) and the time stamp being different between the twitter server and my machine (machine timing set correctly). Can anyone advise what's causing the below authorisation error (I've also checked the twitter app and settings are correct and working).
Logstash startup completed
←[33m {:exception=>Twitter::Error::Unauthorized, :backtrace=>["C:/logstash-1.5.1
/vendor/bundle/jruby/1.9/gems/twitter-5.12.0/lib/twitter/streaming/response.rb:2
1:in on_headers_complete'", "org/ruby_http_parser/RubyHttpParser.java:370:in<
<'", "C:/logstash-1.5.1/vendor/bundle/jruby/1.9/gems/twitter-5.12.0/lib/twitter/
streaming/response.rb:16:in <<'", "C:/logstash-1.5.1/vendor/bundle/jruby/1.9/ge
ms/twitter-5.12.0/lib/twitter/streaming/connection.rb:22:instream'", "C:/logst
ash-1.5.1/vendor/bundle/jruby/1.9/gems/twitter-5.12.0/lib/twitter/streaming/clie
nt.rb:116:in request'", "C:/logstash-1.5.1/vendor/bundle/jruby/1.9/gems/twitter
-5.12.0/lib/twitter/streaming/client.rb:36:infilter'", "C:/logstash-1.5.1/vend
or/bundle/jruby/1.9/gems/logstash-input-twitter-0.1.6/lib/logstash/inputs/twitte
r.rb:88:in run'", "C:/logstash-1.5.1/vendor/bundle/jruby/1.9/gems/logstash-core
-1.5.1-java/lib/logstash/pipeline.rb:176:ininputworker'", "C:/logstash-1.5.1/v
endor/bundle/jruby/1.9/gems/logstash-core-1.5.1-java/lib/logstash/pipeline.rb:17
0:in `start_input'"], :level=>:warn}←[0m
I'm using the below twitter config:-
input {
twitter {
consumer_key => "xxxxxxxxxxxxxxxxxx"
consumer_secret => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
oauth_token => "xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
oauth_token_secret => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
keywords => ["xxxxxxxxxxx"]
full_tweet => true
}
}
output {
stdout { codec => dots }
elasticsearch {
protocol => "http"
host => "localhost"
index => "twitter"
document_type => "tweet"
template => "twitter_template.json"
template_name => "twitter"
}
}
I tried the same from outside the firewall and instead of authorisation issue I get a connection refused. So I believe warkolm you were correct but that's just left me with a different problem.
←[33mConnection refused - Connection refused {:exception=># Connection refused - Connection refused>, :backtrace=>["org/jruby/ext/socket/Ru
byTCPSocket.java:126:in initialize'", "org/jruby/RubyIO.java:853:innew'", "C:
/logstash-1.5.1/vendor/bundle/jruby/1.9/gems/twitter-5.12.0/lib/twitter/streamin
g/connection.rb:16:in stream'", "C:/logstash-1.5.1/vendor/bundle/jruby/1.9/gems
/twitter-5.12.0/lib/twitter/streaming/client.rb:116:inrequest'", "C:/logstash-
1.5.1/vendor/bundle/jruby/1.9/gems/twitter-5.12.0/lib/twitter/streaming/client.r
b:36:in filter'", "C:/logstash-1.5.1/vendor/bundle/jruby/1.9/gems/logstash-inpu
t-twitter-0.1.6/lib/logstash/inputs/twitter.rb:88:inrun'", "C:/logstash-1.5.1/
vendor/bundle/jruby/1.9/gems/logstash-core-1.5.1-java/lib/logstash/pipeline.rb:1
76:in inputworker'", "C:/logstash-1.5.1/vendor/bundle/jruby/1.9/gems/logstash-c
ore-1.5.1-java/lib/logstash/pipeline.rb:170:instart_input'"], :level=>:warn}←[
0m
This is even after trying a new set of twitter authorization codes.
If this same configuration is run on another developers machine it works fine so there is a config on my desktop that isn't set correctly (both machines are running the same setup windows 8.1 running ES 1.7.1 and LS 1.5.1).
Any thoughts on what setting is missing on my machine.
Thanks
Leigh
The stack trace that you provided originates in the Twitter connection phase -- its's getting connect refused when trying to initiate a connection to stream.twitter.com:443. Since that's unlikely that the streaming api for twitter was down, you've probably got a firewall blocking you in some way.
Looking up stream.twitter.com, there are multiple A records for it... So it's possible that whomever runs your firewall missed an address or something like that. It could be that the other developer has a host file entry to force it to a specific address.
If you look at something like this it will show you what addresses a given host is associated with.
You can then try telneting to port 443 on each address to see if you get a connection or a connection refused on each one.
UPDATE: I tried changing the storage of the PostgreSQL connection object from a constant POSTGRES to a class variable ##pg. That didn't fix the issue. Then, I tried changing it to a global variable $pg. That seems to have worked! I still would like to eventually implement database connection pooling, however, so that I can have a pool of up to 5 (or so) database connections handling requests instead of just one global connection for all requests. END UPDATE
I'm hosting the Acani Chats REST Server on Heroku.
The first request works OK, but subsequent requests fail to connect to the PostgreSQL database.
I get the following errors:
Rack app error: #<PG::UnableToSend: SSL error: decryption failed or bad record mac>
Rack app error: #<PG::UnableToSend: no connection to the server>
What's going on?
In /config/application.rb, I define the constant POSTGRES to be the PostgreSQL connection object.
Should I be using a global or class variable instead of a constant to hold onto the connection instance?
In /config/routes.rb, I define the Rack call method.
I want to learn how to implement database connection pooling in Ruby for Rack with Puma and PostgreSQL.
Puma is threaded so you need a thread safe pool of connections to PostgreSQL, otherwise concurrent requests will all use the same connection concurrently, which is unexpected.
Please have a look to the connection_pool gem. It should help.