Skip to content
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

Merged
merged 9 commits into from
Mar 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion backends.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ module.exports = function (db) {
},
live: {
use: [backboneio.middleware.memoryStore()]
}
},
sketchschedule: {
use: [middleware.uuid],
mongo: {
db: db,
collection: collections.SketchSchedules,
}
},
}
return backends;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"backbone-pageable" : "1.3.2",
"socket.io" : "0.9.x",
"knockback" : "0.17.x",
"knockout" : "2.2.x"
"knockout" : "2.2.x",
"moment" : "1.7.x"
},
"license" : "agplv3",
"engines" : {
Expand Down
2 changes: 2 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ module.exports = function(app) {
var vendorJs = new folio.Glossary(commonVendor.concat([
require.resolve('jqueryui-browser/ui/jquery-ui.js'),
require.resolve('node-uuid'),
require.resolve('moment'),
require.resolve('jed'),
path.join(common_lib_dir, 'kinetic-v4.5.2.min.js'),
path.join(common_lib_dir, 'backbone.modal-min.js'),
Expand Down Expand Up @@ -289,6 +290,7 @@ module.exports = function(app) {
'alert',
'confirm',
'prompt',
'schedule_prompt'
];

var getFileName = function (e) {
Expand Down
155 changes: 155 additions & 0 deletions scheduler.js
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);
}
});
Copy link
Member

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.

this.collection.fetch({
    success: function() {
    },
    error: function(e) {
    },
    data: {
        query: {criteria: {in_window: [start.valueOf(), end.valueOf()]}},
    },
});

Copy link
Member Author

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.

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! #53

proceed();
}});
this.sketchs = new Sketch.Collection();
this.sketchs.bindBackend();
this.sketchs.fetch({success: proceed});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also fetching entire sketchs from db

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be solved by using bb relational dont it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because will fetch the one you need.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, add it to your task list ;)

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
5 changes: 5 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var _ = require('underscore'),
iobackends = new mbc.iobackends(db, backends)
;

iobackends.patchBackbone();

var loggerStream = {
write: function(message, encoding) {
logger.info(message);
Expand Down Expand Up @@ -109,3 +111,6 @@ if (process.env.HEROKU) {
}

io.set('logger', logger); // Log socket.io with custom logger

var scheduler = require('./scheduler')
scheduler.initScheduler();