监视数据库表以了解 Rails 应用程序内的外部更改

发布于 2024-08-27 21:38:57 字数 745 浏览 17 评论 0原文

我正在我的 Rails 应用程序中集成一些非 Rails 模型表。一切都很顺利,我设置模型的方式是:

class Change < ActiveRecord::Base
  establish_connection(ActiveRecord::Base.configurations["otherdb_#{RAILS_ENV}"])
  set_table_name  "change"
end

这样我就可以通过 find 等对所有现有记录使用 Change 模型。

现在我想当记录添加到表中时运行某种通知。由于模型永远不会通过 Change.new 创建,并且使用 ActiveRecord::ObserverChange.save 不是一个选项。

每当添加新记录时,有什么方法可以执行我的一些 Rails 代码吗?我查看了 delayed_job 但不太明白如何设置它。我想象它是围绕一个 cron 作业演变的,该作业选择自作业上次运行以来创建的所有行,然后为每行调用相应的 Rails 代码。

更新 目前正在看Javan的Whenever,看起来可以解决'run来自 cron 部分的 Rails 代码。

I'm integrating some non-rails-model tables in my Rails application. Everything works out very nicely, the way I set up the model is:

class Change < ActiveRecord::Base
  establish_connection(ActiveRecord::Base.configurations["otherdb_#{RAILS_ENV}"])
  set_table_name  "change"
end

This way I can use the Change model for all existing records with find etc.

Now I'd like to run some sort of notification, when a record is added to the table. Since the model never gets created via Change.new and Change.save using ActiveRecord::Observer is not an option.

Is there any way I can get some of my Rails code to be executed, whenever a new record is added? I looked at delayed_job but can't quite get my head around, how to set that up. I imagine it evolves around a cron-job, that selects all rows that where created since the job last ran and then calls the respective Rails code for each row.

Update Currently looking at Javan's Whenever, looks like it can solve the 'run rails code from cron part'.

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

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

发布评论

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

评论(3

孤芳又自赏 2024-09-03 21:38:57

是的,您要么需要某种后台任务处理器(Delayed::Job 是流行的之一,或者您可以使用 Daemon 库或类似库来伪造自己的任务处理器),要么设置一个按某种时间表运行的 cronjob 。如果你想经常检查(比如每分钟),我建议使用 Delayed::Job 路线,如果它更长(每小时左右),那么 cron 作业就可以了。

走 DJ 路线,您需要创建一个作业来检查新记录,处理它们(如果有),然后重新排队该作业,因为每个作业在完成时都会被标记为“已完成”。

-乔恩

Yeah, you'll either want some sort of background task processor (Delayed::Job is one of the popular ones, or you can fake your own with the Daemon library or similar) or to setup a cronjob that runs on some sort of schedule. If you want to check frequently (every minute, say) I'd recommend the Delayed::Job route, if it's longer (every hour or so) a cron job will do it just fine.

Going the DJ route, you'd need to create a job that would check for new records, process them if there are any, then requeue the job, as each job is marked "completed" when it's finished.

-jon

情痴 2024-09-03 21:38:57

这就是我最终所做的:使用 Whenever,因为它与 Capistrano 很好地集成,并向我展示了如何运行来自 cron 中的 Rails 代码。我缺少的和平基本上是

script/runner -e production 'ChangeObserver.recentchanges'

现在每 5 分钟运行一次。 recentchanges 从 tmp 文件中读取最后查看的 ID,提取 ID 高于该 ID 的所有新 Change 记录,并为每个记录运行正常的观察者代码(当然,并将最高查看的 ID 保存到 tmp 文件中)。

This is what I finally did: Use Whenever, because it integrates nicely with Capistrano and showed me how to run Rails code from within cron. My missing peace was basically

script/runner -e production 'ChangeObserver.recentchanges'

which is now run every 5 minutes. The recentchanges reads the last looked-at ID from a tmp-file, pulls all new Change records which have a higher ID than that and runs the normal observer code for each record (and saves the highest looked-at ID to the tmp-file, of course).

通知家属抬走 2024-09-03 21:38:57

与通常监视状态变化一样,有两种方法:轮询和通知。您现在似乎选择了轮询方式(使用 cron 作业定期查看数据库的状态,并在发生变化时执行一些代码)
您可以使用其中一个 Rails 调度程序来做同样的事情,那里有一些(谷歌会很容易找到它们,它们有各种功能集,如果您这样做的话,我会让您选择适合您需要的一个)

您也可以尝试根据您的数据库采用通知方式。某些数据库支持触发器和外部进程执行或特定通知协议。
在这种情况下,数据库本身会通知您表已更改。 从数据库获取事件中的各种 DBMS 有许多此类选项

As usual with monitoring state changes, there are two approaches : polling and notification. You seem to have chose to go the polling way for now (having a cron job look at the state of the database on a regular basis and execute some code if that changed)
You can do the same thing using one of the rails schedulers, there are a few out there (google will find them readily, they have various feature sets, I'll let you choose the one which suits your need if you got that way)

You could also try to go the notification way depending on your database. Some database support both triggers and external process execution or specific notification protocols.
In this case you are notified by the database itself that the table changed. there are many such options for various DBMS in Getting events from a database

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