Server side reactivity in Methods?


#1

I’ve created something I don’t fully understand :smile:

My questions are essentially “what is and is-not reactive on the server?”.
I read the docs, but didn’t full grok them re: methods, timeouts, and collections.

I have two Meteor Micro-services. They are different meteor run processes, and share the same MongoDB oplog.

The “add app” adds an item to a work queue collection. The “resolve app” let’s multiple people resolve them. The collection is published to both clients.

"add app"     -[adds item]----> work queue collection
                                      [oplog]
"resolve app" -[updates item]-> work queue collection

In “resolve app”, different users call a Meteor “resolve” method to respond yes/no/maybe to an item. The first “yes” get’s it. After a timeout, the first “maybe” get’s it.

The UI for this is working beautifully for one person on my laptop. :sweat_smile:

The 2 processes communicate using MongoDB oplog. The “resolve” method’s update checks the item’s status field so the updates don’t clobber:

var affected = queue.update(
  {_id: item._id, status: item.status}, 
  {status:newStatus}
);
if (!affected) throw new Meteor.Error('Someone else got there first.  Try again later!');

If the update affects 0 rows, then throw an error and the client can try again after waiting for new data.

This also appears to be working well for one person on a laptop. :sweat_smile:

This approach doesn’t seem “reactive”, although it calls reactive things:

  1. The Meteor method queue.findOne()s the item.
  2. The method sets a Meteor.setTimeout() to handle the maybes
  3. The method/timeout findOne() and find().count() other collections.

My questions are this:

  1. In the method and timeout, am I leaking reactive calculations that I’m not aware of?
  2. This code is designed to run once for each method call. If the collection updates, before the method returns, will it run an extra time?
  3. Once the method and timeout return, is there anything leftover?
  4. If affected is 0, it’s a panic. Memory is out of sync with the db and I throw an Error. Is anything left over after that?
  5. What is reactive on the server?
  6. How does this affect the “mergebox”?
  7. Is there an easier way?
  8. Am I shooting myself in the foot?

Sorry for the vague questions. I think it’s workable, but wanted to ask before having 1000 angry users.

Thanks!

Mike