airbrake don't working by Rake task - ruby-on-rails

ENV
Rails 3.2.13
ruby 1.9.3p392
airbrake 3.1.11
rake, version 10.0.4
my config file and rake task file
#lib/tasks/raise.rake
namespace :raise do
desc "raise sth"
task :sth => :environment do
num = 1 / 0 # should raise error
end
end
end
# config/initializers/airbrake.rb
Airbrake.configure do |config|
config.rescue_rake_exceptions = true
config.api_key = Setting.airbrake_api
end
I hope run RAILS_ENV=production rake raise:sth send message to my airbrake, but nothing happen, just output divided by 0, not anything call stack info.
please help me, thank you ~

Related

Rails console/rake detect in initializer. defined?(Rails::Console) does not working

I have an initializer in my rails app which I don't want to be started with rails console or rake task.
I put this code into my initializer:
puts "start"
if defined?(Rails::Console)
puts "console"
elsif File.basename($0) == "rake"
puts "rake"
end
puts "end"
When I run 'rails console' I get this:
$ rails console
start
end
Loading development environment (Rails 4.2.0.beta2)
[1] pry(main)>
But inside console:
[1] pry(main)> defined?(Rails::Console)
=> "constant"
However, it works for rake:
$ bundle exec rake routes
start
rake
end
Why it works inside console, but doesn't in initializer?
Or is there a better way to determine console/rake inside initializer?

rake sunspot:solr:start throwing an error

I'm trying to use sunspot with Rails 4 and I'm running into some issue. When I have only gem 'sunspot_rails', '2.0.0' in my gemfile I get this error:
Note: This task has been moved to the sunspot_solr gem. To install, start and
stop a local Solr instance, please add sunspot_solr to your Gemfile:
group :development do
gem 'sunspot_solr'
end
But when I add that gem(also v 2.0.0) I get this error:
rake aborted!
Don't know how to build task 'sunspot:solr:start'
/home/toasty/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `eval'
/home/toasty/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `<main>'
(See full trace by running task with --trace)
I have seen this question:
Sunspot/Solr raketasks not loading in Rails 3 Mountable Engine
but it doesn't seem to work in my case. Does anyone have any ideas? Is sunspot_solr just not compatible with rails 4?
I had this same problem. I don't recall but I found this rake task that you need to add
lib/tasks/solr.rake
namespace :sunspot do
namespace :solr do
desc 'Start the Solr instance'
task :start => :environment do
case RUBY_PLATFORM
when /w(in)?32$/, /java$/
abort("This command is not supported on #{RUBY_PLATFORM}. " +
"Use rake sunspot:solr:run to run Solr in the foreground.")
end
if defined?(Sunspot::Rails::Server)
Sunspot::Rails::Server.new.start
else
Sunspot::Solr::Server.new.start
end
puts "Successfully started Solr ..."
end
desc 'Run the Solr instance in the foreground'
task :run => :environment do
if defined?(Sunspot::Rails::Server)
Sunspot::Rails::Server.new.run
else
Sunspot::Solr::Server.new.run
end
end
desc 'Stop the Solr instance'
task :stop => :environment do
case RUBY_PLATFORM
when /w(in)?32$/, /java$/
abort("This command is not supported on #{RUBY_PLATFORM}. " +
"Use rake sunspot:solr:run to run Solr in the foreground.")
end
if defined?(Sunspot::Rails::Server)
Sunspot::Rails::Server.new.stop
else
Sunspot::Solr::Server.new.stop
end
puts "Successfully stopped Solr ..."
end
# for backwards compatibility
task :reindex => :"sunspot:reindex"
end
end
EDIT~ Source of Rakefile

How and where i can define wkhtmltopdf path for rake task running through whenever cron job Rails3

