Action when database changes


#1

Hello,

Is it possible to trigger an action in a controller when
a table from database changes?
I thought about using an utility like cron (railscron) to
check every, e.g. 2 s, if the table changed, but I am wondering
if there is a solution a bit more ‘elegant’: to trigger an action
exactly when the change is made.

Best regards,


#2

On 2/14/06, Jakub N. removed_email_address@domain.invalid wrote:


Kuba Nowak


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

You could use the after_save callback, along with the ruby http
library, to ping the controller.


Kyle M.
Chief Technologist
E Factor Media // FN Interactive
removed_email_address@domain.invalid
1-866-263-3261


#3

Kyle M. removed_email_address@domain.invalid wrote:

On 2/14/06, Jakub N. removed_email_address@domain.invalid wrote:

Is it possible to trigger an action in a controller when
a table from database changes?
I thought about using an utility like cron (railscron) to
check every, e.g. 2 s, if the table changed, but I am wondering
if there is a solution a bit more ‘elegant’: to trigger an action
exactly when the change is made.

You could use the after_save callback, along with the ruby http
library, to ping the controller.

I have two independent application (Rails app and C++ app) which
change the same table (but diffrent columns) in a database. The
scenario is following: Rails app populates a table with some data to
check and leaves the column ‘checked_at’ null. Next C++ app checks
this data and mark its activity by setting the column ‘checked_at’ at
the proper time. The results goes to new row in another table which
“belongs” to Rails app, it means, it’s mapped to Rails class.

I don’t know if this idea is correct, but I would like to know how to
find out when C++ app makes its work in order to take a glance (in
Rails app) at the results.

Any help, any ideas will be (very :)) apprecieted.


#4

On 2/15/06, Jakub N. removed_email_address@domain.invalid wrote:

library, to ping the controller.
find out when C++ app makes its work in order to take a glance (in

C++ system command executes curl/short ruby script that pings the
action?


Kyle M.
Chief Technologist
E Factor Media // FN Interactive
removed_email_address@domain.invalid
1-866-263-3261


#5

Dave T. removed_email_address@domain.invalid wrote:

However, after further thought, I’m wondering why you need all this
complexity? The only reason I can see why you’d need to have the Rails
app check the db every few seconds is because you are caching something.

Each user of my application can send a task (a request) for which the
calculating time may vary from one up to several minutes. So it’s
obvious that I’m forced to make him wait. But I don’t want to. I think
that if I used a rails app to submit tasks and another app to give the
response I would need a trick to notify a user that his task was
calculated. That’s the reason I wanted to check the state of database
to find out which tasks of which users are finished.

Maybe should I use a queuing system or something like that?

Performance is the issue. I will have about 2000 users, and wait for
demands of about 100 users at once.

Best regards,


#6

On 2/16/06, Jakub N. removed_email_address@domain.invalid wrote:

calculated. That’s the reason I wanted to check the state of database

upon every request and recompute your values. Only in slow Java

if there is a solution a bit more ‘elegant’: to trigger an action
the proper time. The results goes to new row in another table which


E Factor Media // FN Interactive
Cheers!
http://lists.rubyonrails.org/mailman/listinfo/rails

Kuba Nowak


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Look at this article for an example of running a queue with RailsCron:
http://kylemaxwell.com/articles/2006/01/26/simple-emailqueue-for-rails

For something like this, I prefer to send the user an email informing
him/her that the task is complete. O’Reilly Safari also sends emails
in similar situations.


Kyle M.
Chief Technologist
E Factor Media // FN Interactive
removed_email_address@domain.invalid
1-866-263-3261


#7

Kyle beat me to the curl suggestion :slight_smile:

However, after further thought, I’m wondering why you need all this
complexity? The only reason I can see why you’d need to have the Rails
app check the db every few seconds is because you are caching something.
I would simply have the Rails app grab the data directly from the DB
upon every request and recompute your values. Only in slow Java
applications have I found caching very useful…

On Wednesday, February 15, 2006, at 2:37 PM, Kyle M. wrote:

You could use the after_save callback, along with the ruby http
I don’t know if this idea is correct, but I would like to know how to
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Cheers!
–Dave T.
http://devlists.com - Email list management
http://palmsphere.com - Apps for your hand-held


#8

The approach I would take is to have the the task processor have a
“completion” flag/timestamp in the database, and corresponding
“has_been_read” flag/timestamp, or something along those lines. Any time
the user requests a page in the app, check to see if there are any
completed, unread notices and use the :flash to notify them somewhere in
the layout. Think along the lines of a “New Mail” alert on a portal or
webmail service.

If you want something more proactively pushed out, maybe because the app
window gets left open idle for long periods after task submission, maybe
have a div in your layout reserved for that sort of notice and have a
periodically_call_remote() check for newly completed jobs and put an
elert in the div. Use it sparingly, though, to save the server from
thousands of status checks a minute. Maybe only bring it into play in a
view when the user has a pending submitted task.

-sk

Jakub N. wrote:

Dave T. removed_email_address@domain.invalid wrote:

However, after further thought, I’m wondering why you need all this
complexity? The only reason I can see why you’d need to have the Rails
app check the db every few seconds is because you are caching something.

Each user of my application can send a task (a request) for which the
calculating time may vary from one up to several minutes. So it’s
obvious that I’m forced to make him wait. But I don’t want to. I think
that if I used a rails app to submit tasks and another app to give the
response I would need a trick to notify a user that his task was
calculated. That’s the reason I wanted to check the state of database
to find out which tasks of which users are finished.

Maybe should I use a queuing system or something like that?

Performance is the issue. I will have about 2000 users, and wait for
demands of about 100 users at once.

Best regards,