Setting up a scheduled rake task using Whenever gem.
Required to switch to a specific schema (Using Apartment gem in my project)
Code in config/schedule.rb
env :PATH, ENV['PATH']
env :GEM_PATH, ENV['GEM_PATH']
set :output, "#{Whenever.path}/log/scheduler.log"
every 1.minute do
rake "db:my_task"
end
Code in lib/tasks/my_task.rake
namespace :db do
task :my_task => :environment do
Apartment::Tenant.switch("subdomain") do
#My Code here
end
end
end
Produces the following error in logs log/scheduler.logs
rake aborted!
Apartment::TenantNotFound: One of the following schema(s) is invalid: "subdomain" "public"
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/postgresql_adapter.rb:72:in `rescue in connect_to_new'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/postgresql_adapter.rb:65:in `connect_to_new'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:91:in `block in switch!'
/var/lib/gems/2.3.0/gems/activesupport-5.1.3/lib/active_support/callbacks.rb:97:in `run_callbacks'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:88:in `switch!'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:105:in `switch'
/home/user1/Desktop/SPERICORN/kidversity/lib/tasks/dynamic_age_setter.rake:4:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
ActiveRecord::StatementInvalid: Could not find schema kochi
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/postgresql_adapter.rb:66:in `connect_to_new'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:91:in `block in switch!'
/var/lib/gems/2.3.0/gems/activesupport-5.1.3/lib/active_support/callbacks.rb:97:in `run_callbacks'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:88:in `switch!'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:105:in `switch'
/home/user1/Desktop/SPERICORN/kidversity/lib/tasks/dynamic_age_setter.rake:4:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:dynamic_age_setter
(See full trace by running task with --trace)
you are missing ActiveRecord::Base.establish_connection.connection try this
sample code below :
namespace :debtor_email_data_fix do
desc 'update data'
task email_normalize_to_downcase: :environment do
create_connection_with_db
Apartment::Tenant.switch!('app')
Email.find_each do |t|
t.update(address: t.address.downcase)
end
end
task all: [:email_normalize_to_downcase] do
end
private
def create_connection_with_db
ActiveRecord::Base.establish_connection.connection
end
end
Related
I created a rake task for import a csv with smarter_csv gem.
Throws the following message, i added the entire error code if it helps:
rake import_csv:import_csv
Digest::Digest is deprecated; use Digest
I, [2017-03-01T11:46:08.551994 #7029] INFO -- : ** [Raven] Raven 0.14.0 configured not to send errors.
rake aborted!
NoMethodError: undefined method `close' for nil:NilClass
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:223:in `ensure in process'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:223:in `process'
/home/my_user/my_app/lib/tasks/import_csv.rake:7:in `block (2 levels) in <top (required)>'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `eval'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `<main>'
Errno::ENOENT: No such file or directory # rb_sysopen - /csv_imports/pacientes.csv
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `initialize'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `open'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `process'
/home/my_user/my_app/lib/tasks/import_csv.rake:7:in `block (2 levels) in <top (required)>'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `eval'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => import_csv:import_csv
(See full trace by running task with --trace)
This is the code on the rake task:
namespace :import_csv do
desc "import csv file"
task import_csv: :environment do
require 'smarter_csv'
filename = '/csv_imports/file.csv'
options = {:key_mapping => { :downcase_header => true }}
SmarterCSV.process(filename, options) do |array|
CsvImports.create( array.first )
end
end
end
This solved just witha dot. route issue.
./csv_imports/file.csv
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.
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.
I am trying to make rake task to populate db from JSON API fixer.io,
but when i type my rake :
rake db:populate
this error occurs:
OpenURI::HTTPError: 500 Internal Server Error
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:16:in `block (5 levels) in <top (required)>'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:13:in `block (4 levels) in <top (required)>'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:12:in `each'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:12:in `block (3 levels) in <top (required)>'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:11:in `each'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:11:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:populate
(See full trace by running task with --trace)
This is my rake task (populate.rake) in lib/tasks:
require 'open-uri'
namespace :db do
desc "Erase and fill database"
task :populate => :environment do
require 'populator'
[ConversionRate].each(&:delete_all)
n = JSON.load(open('http://api.fixer.io/latest'))["rates"].keys.count
currencies = JSON.load(open('http://api.fixer.io/latest'))["rates"].keys
currencies.each do |curr1|
currencies.each do |curr2|
ConversionRate.populate 1 do |cr|
cr.currency1 = curr1
cr.currency2 = curr2
cr.conversion_rate = JSON.load(open('http://api.fixer.io/latest?base=' + curr1))["rates"][curr2]
end
end
end
end
end
Please help me, I have no idea what causes this problem.
the main problem is here, you are not taking keys of "rates"
currencies = JSON.load(open('http://api.fixer.io/latest')).keys
currencies = JSON.load(open('http://api.fixer.io/latest'))["rates"].keys
require 'open-uri'
namespace :db do
desc "Erase and fill database"
task :populate => :environment do
require 'populator'
[ConversionRate].each(&:delete_all)
latest_data = JSON.load(open('http://api.fixer.io/latest'))
currencies = latest_data["rates"].keys
n = currencies.count
currencies.each do |curr1|
currencies.each do |curr2|
ConversionRate.populate 1 do |cr|
#take care of any error, as we are going to call third party api here
begin
cr.currency1 = curr1
cr.currency2 = curr2
cr.conversion_rate = JSON.load(open('http://api.fixer.io/latest?base=' + curr1))["rates"][curr2]
rescue => e
puts "error #{e}"
end
end
end
#give a bit rest
sleep 2
end
end
end
I'm following the directions here to integrate Sidekiq with Capistrano on Ubuntu: https://github.com/mperham/sidekiq/wiki/Deploying-to-Ubuntu
I'm having an issue with the Capistrano part.
I put this code at the end of deploy.rb:
# For capistrano 3
namespace :sidekiq do
task :quiet do
# Horrible hack to get PID without having to use terrible PID files
puts capture("kill -USR1 $(sudo initctl status workers | grep /running | awk '{print $NF}') || :")
end
task :restart do
execute :sudo, :initctl, :restart, :workers
end
end
after 'deploy:starting', 'sidekiq:quiet'
after 'deploy:reverted', 'sidekiq:restart'
after 'deploy:published', 'sidekiq:restart'
And I get this error when I run cap production deploy:
cap aborted!
NoMethodError: undefined method `capture' for main:Object
config/deploy.rb:67:in `block (2 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/dsl/task_enhancements.rb:12:in `block in after'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/dsl.rb:15:in `invoke'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/tasks/framework.rake:65:in `block (2 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/tasks/framework.rake:64:in `each'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/tasks/framework.rake:64:in `block in <top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/application.rb:15:in `run'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/bin/cap:3:in `<top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2#project/bin/cap:23:in `load'
/Users/user/.rvm/gems/ruby-2.1.2#project/bin/cap:23:in `<main>'
/Users/user/.rvm/gems/ruby-2.1.2#project/bin/ruby_executable_hooks:15:in `eval'
/Users/user/.rvm/gems/ruby-2.1.2#project/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => sidekiq:quiet
(See full trace by running task with --trace)
The deploy has failed with an error: #<NoMethodError: undefined method `capture' for main:Object>
Obviously, it's the capture function that's undefined, but what is the problem? Did I put the code in the right file deploy.rb? Do I need to require something?
You have to specify which server to run the code on. For instance:
namespace :sidekiq do
task :quiet do
# Horrible hack to get PID without having to use terrible PID files
on roles(:app) do
puts capture("kill -USR1 $(sudo initctl status workers | grep /running | awk '{print $NF}') || :")
end
end
task :restart do
on roles(:app) do
execute :sudo, :initctl, :restart, :workers
end
end
end
Try to require the sidekiq gem, or take a look at capistrano with sidekiq integration