Forum: Ruby on Rails Action when database changes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1fa6dae9e513c52473f0b1ccd8173c27?d=identicon&s=25 Jakub Nowak (Guest)
on 2006-02-14 20:19
(Received via mailing list)
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,
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 Kyle Maxwell (Guest)
on 2006-02-14 20:46
(Received via mailing list)
On 2/14/06, Jakub Nowak <jnowak@wmid.amu.edu.pl> wrote:
> --
> Kuba Nowak
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> 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 Maxwell
Chief Technologist
E Factor Media // FN Interactive
kyle@efactormedia.com
1-866-263-3261
1fa6dae9e513c52473f0b1ccd8173c27?d=identicon&s=25 Jakub Nowak (Guest)
on 2006-02-15 23:24
(Received via mailing list)
Kyle Maxwell <kyle@kylemaxwell.com> wrote:
> On 2/14/06, Jakub Nowak <jnowak@wmid.amu.edu.pl> 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.
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 Kyle Maxwell (Guest)
on 2006-02-15 23:39
(Received via mailing list)
On 2/15/06, Jakub Nowak <jnowak@wmid.amu.edu.pl> 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 Maxwell
Chief Technologist
E Factor Media // FN Interactive
kyle@efactormedia.com
1-866-263-3261
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Dave Teare (Guest)
on 2006-02-15 23:54
(Received via mailing list)
Kyle beat me to the curl suggestion :)

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 Maxwell 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
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails
>


Cheers!
--Dave Teare
http://devlists.com - Email list management
http://palmsphere.com - Apps for your hand-held
1fa6dae9e513c52473f0b1ccd8173c27?d=identicon&s=25 Jakub Nowak (Guest)
on 2006-02-16 20:36
(Received via mailing list)
Dave Teare <devlists-rubyonrails@devlists.com> 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,
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 Kyle Maxwell (Guest)
on 2006-02-16 21:22
(Received via mailing list)
On 2/16/06, Jakub Nowak <jnowak@wmid.amu.edu.pl> 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
> Rails@lists.rubyonrails.org
> 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-...

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 Maxwell
Chief Technologist
E Factor Media // FN Interactive
kyle@efactormedia.com
1-866-263-3261
34f5b045aec62235c17458650ea75353?d=identicon&s=25 Steve Koppelman (hatless)
on 2006-02-16 21:50
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 Nowak wrote:
> Dave Teare <devlists-rubyonrails@devlists.com> 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,
This topic is locked and can not be replied to.