Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku rake tasks failing for Sinatra app with string arguments

I've just deployed a Sinatra app to heroku and the app contains two rake tasks:

task :create_db , [:db_id , :db_name]
task :destroy_db , [:db_id, :token] 

When I run

heroku run rake -T

in the console, Heroku prints the following response:

(in /app)
rake create_db[db_id,db_name]  # Creation count database task
rake destroy_db[db_id,token]   # Destroy database task

However when I run:

heroku run rake create_db['test', 'test database']

It responds with the following error:

(in /app)
rake aborted!
Don't know how to build task 'create_db[test,'
/usr/local/lib/ruby/1.9.1/rake.rb:1720:in `[]'
/usr/local/lib/ruby/1.9.1/rake.rb:2040:in `invoke_task'
/usr/local/lib/ruby/1.9.1/rake.rb:2019:in `block (2 levels) in top_level'
/usr/local/lib/ruby/1.9.1/rake.rb:2019:in `each'
/usr/local/lib/ruby/1.9.1/rake.rb:2019:in `block in top_level'
/usr/local/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/local/lib/ruby/1.9.1/rake.rb:2013:in `top_level'
/usr/local/lib/ruby/1.9.1/rake.rb:1992:in `run'
/usr/local/bin/rake:31:in `<main>'

I'm not quite sure why this is failing, however my guess is that it has to do with the string arguments. As you can see above, it says "don't know how to build task 'create_db[test,' with test showing up without strings.

Have any idea how to get this task to run from the command line?

like image 481
Nick ONeill Avatar asked Jan 03 '13 00:01

Nick ONeill


3 Answers

From the Rake docs:

The rake task name and its arguments need to be a single command line argument to rake. This generally means no spaces. If spaces are needed, then the entire rake + argument string should be quoted. Something like this:

rake "name[billy bob, smith]"

(Quoting rules vary between operating systems and shells, so make sure you consult the proper docs for your OS/shell).

Also note that you don’t need to quote the individual parameters.

In your case this should work:

rake "create_db[test, test database]"

This also works:

rake create_db[test,'test database']

Note that the second parameter is quoted, but there is no space between the comma and the quote, so the shell treats the whole thing as a single argument.

On Heroku

This doesn’t work using heroku run, because in this case the command gets interpreted by a shell twice, once by your own, and again on the Heroku Dyno, so you need to make sure the command as run by Heroku has the correct quotes.

You need to either escape the quotes:

heroku run rake \"create_db[test, test database]\"

so that the command run on the Dyno is rake "create_db[test, test database]"; or quote the quotes:

heroku run rake "create_db[test,'test database']"

so that the command that gets run is rake create_db[test,'test database'].

like image 170
matt Avatar answered Oct 18 '22 17:10

matt


The reason

$ heroku run rake create_db['test', 'test database']

is not working is due to the space between/within the parameters.The following will work.

$ heroku run rake "create_db['test','test database']"

like image 2
rickyc Avatar answered Oct 18 '22 16:10

rickyc


Well for me this works

heroku run rake "namespace:task['var1','var2']"
like image 1
JBG Avatar answered Oct 18 '22 17:10

JBG