Skip to content
3 changes: 2 additions & 1 deletion server/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var mongoose = require('mongoose');
logger.debug('Starting Catalyst');
logger.debug('Logger Initialized');
var LDAPUser = require('_pr/model/ldap-user/ldap-user.js');
var instanceSync = require('_pr/cronjobs/instance-sync/instanceSync.js');
LDAPUser.getLdapUser(function(err, ldapData) {
if (err) {
logger.error("Failed to get ldap-user: ", err);
Expand Down Expand Up @@ -215,7 +216,7 @@ io.sockets.on('connection', function(socket) {

var cronTabManager = require('_pr/cronjobs');
cronTabManager.start();

instanceSync.executeScheduledInstances();
server.listen(app.get('port'), function() {
logger.debug('Express server listening on port ' + app.get('port'));
});
24 changes: 24 additions & 0 deletions server/app/cronjobs/instance-sync/instanceSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var logger = require('_pr/logger')(module);
var instancesDao = require('_pr/model/classes/instance/instance');
var instanceService = require('_pr/services/instanceService');

var instanceSync = module.exports = {};

instanceSync.executeScheduledInstances = function executeScheduledInstances() {
instancesDao.getScheduledInstances(function(err, instances) {
if (err) {
logger.error("Failed to fetch Instance: ", err);
return;
}
//logger.debug("instances: ", JSON.stringify(instances));
if (instances && instances.length) {
for (var i = 0; i < instances.length; i++) {
(function(i) {
instanceService.executeScheduleJob([instances[i]]);
})(i);
}
}else{
logger.debug("There is no scheduled Instance right now.");
}
});
}
129 changes: 127 additions & 2 deletions server/app/model/classes/instance/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,25 @@ var InstanceSchema = new Schema({
type: String,
required: false,
trim: true
}
},
instanceStart: {
cron: String,
startOn: String,
endOn: String,
repeats: String,
repeatEvery: Number,
cronJobId: String
},
instanceStop: {
cron: String,
stopOn: String,
endOn: String,
repeats: String,
repeatEvery: Number,
cronJobId: String
},
cronEndedOn: String,
isScheduled: Boolean
});

InstanceSchema.plugin(uniqueValidator);
Expand Down Expand Up @@ -2214,6 +2232,113 @@ var InstancesDao = function() {
});
};

this.updateCronJobId = function(instance, jobId, flag, callback) {
Instances.find(instance._id, function(err, anInstance) {
if (err) {
logger.debug("Failed to fetch Instance.", err);
} else {
if (anInstance && anInstance.length) {
if (flag === 'start') {
Instances.update({
"_id": new ObjectId(instance._id),
}, {
$set: {
instanceStart: {
cron: anInstance[0].instanceStart.cron,
startOn: anInstance[0].instanceStart.startOn,
endOn: anInstance[0].instanceStart.endOn,
repeats: anInstance[0].instanceStart.repeats,
repeatEvery: anInstance[0].instanceStart.repeatEvery,
cronJobId: jobId
},
instanceStop: {
cron: anInstance[0].instanceStop.cron,
stopOn: anInstance[0].instanceStop.stopOn,
endOn: anInstance[0].instanceStop.endOn,
repeats: anInstance[0].instanceStop.repeats,
repeatEvery: anInstance[0].instanceStop.repeatEvery,
cronJobId: anInstance[0].instanceStop.cronJobId
},
cronEndedOn: anInstance[0].cronEndedOn
}
}, {
upsert: false
}, function(err, data) {
if (err) {
callback(err, null);
return;
}
callback(null, data);
});
} else {
Instances.update({
"_id": new ObjectId(instance._id),
}, {
$set: {
instanceStop: {
cron: anInstance[0].instanceStop.cron,
stopOn: anInstance[0].instanceStop.stopOn,
endOn: anInstance[0].instanceStop.endOn,
repeats: anInstance[0].instanceStop.repeats,
repeatEvery: anInstance[0].instanceStop.repeatEvery,
cronJobId: jobId
},
instanceStart: {
cron: anInstance[0].instanceStart.cron,
startOn: anInstance[0].instanceStart.startOn,
endOn: anInstance[0].instanceStart.endOn,
repeats: anInstance[0].instanceStart.repeats,
repeatEvery: anInstance[0].instanceStart.repeatEvery,
cronJobId: anInstance[0].instanceStart.cronJobId
},
cronEndedOn: anInstance[0].cronEndedOn
}
}, {
upsert: false
}, function(err, data) {
if (err) {
callback(err, null);
return;
}
callback(null, data);
});
}
} else {
logger.debug("No Instance Found.");
}
}
});
};

this.getScheduledInstances = function(callback) {
Instances.find({ isScheduled: true, instanceState: { $ne: 'terminated' } }, function(err, instances) {
if (err) {
return callback(err, null);
}
callback(null, instances);
})
}

this.updateScheduler = function(instanceId, scheduler, isScheduled, callback) {
Instances.update({
"_id": ObjectId(instanceId)
}, {
$set: {
instanceStart: scheduler.instanceStart,
instanceStop: scheduler.instanceStop,
cronEndedOn: scheduler.cronEndedOn,
isScheduled: isScheduled
}
}, function(err, data) {
if (err) {
logger.error("Failed to update managed Instance status data", err);
callback(err, null);
return;
}
callback(null, data);
});
};

this.getInstancesByTagServer = function(tagServer, callback) {
Instances.find({
"tagServer": tagServer
Expand All @@ -2227,4 +2352,4 @@ var InstancesDao = function() {
};
};

module.exports = new InstancesDao();
module.exports = new InstancesDao();
Loading