Could not run Rake Task in Production - ruby-on-rails

I have a rake task which is called by a third party service. In my application I need to set Customer before starting the application. I could not access the model variable from the rake task. But this happens only in Production not in Development. When I run
RAILS_ENV = 'production' rake auth:tester
I get the followng error.
rake aborted!
Unable to autoload constant CUSTOMER, expected /home/suganya/academics/tsg-mcds-server/app/models/customer.rb to define it
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:495:in `load_missing_constant'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:184:in `const_missing'
/home/suganya/academics/tsg-mcds-server/config/initializers/customer_config.rb:11:in `xml'
/home/suganya/academics/tsg-mcds-server/config/initializers/customer_config.rb:38:in `set_customer_settings'
/home/suganya/academics/tsg-mcds-server/config/initializers/customer_config.rb:710:in `<top (required)>'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `block in load'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.2.7.1/lib/rails/engine.rb:652:in `block in load_config_initializer'
I have referred all the related questions. When I enable in poduction.rb
config.threadsafe!
config.dependency_loading = true if $rails_rake_task
I get the following error.
undefined method `threadsafe!' for #<Rails::Application::Configuration:0x00000004aebdf8>
Also I tried
Rails.application.eager_load!
this is my rake task:
namespace :authentication do
desc "Automatically runs authentication tester"
task :tester => :environment do
Rake.application.rake_require "#{Rails.root}/config/initialize_customer_in_development.rb"
begin
AuthenticationTester.perform
rescue
SystemEvent.error(50049, "Authentication Tester Finished Unsuccessfully.")
end
end
end
Nothing helped. please help to solve this problem.

Related

How can I fix the "class variable ##debug_missing_translation of ActionView::Base is overtaken by Object" error when running any task with ruby 3.0?

I am trying to upgrade a Rails 6.1.3.1 application from Ruby 2.6.6 to 3.0.0
All the rspec tests run fine and the in development everything seems to work just fine except one thing:
Even the simplest of tasks fails with this error:
"class variable ##debug_missing_translation of ActionView::Base is overtaken by Object"
For example, this simpletask.rake file
task simpletask: :environment do
puts 'Hello'
end
is not able to run as I get the following error:
lxxx#xxx:~/Workspace/edumino$ rails simpletask
rails aborted!
class variable ##debug_missing_translation of ActionView::Base is overtaken by Object
/home/xxx/railsapp/config/environment.rb:5:in `<top (required)>'
/home/xxx/railsapp/bin/rails:5:in `require'
/home/xxx/railsapp/bin/rails:5:in `<top (required)>'
/home/xxx/railsapp/bin/spring:10:in `require'
/home/xxx/railsapp/bin/spring:10:in `block in <top (required)>'
/home/xxx/railsapp/bin/spring:7:in `<top (required)>'
Tasks: TOP => simpletask => environment
(See full trace by running task with --trace)
...so, it turns out the problem was an already existing task. I don't remember exactly why this was necessary but the task had this:
# frozen_string_literal: true
require "#{Rails.root}/app/helpers/cron_helper"
require "#{Rails.root}/app/helpers/notifier_helper"
require "#{Rails.root}/app/helpers/application_helper"
include ApplicationHelper
include ActionView::Helpers::TranslationHelper
include CronHelper
namespace :cron do
task sms: :environment do |_t, _args|
...
end
end
I assume that the guilty code was including the ActionView::Helpers::TranslationHelper
Again, I can't remember why I had to do that at that time. But if you bump into this kind of 'overtaken by' error when upgrading to Ruby 3.0, check your other tasks and see what kinda weird includes you might have had. In my case, since I didn't need the old task, I just removed it.

Devise config modification based on subdomain / request

I'm wondering how I can go about something like this in the devise config.rb.
if request.subdomain == 'app'
config.case_insensitive_keys = [:username]
elsif request.subdomain == 'support'
config.case_insensitive_keys = [:email]
end
The use here is that I want system users to only be able to login with their employee_id's (username) because their email addresses can appear across multiple accounts so I needed a way to log them in and register without the devise email validations catching them, however I want the backend group to login only by email (me and future devs) as there should never be more than one user there with multiple emails
this is failing when I try to drop my database for some reason?
STACK TRACE
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type.rb:27: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type/adapter_specific_registry.rb:9: warning: The called method `add_modifier' is defined here
rails aborted!
SyntaxError: /Users/taurenltd/Desktop/Development/roadze-base/config/initializers/devise.rb:301: syntax error, unexpected end-of-input, expecting `end'
/Users/taurenltd/Desktop/Development/roadze-base/config/environment.rb:5:in `<main>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/rails:9:in `<top (required)>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:drop => db:load_config => environment
(See full trace by running task with --trace)
taurenltd#Shawns-MBP roadze-base % rails db:drop db:create db:migrate
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type.rb:27: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type/adapter_specific_registry.rb:9: warning: The called method `add_modifier' is defined here
rails aborted!
NameError: undefined local variable or method `request' for main:Object
Did you mean? require
/Users/taurenltd/Desktop/Development/roadze-base/config/initializers/devise.rb:55:in `block in <main>'
/Users/taurenltd/Desktop/Development/roadze-base/config/initializers/devise.rb:5:in `<main>'
/Users/taurenltd/Desktop/Development/roadze-base/config/environment.rb:5:in `<main>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/rails:9:in `<top (required)>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Got this working by using the following block
def self.matches?(request)
if request.subdomain.present? && request.subdomain == 'app'
config.case_insensitive_keys = [:username]
elsif request.subdomain.present && request.subdomain == 'support'
config.case_insensitive_keys = [:email]
end
end

Rails error Uninitialized constant importing csv

This is my first time importing a csv file to my rails app.
I have the code below in /lib/tasks/import.rake
require 'csv'
CSV.foreach("lib/articles.csv", headers: true, encoding: "ISO8859-1") do |row|
Article.new(title: row["Title"], body: row["Body"], user: User.find(1))
end
When I run rake import:articles
I get this error:
NameError: uninitialized constant Article
/Users/justinMgrant/code/hrsurvival/lib/tasks/import.rake:8:in `block in <top (required)>'
/Users/justinMgrant/code/hrsurvival/lib/tasks/import.rake:7:in `<top (required)>'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:658:in `block in run_tasks_blocks'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:658:in `each'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:658:in `run_tasks_blocks'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application.rb:452:in `run_tasks_blocks'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:453:in `load_tasks'
/Users/justinMgrant/code/hrsurvival/Rakefile:6:in `<top (required)>'
(See full trace by running task with --trace)
Any idea what I’m doing wrong?
The problem is that you're not actually defining your task in your rakefile. This should work for you to be able to run rake import:articles.
namespace :import do
desc 'An optional description for what the task does'
task :articles => :environment do
# your code goes here
end
end
rake import:articles is saying to look for a task called articles within a namespace called import, which is why the namespace is necessary for what you're currently trying.
And as #max mentioned, utilizing task :articles => :environment is what tells the task to run in the context of your Rails environment, which would make your Articles model and any other model available to you in that task.

undefined method `each' for false:FalseClass

