Rake tasks

I am new to ruby and maintaining an existing web application. I need to change the FTP server in a task and trying to familiarize myself with rake tasks. The task is in lib/tasks/archives.rake under the namespace utils. I read over a rake tutorial and viewed rake options to try and show information about the task and possibly run it to test. But when I do 'rake -T', I get a lot of DEPRECATED messages and the list afterward does not have my task listed. This is how the task is defined:

namespace:utils do    <snip>    task :process_archives => :environment do    <snip>

But I have no task listed with that name? I tried 'rake -D utils:process_archives' as well and only got the deprecated messages output. I do see the task in the schedule.rb file...

./config/schedule.rb: command %{#{cmd_root} && cd /var/www/vhosts/xxxx/current && bundle exec rake utils:process_archives DIR="/var/www/vhosts/xxxx/shared/tmp" RAILS_ENV=production}

And the following file:

./script/process_archives.sh:rake utils:process_archives DIR="/var/www/virtual/xxxx/htdocs/xxxx/tmp" RAILS_ENV="production"

The Rakefile looks like this:

require File.expand_path('../config/application', __FILE__)

XXXX::Application.load_tasks

Thanks for any help!

Hello,

What are the deprecated messages you are getting?

Mike Riley

Robert,

  1. The same deprecation warnings probably happen when you just run “rails c” or “rails s”, right? If so, those are just general deprecation warnings that are happening when you boot up the app. Today, you can ignore them (hence, warning). In the long run, you’ll want to fix those as you upgrade gems and refactor code.

  2. Custom rake tasks (defined in your app) should have a description above them. You actually put the description on its own line above the line that starts with the word “task” (slightly confusing). See the section “Describing Your Tasks” here http://jasonseifer.com/2010/04/06/rake-tutorial

You’ll notice that desc comes on its own line before the task line. If you fail to put that description in, your Rake task will still run, but it won’t show up in the rake -T output.

Finally, you probably almost certainly don’t want to be running RAILS_ENV=“production” if you are developing locally on your own machine. And learn to use a debugger-- I recommend byebug if you’re using Ruby 2.0 or above, and put “byebug” at the top of your rake task. Then just run your rask task and verify that you fall into the debugger. (I’m telling you this as a sanity check methodology because you are trying to verify that you know how to run the task)

Hope that helps.

-Jason

Jason Fleetwood-Boldt wrote:

Finally, you probably almost certainly don't want to be running RAILS_ENV="production" if you are developing locally on your own machine. And learn to use a debugger-- I recommend byebug if you're using Ruby 2.0 or above, and put "byebug" at the top of your rake task. Then just run your rask task and verify that you fall into the debugger. (I'm telling you this as a sanity check methodology because you are trying to verify that you know how to run the task)

Thanks for the info and help. The app is on ruby 1.9.3, I was running rake on the production machine. I downloaded the app and have it running locally, but there are some display issues with development, so I was running production.

I see the task is running via cron every hour, it talks to an ftp server and gets a file list, then creates playlist files from the list of MP3 files it finds. I see the task has a lot of puts when 'ARGV = V', should I be able to run the task with the following command?

# rake utils:process_archive V'

Without seeing the task I can't tell you the answer to that. And the fact that it runs on a cron schedule means that you probably have something like a worker in the background doing that.

Normally you run

rake some_task X="abc" Y="xyz"

where X and Y are arguments being passed in.

But note that you probably want to prefix the rake task in development with "bundle exec" to get it to run with the right gemset.

-Jason

Jason Fleetwood-Boldt wrote:

Without seeing the task I can't tell you the answer to that. And the fact that it runs on a cron schedule means that you probably have something like a worker in the background doing that.

Normally you run

rake some_task X="abc" Y="xyz"

I see this piece of code at the start of the task assuming an argument could be used to get the verbose response....

     @verbose = false      ARGV.each do |arg|        puts "arg: #{arg}"        @verbose = false # if arg == 'V'      end

Then the code uses '#puts something if @verbose' throughout. Like I said, I'm new to ruby, but have used Perl and PHP and other languages before, I hope is what is throwing me off is the # are not comments? This almost looks like @verbose would always be false. What is this piece of code looking for as an argument?

I apologize for trying to get a crash course in ruby here, gonna have to get up to speed soon as this app that was left to me to try and maintain/support. I am reading through the following tutorial to get up to speed as quickly as I can, any other pointers extremely helpful...

But note that you probably want to prefix the rake task in development with "bundle exec" to get it to run with the right gemset.

I downloaded a copy of the app and setup locally on a development server. The production app is running on nginx web server and have the copy running on Apache locally. I saw the following directive available in the nginx conf to enable development, copied to Apache and this is how I am running dev mode...

RailsEnv development

Then I can run "bundle exec rake utils:process_archives"?

As I posted earlier, there is a script/process_archives.sh file that I thought at first runs the rake task since I see results of the task in the cron log...

rake utils:process_archives DIR="/var/www/virtual/xxxx/htdocs/xxxx/tmp" RAILS_ENV="production"

But that DIR does not exist on the server, so I'm questioning if this shell script is being used at all. I found further the task in config/schedule.rb...

command %{#{cmd_root} && cd /var/www/vhosts/wmnf/current && bundle exec rake utils:process_archives DIR="/var/www/vhosts/xxxx/shared/tmp" RAILS_ENV=production}

Those dirs do exist and I also found in the crontabs...

root@app01:~/www# cat /var/spool/cron/crontabs/appuser |grep process 31 * * * * /bin/bash -l -c 'PATH=/opt/ree/bin:$PATH && cd /var/www/vhosts/xxxx/current && bundle exec rake utils:process_archives DIR="/var/www/vhosts/xxxx/shared/tmp" RAILS_ENV=production >> /var/www/vhosts/xxxx/shared/log/cron.log 2>&1'

Is the scheduler.rb being used at all?

Another thing about running the app locally on a development server is when I enable development as shown above, it breaks links to images, css, etc. Seems there is something in the code to prefix the paths of those resources with '/app_development/' when using development environment. So, I'm running in production environment on the development server. Other than references like this in the code for development, what would be the difference between running a copy of the app under the production or development environment on the development server?

Again, thanks a ton for any help and patience, I promise to get up to speed as quickly as possible.

You're reading things correctly.

# starts a comment @verbose is always false in this code, and would be even if the if were not commented out

The code doesn't even make sense as the result of someone trying to comment out something to hard-wire @verbose to one value or the other...

Scott Ribe wrote:

Maybe I missed something earlier in the discussion, but can't you just edit it to:

@verbose = true if arg == 'V'

Scott Ribe wrote:

OK, that's a relief that I can read the code, thanks for clarifying. To get verbosity, I'll need to do a lot of uncommenting and do line-by-line debuggging :-/

Jason mentioned a debugging tool for Rails 2.0 earlier, what about for this 1.9.3, anything available to help?

Maybe I missed something earlier in the discussion, but can't you just edit it to:

@verbose = true if arg == 'V'

Yes, I guess so, and then uncomment all the places in the code where he has....

#puts <something> if @verbose

I'll give it a try, still trying to figure out how to run the task. I put the app in development environment with the Apache directive and this is all the deprecated info I mentioned and other output when trying to "bundle exec", I get "Don't know how to build task 'utils:archives'". Is this how I should be trying to run the task?

$ bundle exec rake utils:archives --trace Your Gemfile lists the gem sitemap_generator (>= 0) more than once. You should probably keep only one of them. While it's not a problem now, it could cause errors if you change the version of just one of them later. Your Gemfile lists the gem backup (>= 0) more than once. You should probably keep only one of them. While it's not a problem now, it could cause errors if you change the version of just one of them later. Your Gemfile lists the gem awesome_print (>= 0) more than once. You should probably keep only one of them. While it's not a problem now, it could cause errors if you change the version of just one of them later. Your Gemfile lists the gem awesome_print (>= 0) more than once. You should probably keep only one of them. While it's not a problem now, it could cause errors if you change the version of just one of them later. DEPRECATION WARNING: Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead. (called from require at /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.7.4/lib/bundler/runtime.rb:76) DEPRECATION WARNING: Calling set_primary_key is deprecated. Please use `self.primary_key = 'the_name'` instead. (called from require at /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.7.4/lib/bundler/runtime.rb:76) DEPRECATION WARNING: Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead. (called from require at /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.7.4/lib/bundler/runtime.rb:76) DEPRECATION WARNING: Calling set_primary_key is deprecated. Please use `self.primary_key = 'the_name'` instead. (called from require at /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.7.4/lib/bundler/runtime.rb:76) DEPRECATION WARNING: Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead. (called from require at /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.7.4/lib/bundler/runtime.rb:76) DEPRECATION WARNING: Calling set_primary_key is deprecated. Please use `self.primary_key = 'the_name'` instead. (called from require at /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.7.4/lib/bundler/runtime.rb:76) DEPRECATION WARNING: Support for Rails < 4.0.4 will be dropped from Formtastic 4.0. (called from require at /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.7.4/lib/bundler/runtime.rb:76) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: Ruby on Rails — Rails 3.2.0.rc2 has been released!. (called from <top (required)> at /backup/storage/sites/wmnf/wmnf.org/Rakefile:7) rake aborted! Don't know how to build task 'utils:archives' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/task_manager.rb:62:in `' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/application.rb:149:in `invoke_task' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/application.rb:106:in `block (2 levels) in top_level' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/application.rb:106:in `each' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/application.rb:106:in `block in top_level' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/application.rb:115:in `run_with_threads' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/application.rb:100:in `top_level' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/application.rb:78:in `block in run' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/application.rb:176:in `standard_exception_handling' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/lib/rake/application.rb:75:in `run' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/gems/rake-10.3.2/bin/rake:33:in `<top (required)>' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/bin/rake:19:in `load' /home/robert/.rvm/gems/ruby-1.9.3-p392@global/bin/rake:19:in `<main>' /home/robert/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `eval' /home/robert/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `<main>'

Robert Fitzpatrick wrote:

Maybe I missed something earlier in the discussion, but can't you just edit it to:

@verbose = true if arg == 'V'

Yes, I guess so, and then uncomment all the places in the code where he has....

#puts <something> if @verbose

I'll give it a try, still trying to figure out how to run the task. I put the app in development environment with the Apache directive and this is all the deprecated info I mentioned and other output when trying to "bundle exec", I get "Don't know how to build task 'utils:archives'". Is this how I should be trying to run the task?

Never mind my last post, I looked up the command used in cron and made an adjustment to the DIR for my local box and got the expected response.

   bundle exec rake utils:process_archives V DIR="/tmp"

I updated the verbose line to recognize the 'V' argument as suggested and it does output the conditional puts lines, but I do get the following at the end....

   rake aborted!    Don't know how to build task 'V'

The task does appear to complete successfully, with comments if I specify 'V' and without if not, as expected. Is this normal or should I be applying the argument differently?

Robert,

I can't tell you all the ins and outs of your script but what you're seeing here is that rake is looking at the V thinking it is another rake task you want to run (that's why the first one succeeds and then gives you the error)

You probably want something like V="true" or V=1 or something like that.

-Jason