Forum: Ruby on Rails Running Rails tasks by schedule, instead of an HTTP request.

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.
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-03-24 09:20
(Received via mailing list)
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 Green
Re:
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-03-24 09:55
(Received via mailing list)
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 Green wrote:
>to be initiated by a scheduler, without any prior client HTTP request.
>
>http://lists.rubyonrails.org/mailman/listinfo/rails
Mikkel Bruun

www.strongside.dk    - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk      - Buy Old Stuff!(DK)
9ae3851d112c52883f2cc88056e9de44?d=identicon&s=25 Larkin Cunningham (Guest)
on 2006-03-24 10:11
(Received via mailing list)
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
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-03-24 11:58
(Received via mailing list)
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 Bruun
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 :)

Cheers,
Alder
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-03-24 12:36
(Received via mailing list)
On Mar 24, 2006, at 2:58 AM, Alder Green 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 Bruun
> 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 :)

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! :-)

--
-- Tom Mornini
B84d42a3a5c343f8fc6ab7d7f47fd3f5?d=identicon&s=25 Robby Russell (Guest)
on 2006-03-24 15:04
(Received via mailing list)
On Fri, 2006-03-24 at 12:58 +0200, Alder Green 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 Bruun
> 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 :)

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


-Robby

--
/**************************************************************
* Robby Russell, 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 *
***************************************************************/
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2006-03-24 16:41
(Received via mailing list)
+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.
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-03-26 19:16
(Received via mailing list)
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 Mornini'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 Green
3ec705c5dd3480c6268b72c5617e8dae?d=identicon&s=25 Michael Smedberg (Guest)
on 2006-03-26 21:57
(Received via mailing list)
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.
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2006-03-26 23:44
(Received via mailing list)
I'm curious about Rails cron -- have people had good results with it?
Better than regular cron or script/runner in a cron job?
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-03-27 05:56
(Received via mailing list)
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 Mornini
This topic is locked and can not be replied to.