When I do rake db:migrate I get this error:
rake db:migrate
rake aborted!
NoMethodError: undefined method `each' for false:FalseClass
/home/chirag/social-login-in-rails/config/application.rb:25:in `<class:Application>'
/home/chirag/social-login-in-rails/config/application.rb:10:in `<module:SociaLoginRails>'
/home/chirag/social-login-in-rails/config/application.rb:9:in `<top (required)>'
/home/chirag/social-login-in-rails/Rakefile:4:in `require'
/home/chirag/social-login-in-rails/Rakefile:4:in `<top (required)>'
/home/chirag/.rvm/gems/ruby-2.0.0-p0/bin/ruby_executable_hooks:15:in `eval'
/home/chirag/.rvm/gems/ruby-2.0.0-p0/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
I have cloned this app :- https://github.com/mohitjain/social-login-in-rails
Rename the file 'social_keys_example.yml' to 'social_keys.yml' and put your app keys then restart the sever, it will work
in config/application.rb
change
social_keys = File.join(Rails.root, 'config', 'social_keys.yml')
to
social_keys = File.join(Rails.root, 'config', 'social_keys_example.yml')
follow the instructions
http://codebeerstartups.com/2013/10/social-login-integration-with-all-the-popular-social-networks-in-ruby-on-rails/
In the config directory there is a file ie social_keys.yml. You need
to specify all the social keys there. Make sure have restarted the
application after making any change in that file.
Here are the urls from where you can create an app and get the keys

uninitialized constant APN::App::RAILS_ENV

I'm struggling with this error after I fire the command rake apn:notifications:deliver on the APN on Rails gem that I've installed.
It's barking about the RAILS_ENV variable. I've tried a couple of forks that change RAILS_ENV to Rails.env but I still get the same error. I've posted my issue over on that repo hoping I might get somewhere.
I don't know enough about rails to dig in any further. I'm hoping someone could point me in the right direction so that I can a) better understand what went wrong and b) fix the problem.
I am using bundler and I'm pointing my gem file to the git repo: rake apn:notifications:deliver
air:apnapp azcoov$ rake apn:notifications:deliver --trace
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/version.rb:2: warning: already initialized constant VERSION
** Invoke apn:notifications:deliver (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute apn:notifications:deliver
rake aborted!
uninitialized constant APN::App::RAILS_ENV
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:11:in `cert'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:22:in `send_notifications'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:32:in `send_notifications'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:31:in `each'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:31:in `send_notifications'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/rails/../tasks/apn.rake:7
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/bin/rake:33
/usr/bin/rake:19:in `load'
/usr/bin/rake:19
Tasks: TOP => apn:notifications:deliver
Which version of Rails to you use? I just ran into this issue too. My Rails knowledge is not very deep, but it seems like the problem is deprecation of RAILS_* environment variables. I use Rails 3.1.0 and I do NOT have:
RAILS_ROOT/ENV/blah
in my environment. Instead, I have:
Rails.root/env/blah
When I replace RAILS_ENV with Rails.env in app/models/apn/app.rb the problem goes away.
So, is this gem compatible with Rails 3.1.0??? It seems not. Is there a workaround that will allow its use in a Production environment? Maybe define somewhere:
APN::App::RAILS_ENV = Rails.env
But where?
UPDATE:
I got it working with a workaround described by garyfoster here: https://github.com/PRX/apn_on_rails/issues/53.
To summarize, three things:
1) Add this ghetto hack to your config/environment.rb file:
APN::App::RAILS_ENV = Rails.env
2) Insert your certs in the APN::App table. I wrote a one-time rake task for that:
task :init_apn_certs => [:environment] do # NOTE: One-time task...
print "Creating APN::App with certs..."
app = APN::App.create(:apn_dev_cert => Rails.root.join('config', 'apple_push_notification_development.pem').read,
:apn_prod_cert => Rails.root.join('config', 'apple_push_notification_production.pem').read)
puts (app.valid? ? "done" : "failed")
end
3) Write your own rake task that bypasses the APN::App.send_notifications method, as that barfs with another error (something about '/config/apple_push_notification_development.pem' not existing). My code for it:
task :deliver_notifications => [:environment] do
print "Delivering APNs for app ids..."
apps = APN::App.all
apps.each do |app|
app.send_notifications
print app.id.to_s + "..."
end
puts "done."
end
Or, if you have only one app, you can just do this and be done with it:
task :deliver_notifications => [:environment] do
APN::App.first.send_notifications
end
DISCLAIMER: I'm yet to test this on Staging and Production (will do so by the end of the week), but I see no reason why it shouldn't work.

Resources