views:

174

answers:

4

Like with browser games. User constructs building, and a timer is set for a specific date/time to finish the construction and spawn the building.

I imagined having something like a deamon, but how would that work? To me it seems that spinning + polling is not the way to go. I looked at async_observer, but is that a good fit for something like this?

A: 

To a certian extent it depends on how much logic is on your front end, and how much is in your model. If you know how much time will elapse before something happens you can keep most of the logic on the front end.

I would use your model to determin the state of things, and on a paticular request you can check to see if it is built or not. I don't see why you would need a background worker for this.

Jeremy French
Well there can be multiple users and many of these timed events at once. I could loop through all previous event in the page requests but this A. gives concurrency problems (multiple instances doing the same things) and B. makes requests slower.
sjmulder
A: 

I would use AJAX to start a timer (see Periodical Executor) for updating your UI. On the model side, just keep track of the created_at column for your building and only allow it to be used if its construction time has elapsed. That way you don't have to take a trip to your db every few seconds to see if your building is done.

Bill
+1  A: 

If you only need the event to be visible to the owning player, then the model can report its updated status on demand and we're done, move along, there's nothing to see here.

If, on the other hand, it needs to be visible to anyone from the time of its scheduled creation, then the problem is a little more interesting.

I'd say you need two things. A queue into which you can put timed events (a database table would do nicely) and a background process, either running continuously or restarted frequently, that pulls events scheduled to occur since the last execution (or those that are imminent, I suppose) and actions them.

Looking at the list of options on the Rails wiki, it appears that there is no One True Solution yet. Let's hope that one of them fits the bill.

Mike Woodhouse
+1  A: 

I just did exactly this thing for a PBBG I'm working on (Big Villain, you can see the work in progress at MadGamesLab.com). Anyway, I went with a commands table where user commands each generated exactly one entry and an events table with one or more entries per command (linking back to the command). A secondary daemon run using script/runner to get it started polls the event table periodically and runs events whose time has passed.

So far it seems to work quite well, unless I see some problem when I throw large number of users at it, I'm not planning to change it.

John Munsch