views:

147

answers:

3

I need to run a ruby-script as a service. The script needs access to the ActiveRecords of a rails-app.

What would be the best way? A rake task? How can it be started as a service on both windows and linux?

+1  A: 

I would say maybe a Sinatra app might be the way to go if it's just one script as a service.

railsninja
+2  A: 

This Stackoverflow thread seems to have a good answer on how to run Ruby as a service on windows: http://stackoverflow.com/questions/163497/running-a-ruby-program-as-a-windows-service

And here is how to instantiate ActiveRecord outside of rails: http://www.juixe.com/techknow/index.php/2009/01/14/activerecord-ruby-on-rails-optional/

If you want to use the same models as your Rails application, you can require them.

Here's an example in console:

irb(main):001:0> require 'ActiveRecord'
=> true
irb(main):002:0>     ActiveRecord::Base.establish_connection(
irb(main):003:1*       :adapter => 'mysql',
irb(main):004:1*       :database => 'development',
irb(main):005:1*       :username => 'root',
irb(main):006:1*       :password => '',
irb(main):007:1*       :host => 'localhost'
irb(main):008:1>     )
=> #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x59613
irb(main):009:0> require 'app/models/User.rb'
=> ["User"]
irb(main):010:0> User.find(1)
=> #<User id: 1, first_name: "Michael">

Good luck!

Gdeglin
A: 

I'll go with a custom daemon

Zoran Zaric