IntelliJ IDEA / RubyMine - Run multiple rake tasks at once? - ruby-on-rails

In a terminal, I can run multiple rake tasks one after another in a single line:
rake grunt war app_server:start
I can't seem to get this to work in IntelliJ IDEA when trying to create a Configuration to do it. I'm able to fill in the "Task Name" field as above, but when I run the configuration, I get the following error:
Don't know how to build task 'grunt war app_server:start' (see --tasks)
Does anyone know if I'm just doing something wrong, or is this just not possible in IDEA? Thank you.

Compounds don't work since they are run in parallel and not in the same process. This makes it impossible to execute for example environment task followed by another task such as elasticsearch:import:all.
The only way I could get it to work was to create a project-level task, for example lib/tasks/es.rake:
require 'elasticsearch/rails/tasks/import'
namespace :es do
task reindex: %w[environment elasticsearch:import:all]
end
You then execute es:reindex as a top-level task which will execute its dependencies in order.

As an option, in the Idea you can create multiple tasks and combine them in a group:
Run -> Edit Configurations -> Add -> Compound

Related

NoMethodError: undefined method `within' for main:Object

I'm trying to make Capistrano deploy script.
In my Capfile I make sure all rake tasks are included
# Load tasks
Dir.glob('config/capistrano_tasks/*.rake').each { |r| import r }
Next I have a 'migrations.rake' containing:
namespace :fileservice do
task :migrate do
within release_path do
info 'Doing migrations'
execute :php, fetch(:symfony_console_path), 'doctrine:migrations:migrate', '--no-interaction', fetch(:symfony_console_flags)
end
end
end
In my deploy.rb I call the task at the very end with:
after 'deploy:publishing', 'fileservice:migrate'
For some reason I keep getting an error saying:
NoMethodError: undefined method `within' for main:Object
I have no idea where to look or what might be wrong... When googling I get a lot of "NoMethodError" hits but none about the 'within' method and most are general Ruby errors.
Where should "within" be defined? I dat a ruby on rails thing? Or capistrano?
Hopefully someone knows where to start looking or which library / script to include!
UPDATE: I just discovered that none of the methods work. When removing lines I got the same error for "info" and "execute".... So I guess somewhere, something is missing.....
You need to tell Capistrano where (i.e. on what servers) to run your SSH commands. Do this using an on block, as follows:
namespace :fileservice do
task :migrate do
on roles(:db) do
within release_path do
info 'Doing migrations'
execute :php, fetch(:symfony_console_path), 'doctrine:migrations:migrate', '--no-interaction', fetch(:symfony_console_flags)
end
end
end
end
Replace roles(:db) as appropriate for your task, depending on where you want the commands to be run. The expression on roles(:all) do ... end, for example, will run the commands on all servers.
You can also follow the official documentation at http://capistranorb.com, or the Capistrano README, both of which show examples of the task/on/execute syntax.
As you already got the answer but Let me put the bulb from basic.
SSHKit was actually developed and released with Capistrano 3, and it’s basically a lower-level tool that provides methods for connecting and interacting with remote servers; it does all the heavy lifting for Capistrano, in other words.
There are four main methods you need to know about.
on(): specifies the server to run on
within(): specifies the directory path to run in
as(): specifies the user to run as
with(): specifies the environment variables to run with
Typically, you’ll start a task by using an on() method to specify the server on which you want your commands to run. Then you can use any combination of as(), within(), and with() methods, which are repeatable and stackable in any order.
Hope this help you

How to write and execute an individual rake task outside the Rails application folder

How do you write a rake task outside the Rails application directory and make it run.
For example, say write a sample rake task
task :dummy do
puts User.first.first_name
end
this task is under ~/fun/sample.rake
And my rails application is located at ~/my_app/
Now I need to run the sample.rake. I know i need to load the environment, DB etc., etc., how do i do that? Stuck at this for the past hour.
I tried the one below, obviously it did not work because it did not know how to build it.
rake -f ~/my_app/Rakefile dummy
Note: I should not touch the files inside the Rails application but I can write whatever I want inside the fun directory
You need to create a Rakefile and load the rake gem. See this answer: Ruby: Accessing rake task from a gem without Rails

Use linux script to make a continuous rake task running (start, stop etc)

