# Scheduled tasks

**URL:** <https://www.ruby-forum.com/t/scheduled-tasks/131515>\
**Category:** Rails\
**Created:** [March 10, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515 "2008-03-10T22:01:37Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 10, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/1 "2008-03-10T22:01:37Z")

</div>

Hi

What is the best way to go about creating a service that has access to  
my rails environment, but that runs on its own?

My specific problem is creating a process that runs in the background  
and updates certain models every 5 minutes. Is there a standard way of  
creating something like this?

Thanks  
Ryan

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 10, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/2 "2008-03-10T22:25:24Z")

</div>

On 10 Mar 2008, at 22:00, Ryan wrote:

> What is the best way to go about creating a service that has access to  
> my rails environment, but that runs on its own?
> 
> My specific problem is creating a process that runs in the background  
> and updates certain models every 5 minutes. Is there a standard way of  
> creating something like this?

- cron tab and script/runner
- backgroundrb’s scheduler
- small ruby daemon with loop with sleep

You can choose what suits you best.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 11, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/3 "2008-03-11T17:21:32Z")

</div>

I’ll chime in a small variant of Peter’s suggestions. If you have a  
ruby or other program running, you might want to run it from inittab  
to keep it running no matter what.

I vote for crontab calling the shots and calling script/runner.

H

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 11, 2008, 7:13pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/4 "2008-03-11T19:13:20Z")

</div>

Same here, crontabs would be my personal favorite too. It takes a few  
seconds to start up the rails process, but at least it releases memory  
again afterwards. Backgroundrb being a persistent process leaked  
memory for me, which lead to server crashes over time (earlier  
versions, haven’t tried the last one).

On 11 Mar 2008, at 17:20, blasterpal wrote:

> > - cron tab and script/runner
> > - backgroundrb’s scheduler
> > - small ruby daemon with loop with sleep

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 11, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/5 "2008-03-11T22:09:03Z")

</div>

Thanks for the help.

What if I wanted it to constantly be working. For example, monitoring  
a database table for jobs to process?

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 11, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/6 "2008-03-11T23:05:41Z")

</div>

On 11 Mar 2008, at 22:08, Ryan wrote:

> Thanks for the help.
> 
> What if I wanted it to constantly be working. For example, monitoring  
> a database table for jobs to process?

Depending on what you mean exactly, you could use triggers in the  
database (if the processing can happen there)

> **[MySQL :: MySQL 9.7 Reference Manual :: 27.4 Using Triggers](https://dev.mysql.com/doc/refman/9.7/en/triggers.html)**

If you’re talking about your rails app saving something to the db and  
you want to process it without locking a mongrel or the user, you  
could use backgroundrb, starling or some other asynchronous processor  
out there. There’s a chapter devoted to such setups in the excellent  
Advanced Rails Recipes book.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 11, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/7 "2008-03-11T23:17:38Z")

</div>

Has anybody used rufus-scheduler? Opinions? I just downloaded it and  
am trying to get it to work. I’m somewhat new to rails so the fix is  
not obvious.

I need to run a task every 5 minutes that gathers data and adds it to my  
database, so the periodic task needs access to ActiveRecord to save  
objects.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 11, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/8 "2008-03-11T23:21:23Z")

</div>

Steven L. wrote:

> Has anybody used rufus-scheduler? Opinions? I just downloaded it and  
> am trying to get it to work. I’m somewhat new to rails so the fix is  
> not obvious.
> 
> I need to run a task every 5 minutes that gathers data and adds it to my  
> database, so the periodic task needs access to ActiveRecord to save  
> objects.

If anybody is interested, here are the errors. It doesn’t seem to be  
finding an ActiveRecord object it’s trying to access:

