Forum: Ruby on Rails Cron jobs, runner and methods. How to?

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.
E98de78bb42013c488fe8aa9d77d2cb1?d=identicon&s=25 Steve Odom (Guest)
on 2005-12-11 17:43
(Received via mailing list)
I want to execute a method in a controller at scheduled intervals
throughout
the day. Can others provide examples of how they do this?

The excerpt below from the wiki page "
http://wiki.rubyonrails.org/rails/pages/HowToRunBa...
is basically what I want to do, but I can't find enough other
information to
fill in the gaps:

<snip>
Use cron or the like to run ruby code (a helper to your app).

You could have a cron job launch your "escalate_bugs" script.

For info regarding accessing your data from a ruby script see
HowToPopulateYourDbFromScript<http://wiki.rubyonrails.org/rails/pages/HowToPopul...
how
to access *your* Rails environment from a
script.<http://wiki.rubyonrails.org/rails/pages/HowToRunBa...

You can have cron call script/runner to execute a one line command such
as:


* * * * * /path/to/app/script/runner -e production
"User.clean_up_expired()"

</snip>
I have had difficulty finding documentation on how to use script/runner.
I
am also a newbie to cron jobs.

My goal is to run a method in controller/products called "updatePrices"
4
times a day using a cron job. Can someone provide sample code to do
that.

I will post any solution on the above wiki page.

Thanks very much,

Steve
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2005-12-12 01:48
(Received via mailing list)
On Dec 11, 2005, at 8:40 AM, Steve Odom wrote:

>
> * * * * * /path/to/app/script/runner -e production
>
> Thanks very much,
>
> Steve

Hey Steve-

	Ok lets get this running for you. So 4 times a day is every 6 hours.
We will make your job run at 2 minutes after the hour every six
hours. This way we wont conflict with other scheduled hourly tasks.
So our cron entry will look like this:

02 6 * * *  /path/to/app/script/runner -e production
"Product.update_prices"

	I already know you are running debian so you will want to put this
line in /etc/cron/daily . But you don't usually do this directly. On
debian you run this command:

crontab -e

Then paste in
02 4 * * *  /path/to/app/script/runner -e production "Product.
update_prices"
then save and exit the editor

	Change production to development if you need to. And you will want
to make the update_prices method in your Product Model instead of
your controller. Usually jobs like this will interact with your data
model and not need to use your controller. So you need to define a
class method in your Product model like this:

Product < AR::Base

   def self. update_prices
       # update prices and do whatever else is needed
   end

end

	Hopefully that should get you going.

Cheers-

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
B79f6edafee26860eb2ef239558bf8dc?d=identicon&s=25 Lee Pope (Guest)
on 2005-12-12 01:51
(Received via mailing list)
Not knowing much about how your app is organized, i would suggest that
what
you describe would make more sense on the Product model. You could still
have a method on the Products controller that would delegate to that
method,
if you needed a way to fire it from your web interface. You could then
run

<pre><code>
    ./script/runner 'Product.UpdatePrices()'
</code></pre>

as a cron job.

If you need help setting up the cron job, please add some information
about
your server -- what OS are you running, is it hosted, do you have shell
access?

Lee
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2005-12-12 03:33
(Received via mailing list)
Ezra Zygmuntowicz wrote:

> Ok lets get this running for you. So 4 times a day is every 6 hours. We
> will make your job run at 2 minutes after the hour every six hours. This
> way we wont conflict with other scheduled hourly tasks. So our cron
> entry will look like this:
>
> 02 6 * * *  /path/to/app/script/runner -e production
> "Product.update_prices"|

This will run the job at 6:02. To run every 6 hours you need:

02 */6 * * *  /path/to/app/script/runner -e production
"Product.update_prices"

or

02 0,6,12,18 * * *  /path/to/app/script/runner -e production
"Product.update_prices"

either of which will run the job at 0:02, 6:02, 12:02, and 18:02. Both
should be on one line.

> crontab -e

> Then paste in
> 02 4 * * *  /path/to/app/script/runner -e production
> "Product.update_prices"

This runs once at 4:02. Change as above.


--

Ray
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2005-12-12 06:39
(Received via mailing list)
On Dec 11, 2005, at 6:30 PM, Ray Baxter wrote:

