I have a couple of collections that I want to be cleared every time I start up my app. The problem is, since i'm using a PaaS (http://modulus.io), instances could start and stop quite often. If I do clients.remove({})
in my startup, the entire collection will be cleared even if one instance dies and is restarted.
What is the best way to have an ephemeral collection that is cleared only ONCE when the app first starts up?
This is not a question specific to Meteor
or Modulus
or Mongo
but could apply to any Node app in this situation.
I suspect you'll need to reframe your problem/goal. If you are using a PaaS and your app is multi-instace, and it's a long-running service, the concept of "every time I start up my app" is largely irrelevant. I think you will getter better answers if you describe the actual functional use case you have vs. the tactics you are trying to code. Perhaps a periodic job is more appropriate? Can you just have each instance record a timestamp when it starts and always filter out clients with {createdAt: {$lt: instanceStartTime}}
?
Otherwise you'll need to correctly implement a mutex at the database level to give one instance a write lock so that instance can go clients.drop()
, then release the lock. But to correctly code that requires a careful state machine plus some timeout logic in case an instance locks the collection for dropping but crashes before releasing the lock.