running incremental\_scan:  
trigger() caught exception  
A copy of QueryController has been removed from the module tree but is  
still active!  
/usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active\_support/dependencies.rb:237:in  
`load_missing_constant' /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:469:in `const\_missing’  
/home/adminline/cvswatch/app/controllers/query\_controller.rb:43:in  
`incremental\_scan’

The QueryController contains a method called incremental\_scan.  
Incremental\_scan is configured to run every 5 minutes. However when the  
scheduler calls it, it throws an exception when it tries to do a  
.find(1) from an ActiveRecord object.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 11, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/9 "2008-03-11T23:36:27Z")

</div>

Ok, I got it to work using script/runner but first I had to move my  
method from my controller to a model and make the methods static.

Thanks.

Steve

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 12, 2008, 1:53am UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/10 "2008-03-12T01:53:05Z")

</div>

On Tue, 2008-03-11 at 19:12 +0100, Peter De Berdt wrote:

> Same here, crontabs would be my personal favorite too. It takes a few  
> seconds to start up the rails process, but at least it releases memory  
> again afterwards. Backgroundrb being a persistent process leaked  
> memory for me, which lead to server crashes over time (earlier  
> versions, haven’t tried the last one).  
> te

Hmm, I doubt that BackgrounDRb itself leaked memory. However with newer  
version there is an option to keep scheduled workers not persistent. As  
in, your worker will be stared afresh each time schedule is arrived.  
Just define your usual configuration file and in worker:

class FooWorker \< BackgrounDRb::MetaWorker  
set\_worker\_name :foo\_worker  
reload\_on\_schedule true  
end

IMHO BackgrounDRb has advantage that you can easily monitor status of  
your workers. You wouldn’t need a separate crontab entry and since  
everything is under RAILS\_ROOT you can manage it with Capistrano or  
Vlad.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 11, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/11 "2008-03-11T23:31:33Z")

</div>

Steven L. wrote:

> Has anybody used rufus-scheduler? Opinions? I just downloaded it and  
> am trying to get it to work. I’m somewhat new to rails so the fix is  
> not obvious.
> 
> I need to run a task every 5 minutes that gathers data and adds it to my  
> database, so the periodic task needs access to ActiveRecord to save  
> objects.

Yes, I am just in the process of using rufus-scheduler.

To use ActiveRecord models from your rails application you need to bring  
in the rails environment. Put your script that uses rufus-scheduler in  
your rails root application directory. Near the top of my little script:

rails\_root = File.expand\_path(File.join(File.dirname( **FILE** )))  
RAILS\_HOME = rails\_root

require RAILS\_HOME + ‘/config/environment’

Then you can use schedules like:

scheduler.schedule\_every(“1m00s”) do  
puts Time.now.to\_s + “…popping up every 1 minute…”  
Session.delete\_all(“updated\_at \< now()-‘20 minutes’::interval”)  
end

Does this help?

Cheers,  
Gary.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 12, 2008, 11:01am UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/12 "2008-03-12T11:01:59Z")

</div>

On 12 Mar 2008, at 01:52, hemant kumar wrote:

> Hmm, I doubt that BackgrounDRb itself leaked memory.

Well, I said I didn’t use the latest version yet, and if I look in the  
changelog, it clearly says:

2008-02-28 hemant kumar [hemant@shire](mailto:hemant@shire)

- fixed some meory leaks.

My problems had to do with exporting records to csv (with lots of  
calculations being done on them before exporting). I tested the same  
report generator from a normal Rails process and it was stable in  
memory use, handing it over to BackgroundRB caused the server to run  
out of memory in a matter of days (after it had been running without a  
hitch for over a year).

That said, I believe backgroundrb is a good solution and the  
advantages the other posters claim are indeed true. And given that  
it’s easy to integrate and if it fails for you, just as easy to take  
out again and replace with something else, I see no reason not to try  
it (I might well give it another shot in the future :-))

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 12, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/13 "2008-03-12T12:16:54Z")

</div>

On Wed, 2008-03-12 at 11:00 +0100, Peter De Berdt wrote:

> - fixed some meory leaks.
> 
> My problems had to do with exporting records to csv (with lots of  
> calculations being done on them before exporting). I tested the same  
> report generator from a normal Rails process and it was stable in  
> memory use, handing it over to BackgroundRB caused the server to run  
> out of memory in a matter of days (after it had been running without a  
> hitch for over a year).

Gosh, I feel naked. I dunno if you were affected by those bugs. But  
anyways, since a fix was posted about 20 days back(and was in git repo  
for much longer time), I think its okay.

> That said, I believe backgroundrb is a good solution and the  
> advantages the other posters claim are indeed true. And given that  
> it’s easy to integrate and if it fails for you, just as easy to take  
> out again and replace with something else, I see no reason not to try  
> it (I might well give it another shot in the future :-))

Yup, fully agree. Choose what works for you. Heck, I will dump bdrb in a  
zip if it didn’t work for me. I just wanted to draw your attention to  
the facility of reloading workers on schedule, so as they are not  
persistent and gets loaded only on scheduled time.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 12, 2008, 10:10am UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/14 "2008-03-12T10:10:44Z")

</div>

Everywhere I look, I see people saying negative things about  
backgroundrb, about how it is unstable and is a memory leak or adds a  
bunch of hassles. I assume this must have been true some time in the  
past (perhaps when backgroundrb was actually using drb).

However, I have been using backgroundrb (with scheduling) [latest  
version] and it seems stable, easy to use and very low memory. I think  
Hemant must have made some radical revisions because nothing bad I hear  
about it rings true anymore as far as I know. For one it doesn’t even  
use drb.

I would really recommend backgroundrb as it is easy to control through  
capistrano and monitor using monit. Also it has full access to the rails  
environment, models, etc.

Just my two cents.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 12, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/15 "2008-03-12T12:23:24Z")

</div>

On Wed, 2008-03-12 at 10:10 +0100, Nathan E. wrote:

> I would really recommend backgroundrb as it is easy to control through  
> capistrano and monitor using monit. Also it has full access to the rails  
> environment, models, etc.

There were/are couple of problems around bdrb.

1. Google search about BackgrounDRb refers to documentation/tutorials  
which are very old.
2. There have been too many bleeding versions and hence the confusion.
3. I am a lousy and lazy programmer.

We are improving things bit by it and hope its usable for everyone as  
its usable for you and me.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [March 12, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/scheduled-tasks/131515/16 "2008-03-12T14:42:15Z")

</div>

Gary D. wrote:

> To use ActiveRecord models from your rails application you need to bring  
> in the rails environment. Put your script that uses rufus-scheduler in  
> your rails root application directory. Near the top of my little script:
> 
> rails\_root = File.expand\_path(File.join(File.dirname( **FILE** )))  
> RAILS\_HOME = rails\_root
> 
> require RAILS\_HOME + ‘/config/environment’
> 
> Then you can use schedules like:
> 
> scheduler.schedule\_every(“1m00s”) do  
> puts Time.now.to\_s + “…popping up every 1 minute…”  
> Session.delete\_all(“updated\_at \< now()-‘20 minutes’::interval”)  
> end
> 
> Does this help?

Yes, I learned several things by reading this. Thanks!

Steve
