how to run a simple file on heroku - ruby

say I've got my rails app on github and am deploying the github repo on heroku.
I've got a situation where I have a simple text file with bunch of words (it is in my github repo). I want to insert these words (using a simple ruby program) into a database. Instead of using the tap command, is it possible in heroku to just run my simple ruby program and insert the words into the database...or maybe just show them on the terminal?
maybe confusing but basically I want to know how to run simple ruby script from heroku command line?

With cedar, you can run bash:
heroku run bash

Put your ruby script in a bin directory and git push it to Heroku. Now you can execute a shell command in the heroku console.
For example, if your Ruby script is bin/foo.rb, you can run the following command in the Heroku console:
`ruby bin/foo.rb`
Note the use of backticks.

Since you're talking about a Rails app on Heroku, how about using rails runner:
heroku run bundle exec rails runner ./path/to/script.rb -a <your-app>
Have a look at the RailsGuides for rails runner for more details.
Alternatively, turn that script into a rake task if runner is not your cup of tea (eg, for recurring tasks).

cd /path/to/my/local/repository
heroku console
require 'my_word_importing_script'
Failing that, try a simple Sinatra application as importer.rb?
require 'sinatra'
require 'sequel'
configure do
// connect to the database with sequel
end
get '/import/a-long-unguessable-url-fdsjklgfuiwfnjfkdsklfds' do
words = YAML.load(File.join(File.dirname(__FILE__), "my_list_of_words.yaml"))
words.each do |word|
// Your logic for inserting into the database with sequel
end
end
Hitting http://example.com/import/a-long-unguessable-url-fdsjklgfuiwfnjfkdsklfds in your browser would kick off the import. Handy for an external cron task.
You would also need a config.ru file in the repo:
require 'importer'
run Sinatra::Application

If you want to run arbitrary local Ruby files on Heroku, check out the blog post at
http://www.22ideastreet.com/debug/run-local-scripts-on-heroku
There are some things to watch out for (long run times, etc.) but it might be useful if you have a file that you haven't checked in that you want to test or run on a Heroku instance.

Related

how to use RVM to a ruby app engine?

