-
Notifications
You must be signed in to change notification settings - Fork 5
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
Jmrunge/schedules #48
Changes from all commits
c42a5c9
a28195d
90d3ccf
8a04eee
270597a
18db450
58c3ead
f7ae12f
f516129
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,155 @@ | ||
var moment = require('moment') | ||
, _ = require('underscore') | ||
, mbc = require('mbc-common') | ||
, collections = mbc.config.Common.Collections | ||
, logger = mbc.logger().addLogger('webvfx_scheduler') | ||
, conf = mbc.config.Webvfx | ||
, Sketch = require('mbc-common/models/Sketch') | ||
; | ||
|
||
function scheduler() { | ||
logger.debug("New instance created"); | ||
} | ||
|
||
scheduler.prototype.initScheduler = function() { | ||
logger.info("Starting Scheduler"); | ||
var proceed = _.after(3, function() { | ||
self.loadSchedules(self.scheds); | ||
}); | ||
this.scheds = new Sketch.ScheduleCollection(); | ||
this.scheds.bindBackend(); | ||
var self = this; | ||
this.scheds.fetch({success: function() { | ||
self.scheds.on({"add": function(sched, col, opts) { | ||
logger.debug("ADD received", sched.toJSON()); | ||
if (!opts.ignore) | ||
self.processSched.bind(self)(sched); | ||
}, | ||
"remove": function(sched, col, opts) { | ||
logger.debug("REMOVE received", sched.toJSON()); | ||
if (!opts.ignore) | ||
self.removeSched.bind(self)(sched); | ||
} | ||
}); | ||
proceed(); | ||
}}); | ||
this.sketchs = new Sketch.Collection(); | ||
this.sketchs.bindBackend(); | ||
this.sketchs.fetch({success: proceed}); | ||
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. Also fetching entire sketchs from db 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 would be solved by using bb relational dont it? 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. Yes, because will fetch the one you need. 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. Same as previous comment, I think is perhaps better to add BB relational on a second PR, to keep inaes-tic/mbc-common#66 smaller. 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. Ok, add it to your task list ;) 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. Done! #52 |
||
this.live = new Sketch.LiveCollection(); | ||
this.live.bindBackend(); | ||
this.live.fetch({success: proceed}); | ||
}; | ||
|
||
scheduler.prototype.loadSchedules = function(col) { | ||
logger.info("Loading Schedules"); | ||
logger.debug("Schedules", col.toJSON()); | ||
var self = this; | ||
col.forEach(function(sched) { | ||
self._processSched(sched); | ||
}); | ||
}; | ||
|
||
scheduler.prototype.processSched = function(sched) { | ||
var self = this; | ||
this.sketchs.fetch({success: function() { | ||
self._processSched.bind(self)(sched); | ||
}}); | ||
}; | ||
|
||
scheduler.prototype._processSched = function(sched) { | ||
logger.info("Processing sched", sched.toJSON()); | ||
var self = this; | ||
var now = moment().valueOf(); | ||
var start = sched.get("date"); | ||
var end = false; | ||
if (sched.get("length")) | ||
end = start + sched.get("length"); | ||
if (!end || (end > now)) | ||
self.addSched.bind(self)(sched, start, end, now); | ||
else | ||
self.removeSched.bind(self)(sched); | ||
}; | ||
|
||
scheduler.prototype.removeSched = function(sched) { | ||
logger.info("Removing sched", sched.toJSON()); | ||
if (sched.get("startTimeout")) | ||
cancelTimeout(sched.get("startTimeout")); | ||
if (sched.get("endTimeout")) | ||
cancelTimeout(sched.get("endTimeout")); | ||
this.unloadSketch(sched.get("sketch_id")); | ||
this.scheds.remove(sched.get("id")); | ||
sched.destroy({ignore: true}); | ||
}; | ||
|
||
scheduler.prototype.addSched = function(sched, start, end, now) { | ||
logger.debug("Adding sched", start, end, now, sched.toJSON()); | ||
var self = this; | ||
if (now === undefined) | ||
now = moment.valueOf(); | ||
if (start === undefined) | ||
start = sched.get("date"); | ||
if (end === undefined) { | ||
end = false; | ||
if (sched.get("length")) | ||
end = start + sched.get("length"); | ||
} | ||
|
||
if (start > now) | ||
sched.set("startTimeout", self.getTimeout(sched.get("sketch_id"), true, start - now)); | ||
else | ||
self.loadSketch(sched.get("sketch_id")); | ||
|
||
if (end) { | ||
if (end > now) | ||
sched.set("endTimeout", self.getTimeout(sched.get("sketch_id"), false, end - now)); | ||
else | ||
self.unloadSketch(sched.get("sketch_id")); | ||
} | ||
}; | ||
|
||
scheduler.prototype.getTimeout = function(sketch_id, load, timeout) { | ||
logger.info("Setting timeout for " + (load ? 'load' : "unload") + " in " + timeout + " millis"); | ||
var self = this; | ||
return setTimeout(function() { | ||
if (load) | ||
self.loadSketch(sketch_id); | ||
else | ||
self.unloadSketch(sketch_id); | ||
}, timeout); | ||
}; | ||
|
||
scheduler.prototype.loadSketch = function(id) { | ||
var self = this; | ||
var sketch = this.sketchs.get(id); | ||
if (sketch) { | ||
logger.info("Loading sketch", sketch.attributes.name); | ||
_.each(sketch.attributes.data, function(data) { | ||
data.origin = 'server'; | ||
var new_model = new Sketch.Live(data); | ||
self.live.add(new_model); | ||
new_model.save(); | ||
}); | ||
logger.debug("Sketch " + sketch.attributes.name + " loaded"); | ||
} | ||
}; | ||
|
||
scheduler.prototype.unloadSketch = function(id) { | ||
var self = this; | ||
var sketch = this.sketchs.get(id); | ||
if (sketch) { | ||
logger.info("Unloading sketch", sketch.attributes.name); | ||
self.live.fetch({success: function() { | ||
_.each(sketch.attributes.data, function(data) { | ||
var model = self.live.findWhere({element_id: data.element_id}); | ||
model.destroy(); | ||
logger.info('Sketch unloaded', sketch.attributes.name); | ||
}); | ||
}}); | ||
logger.debug("Sketch " + sketch.attributes.name + " unloaded"); | ||
} | ||
}; | ||
|
||
exports = module.exports = scheduler; | ||
|
||
scheduler.__proto__ = new scheduler; |
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.
Are we fetching all scheds from DB?
You have to use custom mongo queries over bb collections like in playout view.
Collection must use searchWrapper.
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.
Cool! Will do it and let you know.
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.
Right now is necessary to fetch all schedules because we dont have a tick on scheduler to refetch like mosto does. I was planning to do it on a next PR to keep this one smaller. Is this ok with you or should I add this feature now?
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.
Ok but add an issue for this feature, so we can remember it later.
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.
Done! #53