- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
SAN-4866 Cleanup Old Instances #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6b638e8
              4976f7f
              b079bdc
              8ea0795
              b4bbec4
              7517a84
              a9064cc
              cb1745f
              a3ff89e
              11b92dc
              bbb65bf
              944dbfe
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 'use strict' | ||
|  | ||
| // external | ||
| const Promise = require('bluebird') | ||
| const rabbitmq = require('models/rabbitmq') | ||
| const mongodbHelper = require('tasks/utils/mongodb') | ||
| const moment = require('moment') | ||
|  | ||
| // internal | ||
| const logger = require('logger').getChild(__filename) | ||
|  | ||
| module.exports = CleanupInstances | ||
|  | ||
| function CleanupInstances () { | ||
| const cleanupDate = moment().subtract(7, 'days').toDate() | ||
| var log = logger.child({ | ||
| cleanupCutoff: cleanupDate | ||
| }) | ||
| log.info('CleanupInstances') | ||
|  | ||
| return Promise.using(mongodbHelper(), function (mongoClient) { | ||
| return mongoClient.fetchInstancesAsync({ | ||
| masterPod: false, | ||
| 'contextVersion.created': { $lt: cleanupDate }, | ||
| $or: [ | ||
| { isolated: { $exists: false } }, | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isolated exists only for children right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isolated also exists for  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will never match then right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, it's an OR, so if it's the isolation group master, or it's not isolated at all. | ||
| { isIsolationGroupMaster: true } | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does deleting isolation master delete the kids too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. | ||
| ] | ||
| }) | ||
| }) | ||
| .then(function (instances) { | ||
| log.info({ | ||
| instanceCount: instances.length, | ||
| instanceIds: instances.map(function (instance) { | ||
| return instance._id | ||
| }) | ||
| }, 'Found instances to cleanup') | ||
| instances.forEach(function (instance) { | ||
| rabbitmq.publishEvent('instance.expired', { | ||
| instanceId: instance._id | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| 'use strict' | ||
|  | ||
| require('loadenv')({ debugName: 'khronos:test' }) | ||
|  | ||
| var chai = require('chai') | ||
| var assert = chai.assert | ||
| chai.use(require('chai-as-promised')) | ||
|  | ||
| // external | ||
| var rabbitmq = require('models/rabbitmq') | ||
| var sinon = require('sinon') | ||
| require('sinon-as-promised')(require('bluebird')) | ||
|  | ||
| // Internal | ||
| const MongoDB = require('models/mongodb') | ||
|  | ||
| // internal (being tested) | ||
| var CleanupInstances = require('tasks/instances/cleanup') | ||
|  | ||
| describe('khronos:instances:cleanup', function () { | ||
| var mockInstances | ||
|  | ||
| beforeEach(function () { | ||
| mockInstances = [ | ||
| { | ||
| _id: '1234' | ||
| }, | ||
| { | ||
| _id: '5678' | ||
| } | ||
| ] | ||
| sinon.stub(MongoDB.prototype, 'close').yieldsAsync() | ||
| sinon.stub(MongoDB.prototype, 'connect').yieldsAsync() | ||
| sinon.stub(MongoDB.prototype, 'fetchInstances').yieldsAsync(null, mockInstances) | ||
| sinon.stub(rabbitmq, 'publishEvent').resolves() | ||
| }) | ||
|  | ||
| afterEach(function () { | ||
| MongoDB.prototype.close.restore() | ||
| MongoDB.prototype.connect.restore() | ||
| MongoDB.prototype.fetchInstances.restore() | ||
| rabbitmq.publishEvent.restore() | ||
| }) | ||
|  | ||
| describe('when there are instances to cleanup', function () { | ||
| it('should fetch instances with the propery query parameters', function (done) { | ||
| return assert.isFulfilled(CleanupInstances({})) | ||
| .then(function () { | ||
| sinon.assert.calledOnce(MongoDB.prototype.fetchInstances) | ||
| sinon.assert.calledWith( | ||
| MongoDB.prototype.fetchInstances, | ||
| { | ||
| masterPod: false, | ||
| 'contextVersion.created': { $lt: sinon.match.date }, | ||
| $or: [ | ||
| { isolated: { $exists: false } }, | ||
| { isIsolationGroupMaster: true } | ||
| ] | ||
| } | ||
| ) | ||
| }) | ||
| .asCallback(done) | ||
| }) | ||
|  | ||
| it('should cleanup the old instances', function (done) { | ||
| return assert.isFulfilled(CleanupInstances({})) | ||
| .then(function () { | ||
| sinon.assert.calledTwice(rabbitmq.publishEvent) | ||
| sinon.assert.calledWith( | ||
| rabbitmq.publishEvent, | ||
| 'instance.expired', | ||
| { | ||
| instanceId: '1234' | ||
| } | ||
| ) | ||
| sinon.assert.calledWith( | ||
| rabbitmq.publishEvent, | ||
| 'instance.expired', | ||
| { | ||
| instanceId: '5678' | ||
| } | ||
| ) | ||
| }) | ||
| .asCallback(done) | ||
| }) | ||
| }) | ||
| }) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To whomever reviews this. PLEASE PLEASE PLEASE make sure you think through this query properly. I don't wanna delete the wrong things on prod.