i wanna make a simple ruby App Engine in rails just like heroku, i'm dealing with a problem now.
My idea was:
1.use rails to establish the App Engine, use a class 'App' to handle all apps.
2.when a user create an ruby app he should offer it's git path
3.when the user deploys it, my app engine will do these things:
clone the git to a path in my server (done
use RVM to designatine the ruby version witch user wanted and make a gemdir for the project (some problems here
create a nginx conf for the project, then include it and reload nginx (i can do it
Problems in the second step:
codes here:
def start_thin
Dir.chdir(proj_path) do
system('rvm use ruby-1.8.7-p352#testname --create')
system('gem env gemdir')
success = system ('thin start -s3 --socket ' + self.proj_sock)
if success
return true
end
end
return false
end
when the code runs here, the log told me "RVM is not a function...blahblah", i know something about the login-shell and non-login-shell, then i try to fix it via editing .bashrc but same problem occurred.
And if i ignore it, the app can't be deployed, because of a Load Error :
myapp.rb:2:in `require': cannot load such file -- sinatra (LoadError)
if i open a terminal in that app directory, i can use thin to start it.
i wanna know how to run cmd just like in a terminal, without all these odd problem?
or how to edit my method to fix it?
Thanks!
Thanks Casper and GhostRider.
The user and rvm settings are correct.
After lots of googles and tests i found it's impossible...
Finally I fixed it by using RVM's ruby api instead of running system command.
Such as :
require 'rvm'
env = RVM.current
env.gemset.create('app1')

How can I make a ruby gem's binary file act as rackup?

I have a gem. It can optionally be run as a command line app myapp_cli, or as an app with a local web ui myapp_web.
When working on my app, I can run bundle exec myapp_cli for the command line UI, or
I can run bundle exec rackup for the web UI.
Now I'd like bin/myapp_web to essentially be a synonym for rackup. So when someone installs my gem, they can just run myapp_cli or myapp_web.
Is there a nice way of doing that?
I can make my myapp_web look like
Rack::Handler::WEBrick.run(
App.new,
:Port => 9000
)
but that doesn't give me all the goodies that rackup gives me.
All I had to do was browse the source.
require 'rack'
Rack::Server.start

Rails -- is IRB necessary?

I am following Michael Hartl's RoR toturial and there are multiple places where he uses IRB, often to add users to the database. When I use rails console to open IRB and then create a User in the database everything works fine, but if I try to do the same thing by running the same line of code from a file like test.rb in the directory of my application it doesn't work b/c it says it can't find the User model. Is there any way I can run these lines of code (i.e. for putting a user into a database) from a .rb file rather than from the IRB?
For a separate script look into rails runner. It loads the Rails backend so you have access to all the models and exists for this purpose.
From the "Ruby on Rails Guides":
runner runs Ruby code in the context of Rails non-interactively. For instance:
$ rails runner "Model.long_running_method"
If you're just using test.rb as a convenience to save and re-run console commands, you could do this:
rails console < test.rb
Or, as a bit of a hack, put this at the top of your test.rb:
require 'config/environment'
And invoke it from the app's root directory like this:
ruby -I . test.rb
Placing a ruby file in the folder of your app doesn't automatically load up your Rails app. You need to explicitly load the config/environment.rb file to load the Rails app.
If your test.rb is in the root of your app, you can do something like
require File.expand_path("../config/environment", __FILE__)
# Access your models here

Need help with starting RoR: Command "rails server" does not result in "Booting WEBrick"?

I need help to get started with RoR.
I currently follow this guideline:
http://allaboutruby.wordpress.com/2009/07/20/installing-rails-on-windows-3-years-later/#comment-11099
I followed step 1 through 3 w/o problems.
In step 5: I can get the webserver through WEBrick working.
When i put
"rails server"
instead of getting "Booting Webrick", i get "rails new_path option"
thus when i try 127.0.0.1:3000 in the browser... it does work.
Can anyone guide me on this on how to get it up and runnning? (Im a total newb for now...so i need specific explanations! thanks!)
In your tutorial i can't see the command 'bundle install' - it's checking and installing all necessary gems in your system. So why you don't use another great rails tutorial - http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
I'm guessing you are running windows, on which rails can be a little awkward. You'll probably need to run the rails server command by pointing ruby at the server script. On windows, your rails "commmand" is actually just a .bat file that lives in the /bin file of your ruby installation, and that .bat file just passes the arguments to ruby. If you look at the rails gem that is installed on your machine, you'll see the files that correspond to the normal first argument of a rails command (console, generate, server, etc). You might find it helpful to copy these to the /script directory of your application, and when you want to run a rails command you can just run "ruby script\server" from your application's main directory, though there may be more accepted ways of getting the same result.

Using Rake or Gem for building scripts?

I want to build scripts that automatize things for me.
Here is an example of what I want to do:
Create new rails app (rails new application_name --database=mysql)
Jump to folder
Initialize git (git init; git add .; git commit -m "first commit"; git remote add name address; git push name master)
Create heroku project (heroku create; git push heroku master)
Etc...
I've got a lot of such scripts (not just rails related) I want to build.
Should these kind of steps be coded with Rake or Gem?
From what I have understood rake is usually getting the tasks from the current folder's Rakefile. If i want to make operations universal, is it better to create a gem?
The thing is that I want to be able to call it from whatever directory I'm in.
What are the pros and cons with each?
Going with rake or a gem is fine. If you want to centralize your rake tasks (assuming you are on OSX or Some Linux/*nix variant) you can create them in your home directory:
~/.rake/*.rake
Rake will look there for tasks to run if in a directory w/ no Rakefile.
Also, consider (again, if you are on some sort of *nix platform) just creating shell aliases to your commands.
Edit:
Another consideration specific to your Rails work is to leverage Application Templates. Here is a link to a good screencast.
Some of what you want could be accomplished with shell aliases, some with gems, some with rake.
Using Brian's suggestion, I added the following to ~/.rake/git.rake:
namespace :git do
desc "Init, add, initial commit"
task :init do
`git init .`
`git add .`
`git commit -m 'Initial commit'`
end
end
Then in any directory I can run "rake git:init" and it will do all the intial setup. The remote add is a little harder because the remote name would be a variable (could be provided via a shell variable or a Readline prompt).
For creating a rails app, I'd add an alias to ~/.bash_profile:
alias new_mysql="rails new $ARGV --database=mysql"
Then run "new_mysql myRailsProject".
For the most part I would think just running a bunch of command line scripts would be a shell alias rather than a Rake task.
You should be using rake tasks for this stuff. Bates has a screencast showing how to accomplish what your trying to get done. Basically you create a custom task and after you can call rake my_task and it will execute your script.

Resources