# Cron job in single environment

**URL:** <https://www.ruby-forum.com/t/cron-job-in-single-environment/195774>\
**Category:** Rails\
**Created:** [September 29, 2010, 11:00pm UTC](https://www.ruby-forum.com/t/cron-job-in-single-environment/195774 "2010-09-29T23:00:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [September 29, 2010, 11:00pm UTC](https://www.ruby-forum.com/t/cron-job-in-single-environment/195774/1 "2010-09-29T23:00:06Z")

</div>

Hi,

We have an app running that imports near realtime values from a feed.  
We did that using BackgroundRb, but I was pointed at the fact that  
BackgroundRb has quite the memory footprint.

So I switched to a rake task using a regular Linux cronjob. However,  
the feed we’re using only accepts one concurrent connection from our  
(whitelisted) IP at a time. With BackgroundRb I’d just initialize a  
single connection in the create method of the worker and make every  
worker instance use that connection. I’d only reinitialize the  
connection when it isn’t active anymore.

With rake, however, I can’t seem to do such a thing. I tried setting a  
global $connection in an initializer and checking its state on every  
call of the rake task. If active -\> use, if not -\> reinitialize. It  
doesn’t work because every rake task seems to fire off a new instance  
of the app. If the rake tasks overlap (which they do from time to  
time), the second rake task will fail because the connection is  
already open.

How would you approach this? Can this be done using a rake task? Or do  
we need to switch to another cronjob-like system?

Regards,  
Jaap H.
