加载Ruby on Rails的模型,而无需加载整个框架 [英] Load Ruby on Rails models without loading the entire framework

查看:214
本文介绍了加载Ruby on Rails的模型,而无需加载整个框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个自定义守护进程将运行各种数据库任务,如延迟邮件和用户通知(每份通知是通知表中的单独行)。我不想使用脚本/亚军来完成这些任务,因为它是可能的一些任务只需要一个或两个数据库中的行或数千根据任务的行的创建。我不希望启动一个Ruby进程或加载整个Rails框架的每个操作的开销。我打算把这个后台程序在内存中全职工作。

I'm looking to create a custom daemon that will run various database tasks such as delaying mailings and user notifications (each notice is a separate row in the notifications table). I don't want to use script/runner or rake to do these tasks because it is possible that some of the tasks only require the create of one or two database rows or thousands of rows depending on the task. I don't want the overhead of launching a ruby process or loading the entire rails framework for each operation. I plan to keep this daemon in memory full time.

要创建这个守护我想用我的模型从我的Ruby on Rails应用。我有一些Rails插件,如 acts_as_tree AASM ,我需要加载,如果我在这里用的模型。一些我需要加载的插件是我创建的的ActiveRecord :: Base的定制黑客。 (我愿意接受删除或重新编写了一些,如果他们需要从导轨的其它部分组件的插件。)

To create this daemon I would like to use my models from my ruby on rails application. I have a number of rails plugins such as acts_as_tree and AASM that I will need loaded if I where to use the models. Some of the plugins I need to load are custom hacks on ActiveRecord::Base that I've created. (I am willing to accept removing or recoding some of the plugins if they need components from other parts of rails.)

我的问题是

  • 这是一个好主意?
  • 还有 - 这是可以做到的方式,没有我手动包括在我的模型和插件每个文件

如果不是一个好主意。

  • 什么是一个很好的选择?

(我不是并列做写我自己的SQL查询,但我将不得不增加数据库约束和一个单独的用户的守护程序prevent任何愚蠢的意外。由于我不熟悉与配置数据库,我想用活动记录作为一个拐杖。)

(I am not apposed to doing writing my own SQL queries but I would have to add database constraints and a separate user for the daemon to prevent any stupid accidents. Given my lack of familiarity with configuring a database, I would like to use active record as a crutch.)

推荐答案

您最多可以装入滑轨等的ActiveRecord的一个方面,但是当你一直到它加载整个环境的成本并不比只是装载更多ActiveRecord的本身。你可以肯定只是不包括像的ActionMailer或某些侧面位方面,但我会猜,你不会看到太多取胜出来。

You can load up one aspect of Rails such as ActiveRecord but when you get right down to it the cost of loading the entire environment is not much more than just loading ActiveRecord itself. You could certainly just not include aspects like ActionMailer or some of the side bits but I'm going to guess that you're not going to see much win out of it.

我的建议,而不是要么是通过流道运行/像你控制台说你不想要,但而不是自举每次,尝试批量的事情让你做的1. 1000的时间,而不是有很多使用这种风格的项目,一些批量邮寄春天介意,如果你想的例子。 DJ(delayed_job的)做类似的通过存储在数据库中有点说这code必须在将来某个时候使用环境栈运行,但它试图一批在一起一样,因为它可以让你可能会赢得从那个。

What I would suggest instead is either running through runner/console like you said you didn't want to but rather than bootstrapping each time, try to batch things so that you're doing 1000 at a time instead of 1. There are a lot of projects that use this style, some of the bulk mailers spring to mind if you want examples. DJ (delayed_job) does similar by storing a bit in the database saying that this code needs to be run at some point in the future using the environment stack but it tries to batch together as much as it can so you may get win from that.

另一种选择是有一个持续的迷你Rails应用程序尽可能多的剥离尽可能使该内存使用量低,可以监听请求,做您的出价,当你想要它。这将是更多的内存,但引导的等待时间将基本无效。

The other option is to have a persistent mini-rails app with as much stripped out as possible so that the memory usage is lower which can listen for requests and do your bidding when you want it to. This would be more memory but the latency of bootstrapping would be essentially nullified.

最后,在事后,这将是Postgres的一个伟大的使用。

Lastly, as an afterthought, this would be a great use for Postgres.

这篇关于加载Ruby on Rails的模型,而无需加载整个框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