【问题标题】:how to run a simple file on heroku如何在heroku上运行一个简单的文件
【发布时间】:2011-05-31 10:25:02
【问题描述】:

假设我在 github 上安装了我的 rails 应用程序,并且正在 heroku 上部署 github 存储库。

我有一个简单的文本文件,里面有一堆单词(它在我的 github 存储库中)。我想将这些单词(使用简单的 ruby​​ 程序)插入数据库。除了使用 tap 命令,是否可以在 heroku 中运行我的简单 ruby​​ 程序并将单词插入数据库......或者只是在终端上显示它们?

可能会令人困惑,但基本上我想知道如何从 heroku 命令行运行简单的 ruby​​ 脚本?

【问题讨论】:

    标签: ruby heroku


    【解决方案1】:

    如果您想在 Heroku 上运行任意本地 Ruby 文件,请查看博客文章

    http://www.22ideastreet.com/debug/run-local-scripts-on-heroku

    有些事情需要注意(运行时间长等),但如果您有一个尚未签入的文件想要在 Heroku 实例上测试或运行,这可能会很有用。

    【讨论】:

    • 链接已失效。请引用相关部分。
    【解决方案2】:

    既然您说的是 Heroku 上的 Rails 应用程序,那么使用 rails runner 怎么样:

    heroku run bundle exec rails runner ./path/to/script.rb -a <your-app>
    

    查看RailsGuides for rails runner了解更多详情。


    或者,turn that script into a rake task 如果跑步者不是你的一杯茶(例如,recurring tasks)。

    【讨论】:

      【解决方案3】:

      使用 cedar,您可以运行 bash:

      heroku run bash
      

      【讨论】:

        【解决方案4】:

        将你的 ruby​​ 脚本放在 bin 目录中,然后 git push 到 Heroku。现在您可以在 heroku 控制台中执行 shell 命令。

        例如,如果您的 Ruby 脚本是 bin/foo.rb,您可以在 Heroku 控制台中运行以下命令:

        `ruby bin/foo.rb`
        

        注意反引号的使用。

        【讨论】:

          【解决方案5】:
          cd /path/to/my/local/repository
          heroku console
          require 'my_word_importing_script'
          

          如果做不到这一点,试试一个简单的 Sinatra 应用程序作为 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
          

          在浏览器中点击http://example.com/import/a-long-unguessable-url-fdsjklgfuiwfnjfkdsklfds 将启动导入。对于外部 cron 任务很方便。

          您还需要在 repo 中添加一个 config.ru 文件:

          require 'importer'
          run Sinatra::Application
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-16
            • 1970-01-01
            相关资源
            最近更新 更多