简体   繁体   English

Heroku的Rails:如何下载由rake任务创建的CSV

[英]Rails with Heroku: How to download a CSV created by a rake task

I deployed "Harrys Prelauncher" on Heroku and try to do the teardown (currently just testing). 我在Heroku上部署了“Harrys Prelauncher”并试图进行拆解(目前仅进行测试)。 See here: https://github.com/harrystech/prelaunchr#teardown 请参见: https//github.com/harrystech/prelaunchr#teardown

After running the rake task ... 运行rake任务后......

heroku run rake prelaunchr:create_winner_csvs

... a csv file is created in "/lib/assets", but I dont know how to access the file (it works locally in development). ...在“/ lib / assets”中创建了一个csv文件,但我不知道如何访问该文件(它在开发中本地工作)。

How can I download or access the file? 我该如何下载或访问该文件?

Heroku uses "ephemeral" filesystem that is not guaranteed to preserve changes made at runtime . Heroku使用“短暂的”文件系统,不保证在运行时保留更改 Simply put, if it's not pushed to git (I assume you're using git with heroku), it's not guaranteed to exist in all the instances of your app. 简单地说,如果它没有被推送到git(我假设你正在使用带有heroku的git),那么它不能保证存在于你的应用程序的所有实例中。 It may exist in one of them, but you may have no simple way of accessing that specific filesystem. 它可能存在于其中一个中,但您可能没有简单的方法来访问该特定的文件系统。 And you shouldn't, really. 你不应该,真的。

It's done like that so that multiple instances of the same app can be fired up seamlessly. 它就是这样完成的,这样可以无缝地启动同一个应用程序的多个实例。 Of course, that requires some discipline: storage of any meaningful state outside: in the database, on external disk, anywhere. 当然,这需要一些规则:在外部存储任何有意义的状态:在数据库中,在外部磁盘上,任何地方。 The benefit of this is horizontal scalability: should you be short on resources, you can fire up another web dyno that would (normally) behave exactly the same way. 这样做的好处是横向可伸缩性:如果您缺少资源,您可以启动另一个(通常)行为完全相同的Web dyno。 New dynos are started from bundles that are packed on git push and thus do not contain any changes you may have made in another instance. 新的dynos是从git push打包的bundle开始的,因此不包含你在另一个实例中所做的任何更改。

A workaround may be running heroku run bash , so that you end up in an interactive shell linked to another instance of your bundle. 解决方法可能是运行heroku run bash ,以便最终进入链接到另一个bundle实例的交互式shell。

Then you can make that file (by running your rake task) and access its contents in any way you deem reasonable. 然后,您可以制作该文件(通过运行您的rake任务)并以您认为合理的任何方式访问其内容。 Text files can be echoed into the console with cat and copy-pasted anywhere else. 可以使用cat将文本文件回显到控制台中,并在其他任何位置复制粘贴。 That's a dirty way. 那是一种肮脏的方式。

A much cleaner way would be rigging the app to send the file in question via email. 一个更清洁的方法是操纵应用程序通过电子邮件发送有问题的文件。 and it's one of the few reasonable ways if that rake task is invoked by the Rails app itself. 如果Rails应用程序本身调用了rake任务,那么这是少数合理的方法之一。

I ran into this problem recently while developing the Prelaunchr campaign for a client. 我最近在为客户开发Prelaunchr广告系列时遇到了这个问题。 Assuming you have a local version of your app, you can "pull" your Heroku database down to your local machine, set that as your development database in database.yml, and run the rake task from your local app, which should now have the same database as your heroku version. 假设您有一个本地版本的应用程序,您可以将Heroku数据库“拉”到本地计算机,将其设置为database.yml中的开发数据库,​​并从本地应用程序运行rake任务,该任务现在应该具有与heroku版本相同的数据库。 Here is the command to pull the db (subbing out name_for_database & heroku_app_name with your own): 这是拉取db的命令(使用您自己的名称替换name_for_database和heroku_app_name):

heroku pg:pull HEROKU_POSTGRESQL_COPPER_URL name_for_database --app heroku_app_name

Make sure to restart your local server to see the new database info populated. 确保重新启动本地服务器以查看填充的新数据库信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM