Rails Invoking a rake task dependant on :environment task, from a initializer file

2.6k views Asked by At

I'm using a rufus scheduler to replace cron jobs from the deployment system and get those jobs kick-started when an application loads on deployment.

Now I have this scheduler.rb placed in the config/initializers directory from the application root directory.

The content of the scheduler.rb file is as below:

require 'rufus/scheduler'
require 'rubygems'
require 'rake'

load File.join(RAILS_ROOT,'lib','tasks','tempfile.rake')

temp_files_cleaning_scheduler = Rufus::Scheduler.start_new

temp_files_cleaning_scheduler.cron '*/1 * * * *' do
    Rake::Task["tempfile:delete_all"].reenable
    Rake::Task["tempfile:delete_all"].invoke
end

Now when I start the application server, I get the error message as below:

scheduler caught exception :
Don't know how to build task 'environment'
/home/karthik/.rvm/gems/jruby-1.5.2/gems/rake-0.8.7/lib/rake.rb:1728:in `[]'
/home/karthik/.rvm/gems/jruby-1.5.2/gems/rake-0.8.7/lib/rake.rb:605:in `invoke_prerequisites'

where 'environment' is a dependant task for the task "tempfile:delete_all" that I'm invoking. And this :environment task is defined in railties/lib/tasks/misc.rake.

I don't want to load this misc.file by hard-coding the path to it. Is there a cleaner way to solve this problem?

Thanks.

1

There are 1 answers

0
Xavier Holt On BEST ANSWER

It sounds like you need more definitions that are in Rakefiles that aren't getting loaded, probably because "lib/tasks/tempfile.rake" doesn't have any require statements in it.

I assume this works from the command line, and if so, you have two options:

  1. Load your app's main Rakefile, which has all teh necessary includes:

    load File.join(RAILS_ROOT,'lib','tasks','tempfile.rake')

  2. Just call it as as if from the console:

    system('rake tempfile:delete_all')

Hope that helps!