1

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?

Mike Woodhouse
  • 51,832
  • 12
  • 88
  • 127
Zoran Zaric
  • 1,211
  • 2
  • 11
  • 19

3 Answers3

2

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!

Community
  • 1
  • 1
Gdeglin
  • 12,432
  • 5
  • 49
  • 65
1

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

nitecoder
  • 5,496
  • 1
  • 28
  • 35
0

I'll go with a custom daemon

Zoran Zaric
  • 1,211
  • 2
  • 11
  • 19