how to run a simple file on heroku

后端 未结 5 1924
庸人自扰
庸人自扰 2021-02-05 13:46

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 g

相关标签:
5条回答
  • 2021-02-05 13:49

    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.

    0 讨论(0)
  • 2021-02-05 13:53
    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
    
    0 讨论(0)
  • 2021-02-05 13:59

    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).

    0 讨论(0)
  • 2021-02-05 14:01

    With cedar, you can run bash:

    heroku run bash
    
    0 讨论(0)
  • 2021-02-05 14:07

    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.

    0 讨论(0)
提交回复
热议问题