Running Rails tasks by schedule, instead of an HTTP request

Hi there

I’m considering Rails for a new web-application planned to be built
from scratch.

The application has an extensive web front, and in fact most of the
application is interfaced through that front and engaged through
clients’ HTTP requests (i.e. the conventional way web-applications and
“dynamic web-sites” work). However, some essential parts are supposed
to operate more like a non-web server-application: they are supposed
to be initiated by a scheduler, without any prior client HTTP request.

My questions are:

  1. How suitable is Rails for such an application? How suitable is it
    for an application where core parts of the logic are not associated
    with any web concept (not coupled to HTML, HTTP, sending pages to
    clients, etc.)?

  2. How easy is it to run scheduled Rails tasks? Is Rails suitable for
    that?

  3. Finally: I’m planning to go with a rather conventional, mass-market
    host (probably DreamHost) with shared hosting. Would the answers to 1
    and 2 still be valid for such a hosting solution? Or would I need a
    dedicated server?

Regards,
Alder G.

look at script/runner…a tool for accessing models and their methods
from the command line…

script/runner is made just for your purpose and fits nice into a host
with cron or some other scheduler
On Friday, March 24, 2006, at 10:20 AM, Alder G. wrote:

to be initiated by a scheduler, without any prior client HTTP request.

http://lists.rubyonrails.org/mailman/listinfo/rails
Mikkel B.

www.strongside.dk - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk - Buy Old Stuff!(DK)

Alder,

Simply use plain Ruby for the scheduled parts of your application, or
whatever scripting / programming language you are comfortable with (PHP,
etc). Rails is for web applications using the MVC pattern. Not really
suitable for scheduled tasks IMO unless you go and schedule calls to
HTTP
pages. You could incorporate bits of Rails into your scheduled scripts,
e.g.
ActiveRecord.

If you are prepared to go to the cost of a dedicated server, why not
just go
for specialised Rails hosting? Ask if they support user added cron jobs.
Obviously a dedicated server is the most flexible solution, but only if
you
can justify the expense (including backups, etc) and the extra admin
time.

Larkin

Hi

I know Ruby well, so I’ll probably just use it. Note that I’d still be
using parts of Rails’ MVC, in particular the Models. Mikkel B.
suggested it would be possible to accomplish through script/runner. If
that’s indeed the case, I could schedule script/runner runs with with
cron (cron is available on shared hosting solutions, right?) and
there’s no reason not to use Rails and my favorite language, Ruby :slight_smile:

Cheers,
Alder

On Mar 24, 2006, at 2:58 AM, Alder G. wrote:

I know Ruby well, so I’ll probably just use it. Note that I’d still be
using parts of Rails’ MVC, in particular the Models. Mikkel B.
suggested it would be possible to accomplish through script/runner. If
that’s indeed the case, I could schedule script/runner runs with with
cron (cron is available on shared hosting solutions, right?) and
there’s no reason not to use Rails and my favorite language, Ruby :slight_smile:

script/runner is wonderful as it allows use of the entire RoR
application
environment including models, environment constants and configurations,
etc.

See also rails_cron, as it allows you to dispense with cron. As much as
I love cron, rails_cron is even better because now, when installing on
a new machine, you don’t have to remember to install the cron events as
well! :slight_smile:


– Tom M.

On Fri, 2006-03-24 at 12:58 +0200, Alder G. wrote:

Hi

I know Ruby well, so I’ll probably just use it. Note that I’d still be
using parts of Rails’ MVC, in particular the Models. Mikkel B.
suggested it would be possible to accomplish through script/runner. If
that’s indeed the case, I could schedule script/runner runs with with
cron (cron is available on shared hosting solutions, right?) and
there’s no reason not to use Rails and my favorite language, Ruby :slight_smile:

Cron + script/runner is perfect for this… and so are scheduled rake
tasks.

-Robby


/**************************************************************

  • Robby R., Founder & Executive Director *
  • PLANET ARGON, LLC | www.planetargon.com *
  • Ruby on Rails Development, Consulting, and Hosting *
  • Portland, Oregon | p: 503.351.4730 | f: 815.642.4068 *
  • blog: www.robbyonrails.com | book: www.programmingrails.com *
    ***************************************************************/

+1 on script/runner.

My solution for a recent project was to make a model (non-active-record)
that has class methods for all of the things I want to run command-line.

class Run
def self.cleanup_db

end

def self.send_weekly_emails

end

end

So then I can just do
ruby script/runner “Run.send_weekly_emails”

and everything’s nice and clear.

Ok, kind thanks for everyone who contributed to this thread.

Looks like the most natural solution is cron + script/runner, with
Brain Hogan’s neat “runner-model” as a convenient way to organize
various tasks (especially since there indeed might be more than a
few).

Tom M.'s suggestion of rails_cron sounds interesting, as does
Rake scheduled tasks (especially for me as was planning to close a
knowledge-gap wrt Rake in any case) suggested by Robby Russel.

The requirements sound feasible, in any case, and I hope I would
indeed be able to accomplish the project’s goals with Rails.

Regards,
Alder G.

I did a similar thing, and I’ve been happy with it. I went a little
further
and collected the relevant tasks in helper functions.

For instance, I might want to run Run.cleanup_db and
Run.expire_sessionsfrequently, but
Run.send_weekly_emails and Run.generate_statistics only weekly. I’d
make
helper functions like:

def self.frequent_tasks
cleanup_db
expire_sessions
end

def self.weekly_tasks
send_weekly_emails
generate_statistics
end

This way, when I change the frequent or weekly tasks, I don’t need to
change
my cron jobs. I can just change my source, update the source on the
servers, and I’m good to go. Of course, the same thing could be done
with
shell scripts, but keeping this all in the code makes update really
easy,
and it’s portable.

I’m curious about Rails cron – have people had good results with it?
Better than regular cron or script/runner in a cron job?

The advantage is that the timed event is stored in your application.

I have nothing against cron itself, and there are ways to get many
of the same advantages rails_cron gives via system setup scripts,
etc.

However, using rails_cron should be cross platform and allow you
to express the entire application from within the Rails environment
without relying on external scheduling tools.


– Tom M.