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.
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 V. [email protected] wrote:
Alternatively, just create a file that requires config/boot (I think
On 6/28/06, DJ Tequila [email protected] wrote:
http://lists.rubyonrails.org/mailman/listinfo/rails
–
Tom D.
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 M.
On 6/27/06, DJ Tequila [email protected] 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
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 [email protected] wrote:
Ben
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
–