Zoran Zaric
Zoran Zaric

Reputation: 1221

What is the best way for having a ruby service on rails?

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?

Upvotes: 1

Views: 413

Answers (3)

Zoran Zaric
Zoran Zaric

Reputation: 1221

I'll go with a custom daemon

Upvotes: 0

Gdeglin
Gdeglin

Reputation: 12618

This Stackoverflow thread seems to have a good answer on how to run Ruby as a service on windows: 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!

Upvotes: 2

nitecoder
nitecoder

Reputation: 5486

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

Upvotes: 1

Related Questions