如何监听由 3rd 方应用程序触发的数据库更改 [英] How to listen for database changes triggered by a 3rd party application

查看:33
本文介绍了如何监听由 3rd 方应用程序触发的数据库更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 Ruby on Rails 应用程序与第 3 方应用程序集成,这将共享一个公共 PostgreSQL 数据库.也就是说,Rails 应用和第 3 方应用将使用相同的 PG 数据库.

I need to integrate a Ruby on Rails application with a 3rd-party application, which will share a common PostgreSQL database. That is, both the Rails app and the 3rd-party app will be using the same PG database.

是否可以在 Rails 应用中设置侦听器,以便在第 3 方应用添加、编辑或删除数据库记录时触发事件?

Is it possible to set a listener within the Rails app, such that an event is fired when the 3rd-party app adds, edits or deletes a database record?

例如,假设我在 Rails 中定义了一个 Book 模型,并且第 3 方应用程序在数据库的 Book 表中添加了一个新行(不是通过 BooksController).

For example, say I have a Book model defined in Rails, and the 3rd-party app adds a new row in the database's Book table (not via the BooksController).

Rails 应用程序能否检测到已添加新记录,或已编辑/删除现有记录?

Can the Rails application detect that a new record has been added, or that an existing record has been edited/deleted?

推荐答案

这就是我最终做的:使用 Whenever,因为它与 Capistrano 很好地集成并向我展示了如何从 cron 中运行 Rails 代码.我失去的平静基本上是

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 制作 'ChangeObserver.recentchanges'现在每 5 分钟运行一次.最近的更改从 tmp 文件中读取最后查看的 ID,提取所有新的更改记录,这些记录的 ID 高于该 ID,并为每条记录运行正常的观察者代码(并将最高查看的 ID 保存到 tmp 文件,当然).

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

这篇关于如何监听由 3rd 方应用程序触发的数据库更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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