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.

10
  • Why are you using a runner for this? What was your thinking there? Commented Apr 4, 2012 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' Commented Apr 4, 2012 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: faye.jcoglan.com Commented Apr 4, 2012 at 16:01
  • I use gem 'daemons', which every second looks for games, that are needed to process Commented Apr 4, 2012 at 16:07
  • 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. stackoverflow.com/a/9911695/366051 Commented Apr 4, 2012 at 20:33

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

I opened another post related to this, in case you are interested in reading: stackoverflow.com/questions/10045693/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.