I want to generate pdf using rake task running through whenever cron job rails.When i running this rake task by this command on console it is generating pdf fine
bundle exec rake auto_invoice_email
if i give this command
which wkhtmltopdf
then i have this path
"/usr/bin/wkhtmltopdf"
But when i run it through cron job then i have this error
rake aborted!
Failed to execute:
"/usr/bin/wkhtmltopdf" -q - -
Error: PDF could not be generated!
Tasks: TOP => auto_invoice_email
(See full trace by running task with --trace)
how and where i can specify path for wkhtmltopdf for cron job
Any help?
here is rake task
require 'rubygems'
require 'wicked_pdf'
require 'erb'
require 'date'
task :auto_invoice_email => :environment do
#$PATH = '/usr/bin/wkhtmltopdf'
include ApplicationHelper
users = User.find_subscribers
unless users.blank?
users.each do |user|
#user = user
content = File.read "#{Rails.root}/app/views/accountings/generate_invoice_pdf_rake.html.erb"
template = ERB.new(content)
html_content = template.result(binding)
pdf= WickedPdf.new.pdf_from_string(html_content, :wkhtmltopdf => '/usr/bin/wkhtmltopdf')
name = "#{Rails.root}/public/#{user.subscriber.downcase+" "+Time.now.to_s}.pdf"
File.open(name, 'wb') do |file|
file << pdf
end
end
end
end
Thanks

Load resque worker without rails environment?

The resque jobs I have do not depend on anything in Rails, but I'm having a hard time starting workers without the rails env. I've seen this post, but it didn't help (ruby resque without loading rails environment)
Here is my current rake file:
require "resque/tasks"
task "resque:setup" do
root_path = "#{File.dirname(__FILE__)}/../.."
require "#{root_path}/app/workers/myworker.rb"
end
#task "resque:setup" => :environment
The commented task would load the Rails env and everything works, but that's not what I want. When running rake resque:work I get this error:
rake aborted!
No such file to load -- application_controller
Tasks: TOP => resque:work => resque:preload
If you've only added a lib/tasks/resque.rake file and haven't modified your Rakefile, you'll still be loading your Rails environment when you call rake resque:work. Try this for Rakefile:
unless ENV['RESQUE_WORKER'] == 'true'
require File.expand_path('../config/application', __FILE__)
My::Application.load_tasks
else
ROOT_PATH = File.expand_path("..", __FILE__)
load File.join(ROOT_PATH, 'lib/tasks/resque.rake')
end
And then this for your resque.rake file:
require "resque/tasks"
task "resque:setup" do
raise "Please set your RESQUE_WORKER variable to true" unless ENV['RESQUE_WORKER'] == "true"
root_path = "#{File.dirname(__FILE__)}/../.."
require "#{root_path}/app/workers/myworker.rb"
end
Then call rake resque:work RESQUE_WORKER=true
I referred the link here It worked perfectly for me:
This error was resolved, by running
$> QUEUE=* rake environment resque:work
a cleaner solution was to define a rake task:
task "resque:setup" => :environment do
ENV['QUEUE'] ||= '*'
#for redistogo on heroku http://stackoverflow.com/questions/2611747/rails-resque-workers-fail-with-pgerror-server-closed-the-connection-unexpectedl
Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
end
and now
rake resque:work
Worked perfectly
Thanks.

rufus-scheduler fails with rake tasks (Don't know how to build task)

I'm using Rails3 (Windows, Ruby 1.8.7) with rufus-scheduler gem. Gem works fine, but if I'm trying to run some standard rake task, error occurs:
Don't know how to build task 'db:version' # ofc, db:version is just example
Terminal command
rake -T
works
If I'm trying to define own simple rake commands, they works fine too:
# /lib/my_scheduler.rb
require 'rubygems'
require 'rake'
require 'rufus/scheduler'
load File.join( Rails.root, 'lib', 'tasks', 'my_own_tasks.rake')
scheduler = Rufus::Scheduler.start_new
scheduler.every '5s' do
Rake::Task["my_own_namespace:test"].invoke
end
end
# /lib/tasks/my_own_tasks.rb
namespace :my_own_namespace do
task :test do
puts "Some scheduler task"
end
end
... but using standard rake tasks *in my_own_tasks* throws the same error.
Some help would be appreciated
PS. I'm newbie, so sorry, if that was dumb question
Maybe someone will need solution:
system("rake namespace:task")
f.e:
system("rake db:version")

Resources