I have a rake task which parses a streaming API and enters data into database. The streaming API is live feed and the rake task should run continuously for the live data to enter the database. The rake task once called will run continuously and parse the data. Now i have started the rake task and it is running. The problem is that if i close the terminal or reboot the server, the rake task wil be stopped. So, i want a script in linux (something like the one used to start, or stop apache server), which does the following:
1. start the rake task by calling rake command (rake parse:stream) from the RAILS-ROOT (application directory of Rails app)
2. stop the rake task by killing the process.
3. start the rake task automatically when the server reboots.
i am not familiar to linux scripts and i dont know where to start. i am using ubuntu server. can anyone help me?
Here's an article that might help you also. It discussed various options for managing Ruby applications and their related processes:
http://michaelvanrooijen.com/articles/2011/06/08-managing-and-monitoring-your-ruby-application-with-foreman-and-upstart/
You need to run your script as a daemon. When I create this kind of startup scripts I usually make 2 files, one that stays in /etc/init.d and handles the start/stop/status/restart commands and another one that actually does the job and gets called by the first script.
Here is one solution, and although the daemon script is written in perl, you want to run some command lines only, so daemonizing a perl script could do your job easily.
If you want, there are also ruby gems for daemonizing scripts, so you can write a script in ruby that does the rake tasks.
And if you want to go hardcore, there are solutions for writing bash scripts that can daemonize, but I'm not sure I would recommend a solution like that; at least I find them pretty difficult to use.
Take a look at how Github's Resque project does it.
Essentially they create tasks for starting/restarting/stopping a particular task, in this case resque:work. Note that the restart_workers task simply invokes the other tasks, stop and start. It should be really easy to change this for what you want.

How can I run a Rake task as a Windows "scheduled task"?

I have a rake task I need to run as a daily job on a Windows XP machine. I discovered the Windows "scheduled tasks" control panel. I'm trying to figure out how to get it to run my Rake task.
Two of the fields are "run" (with a browse button) and "start in". I tried to enter rake mycategory:mytask into "run" and my Rails project dir containing the Rake task into "start in". The result was a message saying the task "could not start".
How can I get set up a Windows "scheduled task" to run a Rake task?
If you can build a batch file that can execute it properly I would do so, and then you can direct the batch file to run with the task.
Something like this should work:
:: Run task every hour
#call rake stats RAILS_ENV="production"
Also, aside from the (correct) batch file advice above, AFAIK, you may need to run the task on an account that has a non-empty password set. Property of Scheduler.
Just adding an updated/fleshed out answer for those interested...
Create a file called rake.bat - make sure to save with ANSI encoding (the default with Windows Notepad). You can save this file anywhere, but I put it in C:\ror\rake.bat
rake.bat
#call bundle exec rake %*
Now when you create a scheduled task, you can set it to run the .bat file and the arguments are simply what comes after rake. Set it to run inside the directory of your choice. Run whether or not the user is logged in, but do NOT run with the highest privileges. Screenshots below for clarity; my rake task is namespaced manager, and the task itself is sync:

What steps are followed after you say "rake install"?

I'd like to install a plugin but I'm afraid it's going to install a lot of unnecessary stuff. I'd like to look at whatever file rake takes it installation instructions from and remove anything unnecessary.
I believe this is the Rakefile. But I'm not sure what happens when rake looks at the rakefile - does it execute the entire rakefile or only parts of the rakefile that are designated as being relevant to this "install" procedure?
a rake file is a collection of tasks, when you call rake with an argument (in this case install) that's the task that get's executed. (It's similar to ant if you come from Java)
So, no, rake does not execute the whole rakefile when you call "rake + task" but only the task chosen. Note that tasks can have dependencies (eg the "test" task may depend on other previous tasks, like creating some folders and stuff for the tests to run).
Lastly, the rake user guide as pointed by other users is useful, but I recommend a more enjoyable read here -> Ruby on Rails Rake tutorial.
Rake is set up much like Make in that a Rakefile consists of targets and dependencies. This is different from a regular ruby script in that Rake starts at the target you ask for and recursively executes its dependencies before executing the target itself.
So, install might look like this:
task :install => :stage do
# stuff to do
end
Here, your target is the install task, and it depends on some other task called stage.
To execute install, Rake must first execute the dependencies of stage (if it has any), then stage, then finally it executes install. So no, you don't execute the whole file, just enough of it to safely execute the target you asked for.
Rake also supports file targets:
file 'foo.html' => 'bar.xml' do |t|
# Build foo.html from bar.xml, however that is done
end
If you know Make, this looks familiar. Rake first checks whether bar.xml depends on anything, and if so, it executes that. Then, if bar.xml is newer than foo.html, then Rake executes this file task. If foo.html is newer, then Rake assumes that it has aleady been built and skips it.
For more, the Rake User Guide is a good place to start if you want to learn what Rake does.
Why would a plugin install something "unnecessary"?
Assuming your fears are justified, though, could you not install the plugin, do your investigation and then, if not satisfied, revert to the pre-installed version using your source control system? If you're not using source control, this might be the perfect excuse to start...

Resources