Actionmailer nomethoderror with delayed_job on heroku - actionmailer

I'm creating a site that allows a user to introduce two other users. The users are then notified by email that a matching message has been created for them.
Using delayed-job works fine locally but I'm getting an odd error when deploying to heroku:
String#notify_new_match failed with NoMethodError: undefined method `notify_new_match' for "UserMailer":String
Here's the controller code:
UserMailer.delay.notify_new_match(current_user, match_email, params[:note])
And the Mailer code:
class UserMailer < ActionMailer::Base
def notify_new_match(user, email, message)
#message = message
mail(:to => email,
:subject => "#{user.first_name} wants to introduce you")
end
end
The relevant gems:
gem 'rails', "~> 3.2.0"
gem 'delayed_job_active_record'
gem 'activerecord-import'
gem 'thin'
gem 'pg'

Finally got it working again. Added this line to the Procfile.
worker: bundle exec rake jobs:work
Seems to be fine now.

Related

Couldn't load the Unicode tables for UTF8Handler in Rails Admin (1.2.0)

I want to upgrade rails 3 to rails 5. There I am using mongodb as database. I am using rails admin 1.2.0. I am using jruby-9.1.7.0. I am rails api only app. My application gems are given below.
gem 'rails', '~> 5.1.4'
gem 'awesome_print', '~> 1.8.0'
gem 'devise', github: 'plataformatec/devise', :branch => 'master', :ref => "463351922fdafb96c50ba2496c7d0adaa3223283"
gem "doorkeeper-mongodb", github: "doorkeeper-gem/doorkeeper-mongodb"
gem 'cancan', github: 'DevAVIJIT7/cancan', :branch => 'master', :ref => "aa3729bd79d92a993c5186f983eccd2fd496c2d3"
gem 'puma','~> 3.11.0'
gem 'mongoid', '6.2.1'
gem 'mongoid-tree', '~> 2.1.0'
gem 'rails_admin', '~> 1.2'
In my routes rails admin mount path and initializer file look like
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
RailsAdmin.config do |config|
RAILS_ADMIN_BASE_PATH = 'godview'
config.main_app_name = Proc.new do |controller|
[ "Admin", "#{controller.params[:action].try(:titleize)}" ]
end
config.authenticate_with do
authenticate_or_request_with_http_basic('Login required') do |username, password|
user = User.where(username: username).first
if user && user.valid_password?(password) && user.role_names.include?("Super Admin")
user
else
nil
end
end
end
end
when I run http://localhost:3000/admin on browser I am getting error.
In console I am getting
Started GET "/godview" for 0:0:0:0:0:0:0:1 at 2018-01-15 14:54:12
+0530
Processing by RailsAdmin::MainController#dashboard as HTML
Completed 401 Unauthorized in 263ms
IOError (Couldn't load the Unicode tables for UTF8Handler (undefined method
`call' for nil:NilClass
Did you mean? caller), ActiveSupport::Multibyte is unusable):
I went through the link Rails: Couldn't load the Unicode tables for UTF8Handler But didn't get any response.
*note : I am using /admin as /godview in my app.
Thanks
We are using Docker on CentOS for our rails projects, and we got this error recently, we fixed it by explicitly specifying the C.UTF8 encoding in the docker file as below:
ENV LANG C.UTF-8
Encoding
By default, Ruby inherits the locale of the environment in which it is run. For most users running Ruby on their desktop systems, that means it's likely using some variation of *.UTF-8 (en_US.UTF-8, etc). In Docker, however, the default locale is C, which can have unexpected results. If your application needs to interact with UTF-8, it is recommended that you explicitly adjust the locale of your image/container via -e LANG=C.UTF-8 or ENV LANG C.UTF-8
References:
https://hub.docker.com/_/ruby/
https://oncletom.io/2015/docker-encoding/

uninitialized constant Mongoid::Slug

this is my first development on rails, so Im still learning. I'm having a problem with mongoid_slug. In my local development mongoid_slug works fine, but when I test it on production I'm having this error:
uninitialized constant Mongoid::Slug
This is my full trace: http://pastebin.com/7x0q5VwY
If I try to save the document error appears.
This is my GemFile:
gem 'rails', '4.0.0'
gem 'mongoid', '~> 4.0.0.beta1', github: 'mongoid/mongoid'
gem 'bson_ext'
gem 'cancan'
gem 'ckeditor'
gem 'mongoid_slug', '3.2'
gem 'sass-rails', '~> 4.0.0'
gem 'bootstrap-sass', '~> 3.1.1'
This is my model:
class Noticium
include Mongoid::Document
include Mongoid::Slug
field :titulo, type: String
slug :titulo, history: true
field :contenido, type: String
field :imagen, type: String
field :imagen_url, type: String
field :tipo, type: String
field :fecha_registro, type: DateTime
validates :titulo, uniqueness: true
validates :titulo, :contenido, :imagen_url, :tipo, presence: true
end
This is part of my controller where the error appears:
private
def set_noticium
#noticium = Noticium.find(params[:id])
end
def noticium_params
params.require(:noticium).permit(:titulo, :contenido, :imagen, :tipo,
:fecha_registro)
end
I have read related info but I can't figure out what could be wrong.
My website is hosted on Digital Ocean, I have checked the version of Rails, Ruby and mongodb they are the same that in my local machine.
Could someone help me to figure whats going on? Why on my local development the slug field is working and on production im having errors.
Thanks in advance.
Maybe the MongoID version?
http://rubygems.org/gems/mongoid_slug
Runtime Dependencies
mongoid > 3.0
And you are using Mongoid 4.0
Update: see this issue on Github: https://github.com/digitalplaywright/mongoid-slug/issues/30
My bad. Have to release master, which works without requiring the
path, to RubyGems.

Sinatra App on Heroku ActiveRecord::StatementInvalid: PGError: ERROR:

SCENARIO:
I currently have anapplication on Heroku that is Sinatra Application + ActiveRecord. What currently works:
• I am able to view static pages of my site
• I am able to run migrations
• I am able to push my local development db to my Heroku DB (heroku db:push postgres://localhost/dev)
• I am able to pull from the Heroku DB down to my localhost DB (heroku db:pull postgres://localhost/dev)
THE ERROR:
When I navigate to a page that makes a simple query like:
#pics = Picture.find(:all)
I get this error in my log file
DEBUG -- : NoMethodError: undefined method `values' for #<PGresult:0x00000002844098>: SHOW client_min_messages
DEBUG -- : PGError: ERROR: invalid value for parameter "client_min_messages": ""
: SET client_min_messages TO ''
ActiveRecord::StatementInvalid - PGError: ERROR: invalid value for parameter "client_min_messages": ""
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
At this point I'm not sure what is wrong so I decided to log onto the heroku console and run this command to see if my tables are in the DB
> heroku console
> ActiveRecord::Base.connection.tables
AND I get a similar errors
ActiveRecord::StatementInvalid: PGError: ERROR: invalid value for parameter "client_min_messages": ""
: SET client_min_messages TO ''
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in execute'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
/app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
Then I decided to try this in the console...
> ActiveRecord::Base.establish_connection
> ActiveRecord::AdapterNotSpecified: ActiveRecord::AdapterNotSpecified
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:54:in `establish_connection'
/home/heroku_rack/lib/console.rb:158:in `block (2 levels) in <top (required)>'
/home/heroku_rack/lib/console.rb:148:in `eval'
/home/heroku_rack/lib/console.rb:148:in `_eval'
At this point it would seem that I have no tables and adapter connection but how can that be when I'm able to run migrations and push and pull from the Heroku DB?
UPDATE 1 - NOT MUCH FURTHER ALONG BUT MORE INSIGHT
So I decided to start from scratch and do this over again. I seem to have made some progress but I'm stuck. At re-ran my scenario I mentioned above and succeed. So my database, migration and data are all in the DB. But my app still cannot connect to the DB. BUT what's weird is my console interaction.
> heroku console
> ActiveRecord::Base.connection_config
> {:adapter=>"postgresql", :database=>"1e2e12e21e", :username=>"1e2e12e21e", :password=>"i2j3i23joo12", :host=>"1e2-12e-2e1212-e2e2e1.compute-1.amazonaws.com", :port=>5432, :encoding=>"unicode"}
NOTE: I replaced the real details with this fake stuff just to illustrate
> ActiveRecord::Base.connection_handler
> #<ActiveRecord::ConnectionAdapters::ConnectionHandler:0x00000002f8a578 #connection_pools={"….
NOTE: This a really long string but it has content
> ActiveRecord::Base.connected?
> false
At this point I really don't get it? I don't get how I can keep running db migrations, keeping push and pulling data, log into Heroku Console see my connection_config details, BUT when I run my app internal server error. AND when I run ActiveRecord::Base.connection.tables in heroku console I get ActiveRecord::StatementInvalid: PGError: ERROR: invalid value for parameter "client_min_messages": ""
Any thoughts on what I could be doing wrong?
FINAL UPDATE
Here is what the problem is:
Heroku support kindly pointed to me to a known issue with activerecord 3.1.3. It's a bug that should be fixed in future updates: https://github.com/rails/rails/commit/92a3c487bb0d125c437e53a7f45c31fcca97f2d9
How to solve the problem:
I decided to use a much lower version of activerecord in my gemfile gem "activerecord", "~> 3.0.9 and my site is up and running with no problems so far.
Why I never tried this out before I don't know. I hope this post anyone else.
I had this problem for a long time and it was driving me nuts. I couldn't find a solution anywhere (and many rails updates passed by not solving the "problem"), so i decided to just hunt down the issue myself. It turned out to be an easy fix. Just add an additional key, value pair: :min_messages => "warn" (or whatever message level you want) to the ActiveRecord setup in your configure block. here's what I'm using in my Sinatra app's right now, using Sinatra 1.3.2 and ActiveRecord 3.2.2:
db = URI.parse(ENV['DATABASE_URL'] || "postgres://user:password#localhost/database")
adapter = (db.scheme == "postres") ? "postgresql" : db.scheme
ActiveRecord::Base.configurations[:production] = {
:adapter => adapter,
:username => db.user,
:password => db.password,
:port => db.port,
:database => db.path.sub(%r(^/),""),
:host => db.host,
:min_messages => "warn"
}
unless ActiveRecord::Base.connected?
ActiveRecord::Base.establish_connection ActiveRecord::Base.configurations[:production]
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
hope this helps you and any other people having these issues!
None of the other suggestions worked for me. I ultimately fixed this by switching to the Heroku cedar stack:
heroku create your app --stack cedar
I had this problem on both bamboo-ree-1.8.7 and bamboo-mri-1.9.2 stacks. The application worked fine on localhost, and migrated OK on heroku, but reading the database inside the heroku web stack failed with a client_min_messages complaint.
Setting the :min_messages (as suggested in another answer) to either "warn" or "warning" produced different errors.
Forcing my ActiveRecord down to 3.0.11 (from 3.2.3) resolved the issue. (Not that I see anything in AR to explain this. I speculate it's related to differing postgres versions ... I'm using 9.0.3, suspect the heroku bamboo shared is 8.4)
Pushing this up in my gem file worked for me.
group :production do
gem 'pg'
gem 'activerecord', '~> 3.0.9'
end

how to solve 'Mysql2::Error: This connection is still waiting for a result' error with mysql2 and activerecord

Not duplicate of this question with the same title
I am using activerecord with mysql2 and I am designing to handle 10 queries to the same activerecord model/class at a time. Please note I am using strict activerecord and not using mysql queries directly.
I get the calls in Sinatra and then use activerecord to get the data from the DB.
I don't wan't the calls to be blocking so therefore I used mysql2 and I do NOT want to use em-synchrony.
But now I get the following "Mysql2::Error: This connection is still waiting for a result, try again once you have the result:" on subsequent simultanous calls.
I am not establishing connection with pool=10
my class
class User < ActiveRecord::Base
and my code to call
user.find(:all, :conditions => ["id=?", userid])
The mysql2 doc says "To use the ActiveRecord driver (with or without rails), all you should need to do is have this gem installed and set the adapter in your database.yml to "mysql2". That was easy right? :)"
And that is exactly what I have done when I moved from mysql to mysql2.
Why am I getting this error.
Here is a fully working example:
require 'rubygems'
gem 'activerecord', '~> 3.1.0'
gem 'sinatra', '~> 1.3.1'
gem 'mysql2', '~> 0.3.11'
require 'active_record'
require 'sinatra/base'
require 'mysql2'
# thin use the eventmachine thread pool
# you should have at least one connection per thread
# or you can expect errors
EM::threadpool_size = 10
# connect to the database
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:database => "test",
:username => "root",
:encoding => 'utf8',
# number of connections openened to the database
:pool => 10
)
class App < Sinatra::Base
get '/db' do
ActiveRecord::Base.connection.execute("SELECT SLEEP(1)")
end
end
run App
to run it save the file as "config.ru" and run it with thin in threaded mode:
thin start -e production --threaded
You can use ab to check that everything is working, I used a tool called siege:
siege -c 10 -r 1 http://localhost:3000/db
You should use a ConnectionPool... Somehow you have 2 connections in a race condition.
I dont use Sinatra, I use Rails, but I had the same problem and solved it like that:
# class ActiveRecord::Base
# mattr_accessor :shared_connection
# ##shared_connection = nil
#
# def self.connection
# ##shared_connection || retrieve_connection
# end
# end
#
# ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
class ActiveRecord::Base
mattr_accessor :shared_connection
##shared_connection = nil
def self.connection
##shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
You have to use a connection pool. mysql2 allows the queries to be async, but you can still only send one query at a time to MySQL through one connection. If you send multiple queries through one connection you get the waiting for results message.
Use a connection pool and you should be fine.

