Monitoring a database table for external changes from a Rails application

I am integrating some non-rail tables in a Rails application. Everything works very well, as I created the model:

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

That way I can use the model Changefor all existing entries with find, etc.

Now I would like to run some kind of notification when a record is added to the table. Since a model is never created using Change.newand Change.saveusing ActiveRecord::Observer, this is not an option.

Is there any way I can get my Rails code to be executed whenever a new entry is added? I looked at delayed_job, but I can’t tear my head off how to do it. I assume that it develops around cron-job, which selects all the lines that have been created since the job was last run, and then calls the corresponding Rails code for each line.

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

+3
source share
3 answers

, , , : Whenever, Capistrano , Rails- cron.

script/runner -e production 'ChangeObserver.recentchanges'

5 . recentchanges tmp , Change, , , ( tmp , ).

+1

, - (Delayed:: Job - , Daemon ) cronjob, . ( , ), Delayed:: Job, ( ), cron .

- , , , , , "", .

-jon

+1

, : . , ( cron , ) , rails, (google , , , , )

You can also try following the notification path, depending on your database. Some databases support both triggers and external process execution or specific notification protocols. In this case, you yourself are notified by the base that the table has changed. there are many such options for various DBMSs in obtaining events from the database

+1
source

Source: https://habr.com/ru/post/1739458/


All Articles