I want to be able to write a script in Ruby which regularly consumes an XML feed from a fixed source, re-formats it, produces a large file for web delivery, and updates a database with the contained information. It''s simple - just running a script from a cron job, and some basic data manipulation... But I''d prefer to use the Rails database mapping constructs than roll my own. What steps do I need to take to include ActiveRecord and my database models in a Ruby script? Ben
One very easy way is write your script as a rake task and put it in lib/tasks/application.rake task :update_the_universe => :environment do # Put anything in here end Then just add a cronjob that executes ''RAILS_ENV=production rake update_the_universe''. Alternatively, just create a file that requires config/boot (I think that''s right ... ). Something like: #!/usr/bin/ruby1.8 require ''config/boot'' # Rest of code here I''ve used the first solution, but not the second. -Jonathan. On 6/28/06, DJ Tequila <mail@djtequila.com> wrote:> I want to be able to write a script in Ruby which regularly consumes an > XML feed from a fixed source, re-formats it, produces a large file for > web delivery, and updates a database with the contained information. > > It''s simple - just running a script from a cron job, and some basic data > manipulation... But I''d prefer to use the Rails database mapping > constructs than roll my own. > > What steps do I need to take to include ActiveRecord and my database > models in a Ruby script? > > Ben > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
To include railsy things you can always just require the rails gem. But if you already have something on your RoR website that does this you can just use script/runner. http://wiki.rubyonrails.org/rails/pages/RunnerScript http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails On 6/27/06, DJ Tequila <mail@djtequila.com> wrote:> I want to be able to write a script in Ruby which regularly consumes an > XML feed from a fixed source, re-formats it, produces a large file for > web delivery, and updates a database with the contained information. > > It''s simple - just running a script from a cron job, and some basic data > manipulation... But I''d prefer to use the Rails database mapping > constructs than roll my own. > > What steps do I need to take to include ActiveRecord and my database > models in a Ruby script? > > Ben > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -------------- Jon Gretar Borgthorsson http://www.jongretar.net/
I wrote up a brief entry on this on my blog that you may find useful: http://atomgiant.com/articles/2006/04/18/batch-processing-with-rails Tom On 6/27/06, Jonathan Viney <jonathan.viney@gmail.com> wrote:> One very easy way is write your script as a rake task and put it in > lib/tasks/application.rake > > task :update_the_universe => :environment do > # Put anything in here > end > > Then just add a cronjob that executes ''RAILS_ENV=production rake > update_the_universe''. > > Alternatively, just create a file that requires config/boot (I think > that''s right ... ). Something like: > > #!/usr/bin/ruby1.8 > require ''config/boot'' > # Rest of code here > > I''ve used the first solution, but not the second. > > -Jonathan. > > On 6/28/06, DJ Tequila <mail@djtequila.com> wrote: > > I want to be able to write a script in Ruby which regularly consumes an > > XML feed from a fixed source, re-formats it, produces a large file for > > web delivery, and updates a database with the contained information. > > > > It''s simple - just running a script from a cron job, and some basic data > > manipulation... But I''d prefer to use the Rails database mapping > > constructs than roll my own. > > > > What steps do I need to take to include ActiveRecord and my database > > models in a Ruby script? > > > > Ben > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tom Davies http://atomgiant.com http://gifthat.com
Write a class method on a new class and put it in lib/
lib/maintenance.rb:
class Maintenance
def self.run
# Do your rails stuff here, using and relying
# upon the Rails environment.
end
end
then, in your crontab, call this:
/path/to/app/script/runner -e production ''Maintenance.run''
You may also want to check out the very cool rails_cron
plugin:
www.kylemaxwell.com
He has a temporaryarily down page up right now, but I''m
guessing he''ll have that fixed right away.
--
-- Tom Mornini
On Jun 27, 2006, at 5:21 AM, DJ Tequila wrote:
> I want to be able to write a script in Ruby which regularly
> consumes an XML feed from a fixed source, re-formats it, produces a
> large file for web delivery, and updates a database with the
> contained information.
>
> It''s simple - just running a script from a cron job, and some
basic
> data manipulation... But I''d prefer to use the Rails database
> mapping constructs than roll my own.
>
> What steps do I need to take to include ActiveRecord and my
> database models in a Ruby script?
>
> Ben
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
On 6/27/06, DJ Tequila <mail@djtequila.com> wrote:> I want to be able to write a script in Ruby which regularly consumes an > XML feed from a fixed source, re-formats it, produces a large file for > web delivery, and updates a database with the contained information. > > It''s simple - just running a script from a cron job, and some basic data > manipulation... But I''d prefer to use the Rails database mapping > constructs than roll my own. > > What steps do I need to take to include ActiveRecord and my database > models in a Ruby script?Checkout http://svn.kylemaxwell.com/rails_cron/trunk/README. Joe