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.
Related
In Spree 3.1 after bundle install. It need to run rake as follow the document:
bundle exec rake db:migrate
bundle exec rake db:seed
bundle exec rake spree_sample:load
So I have a project to create a rake task for Heroku post deployment. But problem is the db:seed asked default [username] and [password] which required [Enter] [Enter].
Really don't know how to make it go through
And when I put in one task would look like this
task :autoall do
Rake::Task["db:migrate"].invoke
Rake::Task["db:seed"].invoke # This step stucked waiting for Enter
Rake::Task["spree_sample:load"].invoke
end
The one below better but still waiting for [Enter] [Enter] in the process and die.
task :autoall do
Rake::Task["db:reset"].invoke
Rake::Task["spree_sample:load"].invoke
end
If no other way, I might need to dig in the source and modify.
Help Please!
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
I have Rails 4.2 application and I want to execute some one-off code when server starts.
My first approach was to use initializer in config/initializers to run the code. But in this case code is being executed also for all the rake tasks and Sidekiq process.
So I've created a rake task to run my code. Now I wonder how to execute it along with rails server startup. Of course, I can create a shell script that will execute my rake task and then start the server. But is there any rails-way to achieve this?
Foreman is another approach advised by SO, but it's not working for me as my task is not a daemon and the process terminates immediatelly after completion. Apparently all the processes in Procfile have to be daemonized.
Can be achieved with Foreman by running in the same process as daemonized task (e.g. with rails server).
If one-off taks is rake lego:update_all
Then corresponding Procfile is
web: rake lego:update_all && rails s
sidekiq: bundle exec sidekiq
How can I pass arguments to a rake task so that it executes the rake task on a different schema? For example I have rake code such as the one below:
namespace :update_persons_table do
task :import => :environment do
config = Rails.configuration.database_configuration
ActiveRecord::Base.connection.schema_search_path = "my, public, data_master_reports"
# do stuff make updates to table....
end
end
I call this rake task from the command line like this:
RAILS_ENV='production' rake update_persons_table:import
BTW, does the above RAILS_ENV call I am using have to do with the :environment do statement I am using in the second line? Because in my database.yml file i do have a production: database entry. Im trying to figure out how the whole plumbing for this works. This rake task updates a table in a database. But I want to be able to call it on another clone table in a different database. How can I do that with passing parameters in the command line?
How can I pass arguments to a rake task so that it executes the rake task on a different schema?
I think what you are asking is
How can I pass arguments to a rake task so that it executes the rake task on a different environment?
The schema is the same for all environments of your app
does the above RAILS_ENV call I am using have to do with the :environment do statement I am using in the second line?
Yes :environment do will take whatever environment(:development, :test, :production) you are specifying. In your example, its RAILS_ENV='production'
Now to run the rake task in a different environment, like say the development environment
`RAILS_ENV='development' rake update_persons_table:import'
Now the same code that was executed on the production environment db when you ran `RAILS_ENV='production' rake update_persons_table:import' has been run in the development environment db
Hope this is clear enough to get you started
Eventually I would like to get to setting it up as a Rake task and do a cron job, but for right now...all I want to do is take my ruby script that used to work as a standalone script and have it work within my Rails app.
I renamed the file to be .rake instead of .rb and tried doing rake my_script at the command-line, but that gave me this error message:
rake aborted!
Don't know how to build task 'my_script'
(See full trace by running task with --trace)
How do I run this script within my Rails environment?
This is the first time I am doing something like this, so any assistance would be greatly appreciated.
Thanks.
I think what you're looking for is rails runner. I know in Rails 2.3.x you'd do
ruby script/runner <your file>
In Rails 3 it might be slightly different.
http://guides.rubyonrails.org/command_line.html#rails-runner
The primary difference between a runner and a rake task is : runner would boot rails while rake task doesn't (you can tell it to do so).
Since rake can do both (boot/no boot), there's no concept of runner in rails-3 anymore.
So, create a rake task: whatever_name.rake
Example:
desc "This task does awesome stuff"
task :do_awesome_stuff do
awesome_method
end
def awesome_method
#put your ruby code here
end
Now from your command prompt, type rake do_awesome_stuff to execute this rake task.
To make it boot Rails, change task definition to this:
task :do_awesome_stuff => :environment do