Tuesday, 2 May 2017

How to run multiple bots on one firebase database?

database = firebase.database();

var ref = database.ref('Users');

ref.orderByChild('state').equalTo("0").once('value', function(snapshot) {
  var Users = snapshot.val();
  i=0;
  if (Object.keys(Users).length > 0){
    getUser(Users);
  } else {
    console.log("No Users")
  }
});

What I am doing is having a node js bot run through my database and search for users with state= 0. If state equals to zero, I run another script that goes and gets some information about them, updates their entry, and then changes the state to 1.

I have quite a large database, so it would be great if I could run a few instances of my bot. It won't work, though, because when the bots initially run, they all look at the same entries and remember which ones have a state = 0 and then they all repeat each other's work.

I tried changing the ref.orderByChild from using "once" to "on" child changed. That didn't seem to work though because it seemed as though the script was always waiting/listening for changes.. and never actually finished one loop. It does not move on to the next entry.

What's the best way to tackle something like this: having multiple bots being able to edit a firebase database without repeating each other's work?



via ryangineer

No comments:

Post a Comment