>
>
> Ray
Ahh my bad. Thanks Ray.

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
E98de78bb42013c488fe8aa9d77d2cb1?d=identicon&s=25 Steve Odom (Guest)
on 2005-12-12 18:15
(Received via mailing list)
Thanks for all the help guys.

I cheated a bit and used my webmin cron job interface to set it up. I
did
use crontab -e to check it and it seems to be input correctly:
4 0,4,8,12,16,20 * * * /home/steve/myappname/script/runner -e
development "
Product.update_all"

crontab actually seems pretty easy to easy. Ezra is going to make me a
bash/command line guy yet ; )

If I test it and run it right now I get the following error:

/home/steve/myappname/script/runner: Permission denied

I tried running it as both root and my user name.

Any ideas?
Steve
00e3a96684ab390a350b0271e98741d3?d=identicon&s=25 Nshbrown Nshbrown (nshb)
on 2005-12-12 19:01
(Received via mailing list)
You likely have to chmod +x script/runner

-Nb

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Nathaniel S. H. Brown                           http://nshb.net
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E98de78bb42013c488fe8aa9d77d2cb1?d=identicon&s=25 Steve Odom (Guest)
on 2005-12-13 13:31
(Received via mailing list)
> You likely have to chmod +x script/runner


Yes, that was my problem.

Now, one last problem. The suggestion was to move my method I wanted to
call
with a cron job to my model:

And you will want to make theupdate_prices method in your Product Model
> instead of your controller. Usually jobs like this will interact with your
> data model


However, in that method I need to do things like perform some
calculations,
determine whether I need to update the prices are not, and preprocess
and
call and Amazon library. Here is an example of
submethods that are being called:

def calculate_required_update(rank)
    time = case rank
            when 0..10000 then 1.hours.from_now
            when 10001...200000 then 24.hours.from_now
            else 96.hours.from_now
           end
  end

It doesn't seem like these kinds of things belong in my model. I don't
think
I can call controller or helper methods from my model (or at least I'm
getting errors when I do).

So my question is: Can I have the cron job call a method in my
controller?

Thanks,

Steve
E4806eda13ad513a515ee2c15ef19e22?d=identicon&s=25 Harrison (Guest)
on 2006-02-08 20:03
(Received via mailing list)
> So my question is: Can I have the cron job call a method in my controller?
Thanks,Steve

Steve - don't know if you resolved this yet, but I am/was in the same
position
as you were.  I want to run some maintenance tasks on a daily basis.
These
tasks originally were controller actions.  There seems to be two
approaches to
handling this at this:

1. Run wget http://yoursite/yourcontroller/youraction via cron

2. Convert your controller actions to model methods as described above,
and use
script/runner via cron (or kyle maxwell's rails_cron) to run them.

I'm currently testing each approach to figure out which is best.  The
wget
approach may be a little quick/dirty whereas using script/runner
probably is
more legit.

When I converted the maintenance actions to model methods, I also ran
into the
problem you did about not being able to access helper methods in the
model, so I
converted those to new model methods as well (using self.<helper name>).
It's
not that big of deal, but I wonder why you can't use helper methods in
the model?

Now I'm wondering if it matters that I have 1000 new lines of code in my
model
that used to be in controllers and view templates, i.e. if it will
somehow be a
performance issue.

As for the more straightforward wget approach, I've heard that there may
be
timeout issues when running this for longer processes versus using
script/runner.

Any more comments would be appreciated.
E98de78bb42013c488fe8aa9d77d2cb1?d=identicon&s=25 Steve Odom (Guest)
on 2006-02-08 20:28
(Received via mailing list)
Hi Harrison,

I went the wget route. Putting all that stuff in my model didn't seem
right.


I'd be curious what you ultimately decide to do.

Steve
http://www.smarkets.net
B20f60d4dae0d5f372c73caf1418565a?d=identicon&s=25 Jason Stirman (stirman)
on 2006-06-07 20:59
Any more insight to the best way to handle this?

wget with methods in the controller?

or

script/runner with methids in the model?

-stirman
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-06-07 21:19
(Received via mailing list)
or rails_cron with methods in the model.

http://tinyurl.com/k994k

--
-- Tom Mornini
This topic is locked and can not be replied to.