1
  • User goes to page A to create a new multiplayer game
  • The script in page A generates a unique ID for the game, and creates a worker for it. Something like: rails runner GameWorker.new(:game_id => game_id).start_game
  • The script in page A redirects the user to page B, where he can see the newly created game, and others can join.

The worker should be alive until the end of the game.

What would be the proper way to run the command that starts the worker? It must be non blocking and ideally redirect output to the log file, in case something goes wrong.

I'm using Rails 3, if it matters.

UPDATE I'm gonna rephrase my question: How to run a linux command from within ruby and don't wait for the command to end? I mean the equivalent for &>>. In php for instance, &>> works fine and I don't need to use any special php functiont, but in ruby it seems to get overriden by and the script waits for the command to end and grab the output.

HappyDeveloper
  • 12,480
  • 22
  • 82
  • 117
  • Why are you using a runner for this? What was your thinking there? – Jesse Wolgamott Apr 04 '12 at 15:39
  • @JesseWolgamott I use runner to create and run the worker. It is like a cronjob: it is not part of the request/response, it has its own 'life' – HappyDeveloper Apr 04 '12 at 15:51
  • 1
    It sounds to me that you need a subscribe/publish service as supposed to worker. That is if you want to communicate with multiple users in real time. Check out: http://faye.jcoglan.com/ – agmcleod Apr 04 '12 at 16:01
  • I use gem 'daemons', which every second looks for games, that are needed to process – mikdiet Apr 04 '12 at 16:07
  • @agmcleod thanks for the link, I didn't know Faye, I was using Juggernaut. But that's not the problem, I need to run a separate process for the game. – HappyDeveloper Apr 04 '12 at 16:17
  • @Mik_Die I need to start new processes on demand. I can't have a daemon to handle all the games, I need one for each game. – HappyDeveloper Apr 04 '12 at 16:42
  • @HappyDeveloper Is your game turn-based, or does it require 1 running process per game? The turn based is easier and more prevalent. A web based 1 process-per-game is extremely rare. – Jesse Wolgamott Apr 04 '12 at 18:38
  • 1
    My answer here might help you. Don't pay attention to the fact it's using Sinatra, just the action of launching a process and storing its PID. http://stackoverflow.com/a/9911695/366051 – Paul Hoffer Apr 04 '12 at 20:33
  • @JesseWolgamott No it's not turn based, it's real time, like in a chat, where people is throwing text and events when they want. – HappyDeveloper Apr 04 '12 at 20:37
  • @phoffer Thanks! That might be what I'm looking for. Will try it later – HappyDeveloper Apr 04 '12 at 20:38

1 Answers1

3

I HIGHLY recommend not running a process per game. If you want a non-blocking game that is not turn based, then you probably want to look at event-machine, or something like https://github.com/celluloid/celluloid-io

With either, you'll be creating threads that you'll process at future points in time.

But -- if you do want to just fire off a process in ruby, here you go.. from How to fire and forget a subprocess?

pid = Process.fork
if pid.nil? then
  # In child
  exec "whatever --take-very-long"
else
  # In parent
  Process.detach(pid)
end
Community
  • 1
  • 1
Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • I opened another post related to this, in case you are interested in reading: http://stackoverflow.com/questions/10045693/how-to-communicate-with-threads-in-ruby – HappyDeveloper Apr 06 '12 at 15:39