Sinatra app on Passenger can't find Rack (using RVM)

I've been fighting with a staging server for this Sinatra app for two days. I'm getting pretty frustrated. The latest pothole is a Ruby error handed up by Passenger:
no such file to load -- rack
The nginx.conf file shows which Ruby we're using, and where Passenger is located:
passenger_root /usr/local/rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.5;
passenger_ruby /usr/local/rvm/rubies/ruby-1.9.2-p180/bin/ruby;
OK, so that's Ruby 1.9.2 for us.
The vhost configuration includes:
root /var/www/staging-proweb/current/rack/public;
passenger_enabled on;
rack_env staging;
and the rackup file has:
require 'rubygems'
require 'sinatra'
require 'myapp1.rb'
run Sinatra::Application
which then sends us to the myapp1.rb file, which includes
require 'rubygems'
require 'rack'
require 'sinatra'
...and I assume the sinatra require is what's calling for rack.
The gem's there:
~$ which ruby
/usr/local/rvm/rubies/ruby-1.9.2-p180/bin/ruby
~$ gem list rack
*** LOCAL GEMS ***
rack (1.2.2)
rack-flash (0.1.1)
The error seems to be in a file in the RVM directory, judging from the first three lines of the stack trace:
0 /usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb 36 in `require'
1 /usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb 36 in `require'
2 /usr/local/rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.5/lib/phusion_passenger/rack/application_spawner.rb 219 in `load_rack_app'
What should I try next? I'm pretty stumped at this point.
It looks like the issue was with the passenger_ruby configuration value. RVM provides a wrapper to set up the appropriate environment for that Ruby (including the gems), so the passenger_ruby line should have read
passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.2-p180/ruby;
With that in place, we seem to be up and running.