如何在heroku上运行一个简单的文件

发布于 2024-10-09 17:06:40 字数 275 浏览 0 评论 0 原文

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

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

也许令人困惑,但基本上我想知道如何从 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

本王不退位尔等都是臣 2024-10-16 17:06:40

使用 cedar,你可以运行 bash:

heroku run bash

With cedar, you can run bash:

heroku run bash
东北女汉子 2024-10-16 17:06:40

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

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

`ruby bin/foo.rb`

请注意反引号的使用。

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.

高冷爸爸 2024-10-16 17:06:40

既然您谈论的是 Heroku 上的 Rails 应用程序,那么如何使用 rails runner

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

请查看 RailsGuides for rails runner 了解更多详细信息。


或者,将该脚本转换为rake 任务 如果跑步者不是你的菜(例如,对于 重复任务)。

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

隐诗 2024-10-16 17:06:40
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 任务来说很方便。

您还需要存储库中的 config.ru 文件:

require 'importer'
run Sinatra::Application
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
半窗疏影 2024-10-16 17:06:40

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文