3

I want to start my Rails server in a background thread from within a Ruby script. I could use Kernel#system but I want to be able to kill the Rails server when the thread is stopped. Is there a way to execute the Rails server using some Rails API call instead? I'm thinking something it would be nice to be able to put something like Rails.run_server(:port => 3000, ...)

I'm on Windows Server 2008.

Ben
  • 1,321
  • 15
  • 30

2 Answers2

1

Check out the file gems/rails.x.x.x/lib/commands/server.rb. It looks like that's the starting point that script/server uses.

Since script/server is itself a ruby script, it stands to reason that you ought to be able to start a server by doing something similar to what's in server.rb. But I imagine you might have some difficulty getting your ruby environment right...

Note that I'm looking at rails 2.3.8 here, so if you're on 3.whatever your results will probably be different.

mdunsmuir
  • 41
  • 2
  • Thanks, I'll take a deeper look at this. My cursory glance suggests that it delegates through almost immediately to `::Rack::Server`. (I'm using Rails 3.0.6) – Ben Nov 16 '11 at 09:13
  • Yep, this is a bit nasty. I've tried `require`-ing the Rails file `commands/server.rb` but you get all sorts of errors. Once I'd set the `RAILS_ROOT` and `APP_PATH` I still get `ArgumentError`s in `actionpack`. I can't help feeling somebody must already have a gem or other solution for this. Nothing from Google though. – Ben Nov 20 '11 at 22:03
  • Yeah, I actually don't work with Rails a lot (I have a few cheesy little apps under my belt but nothing more) but I'm very familiar with ruby and I'm always struck by how extensively rails changes the standard environment. Honestly, I'm not a fan. But if you don't mind my asking, what's the reason for wanting to do this? I'm not sure I can imagine a case where this would be the best solution. – mdunsmuir Nov 22 '11 at 05:33
  • I'm writing a Windows service that starts my Rails server and then executes Rake tasks on a schedule. Having it all packaged up in one place (the service) means it's all or nothing, which is attractive to me. I think I've managed to work around it anyway - I'll post an answer soon. – Ben Nov 22 '11 at 21:37
1

I eventually decided to avoid any ickiness and start the rails server in its own process, as detailed in this post. (Being able to kill it plus its child processes consistently was the main blocker and the original reason I'd considered starting it in a thread instead.)

Community
  • 1
  • 1
Ben
  • 1,321
  • 15
  • 30