RoR - 在哪里放置自动化流程

发布于 2024-10-18 01:04:13 字数 237 浏览 2 评论 0原文

以下情况(ror 3,ruby 1.9)。

我有一个包含一些字段的模型。

现在我想获取一个 json feed(每小时一次),将内容与我存储的模型对象进行比较,如果此 json feed 中有新项目,则放入一个新的模型对象。

我不确定将这个自动操作放在哪里。 我认为在模型方法中这样做不是正确的地方,对吗?

您会在哪里执行此迁移操作?在控制器中?我真的不这么认为。

感谢所有的提示

following situation (ror 3, ruby 1.9).

i've got a model with some fields.

now i want to grab a json feed (f.e. every hour) compare the content with my stored model objects and put in a new model object if there is a new item in this json feed.

i'm not shure where to put this automated action.
i don't think doing this in a models method is the right place, am i right?

where would you do this migration actions? in a controller? not really i think.

thankful for all hints

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

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

发布评论

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

评论(6

凉城凉梦凉人心 2024-10-25 01:04:13

为此完全忽略控制器。模型充当访问数据的抽象层。在您的例子中,您有一些以数据库形式保存在本地的数据,并且您有通过 HTTP 以 JSON 形式检索的远程数据。因此,您拥有常规的 ActiveRecord 模型,并且拥有 JSON 数据的模型。

假设您有一个名为 RemoteData 的模型,它可以获取 JSON 文档,并且有一些方法可以让您干净地从中获取数据。您还有一个 StoredData 模型,用于将检索到的内容保留在数据库中以便稍后显示。

现在,您希望自动执行调用 RemoteData.fetch('url') 的过程,然后对返回的数据调用 StoredData.create :params 的过程。为此,您需要创建一个“rake 任务”。下面是一个示例:

# lib/tasks/fetch.rake
desc "Fetch remote data and persist it"
task :fetch => :environment do
  RemoteData.fetch('url').each do |json_data|
    sd = StoredData.create :url => 'url', :data => json_data
    puts "Retrieved 'url' and saved data in record ##{sd.id} at #{DateTime.now}."
  end
end

然后您可以将系统 crontab 设置为按照您希望的频率运行。 Rake 任务处理业务逻辑,您的模型处理与数据的交互。漂亮又干净。

Ignore controllers completely for this. Models serve as an abstraction layer for accessing data. In your case, you have some locally persisted data in the form of a database, and you have remote data you're retrieving as JSON over HTTP. So you have your regular ActiveRecord models, and you have a model for your JSON data.

Let's assume you have a model called RemoteData that fetches a JSON document and has some methods for you to cleanly get data from it. You also have a StoredData model that keeps retrieved content in your database to be displayed later.

Now, you want to automate the process of calling RemoteData.fetch('url') and then calling StoredData.create :params on the returned data. To do that, you would create a "rake task". Here's an example:

# lib/tasks/fetch.rake
desc "Fetch remote data and persist it"
task :fetch => :environment do
  RemoteData.fetch('url').each do |json_data|
    sd = StoredData.create :url => 'url', :data => json_data
    puts "Retrieved 'url' and saved data in record ##{sd.id} at #{DateTime.now}."
  end
end

Then you would set your system crontab up to run that as frequent as you would like. The Rake task handles the business logic, and your models handle interacting with the data. Nice and clean.

多情出卖 2024-10-25 01:04:13

您应该将业务逻辑放在模型中,将操作登录放在 rake 任务中,我认为这是此自动化任务的通常策略。你的 cron 应该看起来像:

cd /path/to/web_app && RAILS_ENV=environment /usr/local/bin/rake namespace:task

You should put the business logic in the model and the action login in a rake task, I consider this is the usual strategic for this automated tasks. Your cron should look like:

cd /path/to/web_app && RAILS_ENV=environment /usr/local/bin/rake namespace:task
风吹雨成花 2024-10-25 01:04:13

我会将抓取 json feed 的代码放入与 feed 中的数据相关的模型中。您可以为grab_feed方法编写测试并确保它正常工作。例如,如果你想废弃帖子,我会这样做:

class Post < ActiveRecord

  def self.grab_feed
    (...)
  end
end

然后你可以使用每当:https://github。 com/javan/whenever 每小时检查此内容:

 every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
   runner "Post.grab_feed"
 end

您不应该使用控制器,因为您希望抓取提要成为应用程序的内部部分。而且你不想看到它,所以绝对不是控制器的工作,而是模型。

I would put code of grabbing a json feed in model related with data from feed. You can write tests for grab_feed method and be sure that it works properly. For example if you want to scrap posts I would do something like this:

class Post < ActiveRecord

  def self.grab_feed
    (...)
  end
end

Then you can use whenever: https://github.com/javan/whenever to check this content every hour:

 every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
   runner "Post.grab_feed"
 end

You shouldn't use controller, because you want grabbing feed be internal part of application. Also you don't want to have view for that so definitely isn't controller job, but model.

神妖 2024-10-25 01:04:13

这里的问题是:你觉得这个 JSON 是一个视图还是只是“数据表示”?如果你考虑前者,你需要一个普通的控制器;如果您觉得它是数据,那么您可以编写一个 rake 任务并调用该方法(例如:puts Model.scope1.scope2.to_json

The question here is: do you feel this JSON is a view or just "data representation"? If you consider the former, you need a usual controller; if you feel it's data, then you could write a rake task and call the method (something like: puts Model.scope1.scope2.to_json)

变身佩奇 2024-10-25 01:04:13

您应该保留批处理作业,例如数据处理和从主服务网络应用程序填充。
编写一个 ruby​​ 脚本/rake 脚本并从 cron 运行它。

模型或控制器不是正确的地方。

以下是一些用于编写访问应用程序模型的脚本的示例代码。

require 'rubygems'
require 'active_record'

$config = YAML.load_file(File.dirname(__FILE__) + '/../green_app/config/'+ 'database.yml')
ActiveRecord::Base.establish_connection( $config[ENV['RAILS_ENV']] )
require File.expand_path File.dirname(__FILE__) + '/../green_app/app/models/post.rb'

来运行它。

export RAILS_ENV="production" && ruby /path/to/your/script.rb

并根据您的环境更改 RAILS_ENV 值

You should keep batch jobs like data processing and populating from main serving web-app.
Write a ruby script/rake script and run it from cron.

Model or controllers are not right places.

Following is some sample code to write scripts which accesses models of a app.

require 'rubygems'
require 'active_record'

$config = YAML.load_file(File.dirname(__FILE__) + '/../green_app/config/'+ 'database.yml')
ActiveRecord::Base.establish_connection( $config[ENV['RAILS_ENV']] )
require File.expand_path File.dirname(__FILE__) + '/../green_app/app/models/post.rb'

and run it as

export RAILS_ENV="production" && ruby /path/to/your/script.rb

change RAILS_ENV value depending on your environment.

煮茶煮酒煮时光 2024-10-25 01:04:13

你为什么不想把它放在控制器中?最后,这只是另一个操作,在本例中由自动用户(cron)驱动。我会执行一个 POST 请求,每小时触发一次以获取此新闻源并在必要时创建条目。

Why don't you want to put this in a controller? At last, it is just another action, driven by an automated user in this case (cron). I would do a POST request, triggered every hour to fetch this news feed and create entries if necessary.

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