While running a rake task in rails 3.1.12 and ruby 1.9.3 ie
rake output:generate_files["abc"] --trace>>test1.txt
rake task is working fine , but the test1.txt is being appended when this rake task is executed again.
So I would like to know whether there is a method so that each time this rake task is executed it overwrites my test1.txt so that i need not clear this file again and again while running the rake task
Just delete the file if present before doing any write in the rake task:
require 'FileUtils'
FileUtils.rm(path_to_txt_file)
# do your job
Related
I have my own secret key generating script that I've placed in a rake command. I can execute this on its own without starting the rails server, but I want to incorporate this rake task into the rails server initiation so I don't need to call the rake task by itself anymore.
Where/How do I run this rake command such that it's part of the initiation process?
If the secret key rake task doesn't have the environment dependency, you can just create a new initializer in config/initializers and name it secret_generate.rb for example, and in it write:
`rake your_task_name`
This will run your rake task on rails server initiation.
Here are the first few lines of my rake task:
desc "Import National Data into DB\n Usage:
rake import_regional[/Users/am/0925/xyz.csv, some_market]"
task :import_regional, [:file_path, :market] => [:environment] do |t, args|
.......
Here is how Im trying to run this task on the command line:
rake import_regional ["/Users/xyz.csv", "northeast"]
when I try this, I get this error
rake aborted!
Don't know how to build task
'import_regional[/Users/xyz.csv,'
I suspect it's the way in which you're running the task on the command-line; the [] doesn't have its Ruby meaning, there. Instead, maybe see if this works for you:
rake "import_regional[/Users/xyz.csv, northeast]"
This should still allow for spaces, etc. in :file_path, if required.
Peace,
tiredpixel
Command line isn't a Ruby interpreter, it's mangling things before it gets to Rake.
Try this:
rake "import_regional['/Users/xyz.csv', 'northeast']"
I have defined a rake task as follows in a file called file_locker_task.rake
namespace :myspace do
task :process => :environment do
FileLocker.lock_files
end
end
How do I execute this rake task from the command line?
I tried:
rake myspace:process and rake process but both are throwing an error like this:
rake aborted!
Don't know how to build task 'process'
Run rake -T -A from your Rails home directory to see all the tasks that rake knows about. Yours must be in that list for rake to run it.
By default, in a Rails app, rake looks in the lib/tasks directory and its subdirectories for your .rake files. Check that. (I suspect this is the problem.)
According to docs
Any ruby file (including other rakefiles) can be included with a standard Ruby require command.
-
Additional rake files (with the file extension “.rake”) may be placed in rakelib directory located at the top level of a project (i.e. the same directory that contains the main Rakefile). Also, rails projects may include additional rake files in the lib/tasks directory.
rake --tasks takes about 18s to run. This is just the time it takes to load all the tasks, as a result any task I define will take at least this amount of time to run :
$time rake --tasks
rake db:clean # Cleaning up database
rake passenger:restart # Restart Application
rake spec # Run specs
real 0m18.816s
user 0m7.306s
sys 0m5.665s
My Rakefile :
$: << "."
require "rubygems"
require "rspec/core/rake_task"
desc "Run those specs"
task :spec do
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w{--colour --format progress}
t.pattern = 'spec/*_spec.rb'
end
end
task :default => :spec
Any idea why rake takes to much times ?
Thanks
Try spring
Command line will look like:
spring rake -T
It will take more time running the first time, but subsequent runs will be very fast.
This solution worked for me: Faster rake tasks in Rails.
I had to do a little variation where I created a lib/tasks/no_rails directory and put all the Rake files which do not need Rails in there and loaded only those using the above method.
I like the solution Pratik mentions for the general case of loading rails for tasks that need it and not for those that don't, for any rake task without having to remember beforehand.
A less-invasive method to run a rake task that doesn't need rails is to use the -f rake option to tell rake to use a particular Rakefile. This way, rake won't go looking for rake tasks in all of rails.
For example, assuming your task above is in a file called Rakefile at the top level of your project and your Rakefile doesn't do anything that loads Rails like require File.expand_path('../config/application', __FILE__), you can do:
$ rake -f Rakefile spec
and it should run your spec task much faster. Try $ time rake -f Rakefile -T; I did this with a rails-independent Rakefile of mine and got:
real 0m1.543s
user 0m1.308s
sys 0m0.201s
The downside is you have to remember to specify this option every time, and not to specify it if you want to run a rake task from rails like rake db:migrate.
The entire rails environment has to be loaded, therefore even simple rake tasks such as rake --tasks take a while. Opening a console with rails console or script/console takes a similar time. You may try to hack Ruby or Rails to speed up rake, but too much optimization can be bad if you want to switch to a newer version later. Since the rails environment must be loaded, cleaning up routes may also help.
I would like to run db:migrate VERSION=0 and then db:migrate inside of my own rake task. I am confused about how to do this. Do I need a special require statement? My rake task will reside in the lib/tasks directory of a Rails app. Thanks.
Is your task just dependent on having a clean db? If that's the case then you can do:
task :my_task => [:environment, 'db:reset']
EDIT: Rake::Task[] won't accept parameters, you have to set it in ENV. In addition, you have to reenable the task to run it multiple times.
ENV['VERSION']= '0'
Rake::Task['db:migrate'].invoke
Rake::Task['db:migrate'].reenable
ENV.delete 'VERSION'
Rake::Task["db:migrate"].invoke
NOTE: Rake::Task.reenable requires Rake 0.8.2 or higher.
Check out rake db:reset as that will accomplish what you are trying to do.
To see what all of your rake tasks do, run rake -T