Skip to content
Browse files

have each service check to make sure related tasks have been created

  • Loading branch information...
1 parent 7acf5fa commit 3fc46f97d2739b57375ce79dccc41bea938c548f @brianshaler brianshaler committed
Showing with 148 additions and 74 deletions.
  1. +1 −0 controllers/AdminController.js
  2. +73 −37 controllers/FoursquareController.js
  3. +74 −37 controllers/TwitterController.js
View
1 controllers/AdminController.js
@@ -54,6 +54,7 @@ exports.controller = function(req, res, next) {
var tasks = [
{controller: "TwitterController", method: "timeline", interval: 60},
{controller: "TwitterController", method: "stream", interval: 10, attributes: {connected: false}},
+ {controller: "FoursquareController", method: "timeline", interval: 120},
{controller: "AnalysisController", method: "analyze", interval: 8}
];
tasks.forEach(function (t) {
View
110 controllers/FoursquareController.js
@@ -19,6 +19,10 @@ exports.controller = function(req, res, next) {
self.platform = "foursquare";
+ self.tasks = [
+ {controller: "FoursquareController", method: "timeline", interval: 120}
+ ];
+
self.setup = function () {
if (!req.require_authentication()) { return; }
@@ -34,52 +38,84 @@ exports.controller = function(req, res, next) {
step = steps[0];
}
- self._get_settings(function (err, fsq) {
- if (err) throw err;
-
- if (step == "connect") { // Step 2: /foursquare/setup/connect
- var is_setup = false;
- if (fsq && fsq.value && fsq.value.access_token) {
- is_setup = true;
- }
- // Show the page for this step
- return self.render('admin/setup/foursquare/connect', {
- layout: "admin/admin-layout",
- locals: {
- title: 'Connect to Foursquare',
- settings: {},
- is_setup: is_setup,
- current_step: step
+ function check_tasks (cb) {
+ if (self.tasks.length == 0) {
+ cb(); return;
+ }
+ var ors = [];
+ self.tasks.forEach(function (task) {
+ ors.push({controller: task.controller, method: task.method});
+ });
+ var where = {"$or": ors};
+ Task.find(where, function (err, existing) {
+ if (err) throw err;
+
+ self.tasks.forEach(function (t) {
+ var found = false;
+ existing.forEach(function (et) {
+ if (t.controller == et.controller && t.method == et.method) {
+ found = true;
+ }
+ });
+ if (!found) {
+ var task = new Task(t);
+ task.save(function (err) {
+ if (err) throw err;
+ });
}
});
- } else { // Default is Step 1: /foursquare/setup/app
- if (req.body && req.body.settings && req.body.settings.foursquare) {
- // Process form
- if (!fsq.value) {
- fsq.value = {};
- }
- for (var k in req.body.settings.foursquare) {
- fsq.value[k] = req.body.settings.foursquare[k];
+ cb();
+ });
+ }
+
+ check_tasks(function () {
+ self._get_settings(function (err, fsq) {
+ if (err) throw err;
+
+ if (step == "connect") { // Step 2: /foursquare/setup/connect
+ var is_setup = false;
+ if (fsq && fsq.value && fsq.value.access_token) {
+ is_setup = true;
}
- fsq.commit('value');
- fsq.save(function (err) {
- if (err) throw err;
-
- // Done with this step. Continue!
- res.redirect("/foursquare/setup/connect");
- });
- return;
- } else {
// Show the page for this step
- return self.render('admin/setup/foursquare/app', {
+ return self.render('admin/setup/foursquare/connect', {
layout: "admin/admin-layout",
locals: {
- title: 'Foursquare',
- settings: fsq.value
+ title: 'Connect to Foursquare',
+ settings: {},
+ is_setup: is_setup,
+ current_step: step
}
});
+ } else { // Default is Step 1: /foursquare/setup/app
+ if (req.body && req.body.settings && req.body.settings.foursquare) {
+ // Process form
+ if (!fsq.value) {
+ fsq.value = {};
+ }
+ for (var k in req.body.settings.foursquare) {
+ fsq.value[k] = req.body.settings.foursquare[k];
+ }
+ fsq.commit('value');
+ fsq.save(function (err) {
+ if (err) throw err;
+
+ // Done with this step. Continue!
+ res.redirect("/foursquare/setup/connect");
+ });
+ return;
+ } else {
+ // Show the page for this step
+ return self.render('admin/setup/foursquare/app', {
+ layout: "admin/admin-layout",
+ locals: {
+ title: 'Foursquare',
+ settings: fsq.value
+ }
+ });
+ }
}
- }
+ });
});
}
View
111 controllers/TwitterController.js
@@ -19,6 +19,11 @@ exports.controller = function(req, res, next) {
self.platform = "twitter";
+ self.tasks = [
+ {controller: "TwitterController", method: "timeline", interval: 60},
+ {controller: "TwitterController", method: "stream", interval: 10, attributes: {connected: false}}
+ ];
+
self.setup = function () {
if (!req.require_authentication()) { return; }
@@ -34,52 +39,84 @@ exports.controller = function(req, res, next) {
step = steps[0];
}
- self._get_settings(function (err, tw) {
- if (err) throw err;
-
- if (step == "connect") { // Step 2: /twitter/setup/connect
- var is_setup = false;
- if (tw && tw.value && tw.value.access_token_key && tw.value.access_token_secret) {
- is_setup = true;
- }
- // Show the page for this step
- return self.render('admin/setup/twitter/connect', {
- layout: "admin/admin-layout",
- locals: {
- title: 'Connect to Twitter',
- settings: {},
- is_setup: is_setup,
- current_step: step
+ function check_tasks (cb) {
+ if (self.tasks.length == 0) {
+ cb(); return;
+ }
+ var ors = [];
+ self.tasks.forEach(function (task) {
+ ors.push({controller: task.controller, method: task.method});
+ });
+ var where = {"$or": ors};
+ Task.find(where, function (err, existing) {
+ if (err) throw err;
+
+ self.tasks.forEach(function (t) {
+ var found = false;
+ existing.forEach(function (et) {
+ if (t.controller == et.controller && t.method == et.method) {
+ found = true;
+ }
+ });
+ if (!found) {
+ var task = new Task(t);
+ task.save(function (err) {
+ if (err) throw err;
+ });
}
});
- } else { // Default is Step 1: /twitter/setup/app
- if (req.body && req.body.settings && req.body.settings.twitter) {
- // Process form
- if (!tw.value) {
- tw.value = {};
- }
- for (var k in req.body.settings.twitter) {
- tw.value[k] = req.body.settings.twitter[k];
+ cb();
+ });
+ }
+
+ check_tasks(function () {
+ self._get_settings(function (err, tw) {
+ if (err) throw err;
+
+ if (step == "connect") { // Step 2: /twitter/setup/connect
+ var is_setup = false;
+ if (tw && tw.value && tw.value.access_token_key && tw.value.access_token_secret) {
+ is_setup = true;
}
- tw.commit('value');
- tw.save(function (err) {
- if (err) throw err;
-
- // Done with this step. Continue!
- self._next_step(app_settings, step);
- });
- return;
- } else {
// Show the page for this step
- return self.render('admin/setup/twitter/app', {
+ return self.render('admin/setup/twitter/connect', {
layout: "admin/admin-layout",
locals: {
- title: 'Express TEST',
- settings: tw.value
+ title: 'Connect to Twitter',
+ settings: {},
+ is_setup: is_setup,
+ current_step: step
}
});
+ } else { // Default is Step 1: /twitter/setup/app
+ if (req.body && req.body.settings && req.body.settings.twitter) {
+ // Process form
+ if (!tw.value) {
+ tw.value = {};
+ }
+ for (var k in req.body.settings.twitter) {
+ tw.value[k] = req.body.settings.twitter[k];
+ }
+ tw.commit('value');
+ tw.save(function (err) {
+ if (err) throw err;
+
+ // Done with this step. Continue!
+ self._next_step(app_settings, step);
+ });
+ return;
+ } else {
+ // Show the page for this step
+ return self.render('admin/setup/twitter/app', {
+ layout: "admin/admin-layout",
+ locals: {
+ title: 'Express TEST',
+ settings: tw.value
+ }
+ });
+ }
}
- }
+ });
});
}

0 comments on commit 3fc46f9

Please sign in to comment.
Something went wrong with that request. Please try again.