forked from nko3/website
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finishes #69
- Loading branch information
Gerad Suyderhoud
committed
Nov 2, 2013
1 parent
088c095
commit 6731434
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# setup teams, taking care not to run things for teams that are already setup | ||
|
||
async = require('async') | ||
mongoose = require('../../models')(require('../../config/env').mongo_url) | ||
spawn = require('child_process').spawn | ||
path = require('path') | ||
|
||
rootDir = path.join(__dirname, '..', '..') | ||
Team = mongoose.model 'Team' | ||
slug = '' | ||
team = null | ||
|
||
hasTeam = -> team | ||
|
||
processTeam = (next) -> | ||
async.series [loadTeam, setupTeam], next | ||
|
||
loadTeam = (next) -> | ||
selectTeam (err, res) -> | ||
return next(err) if err | ||
unless res?._id | ||
team = null | ||
return next(res) | ||
Team.findById res._id, (err, _team) -> | ||
team = _team | ||
next(err) | ||
|
||
setupTeam = (next) -> | ||
return next() unless team | ||
console.log team.slug, 'setting up team...' | ||
team.setup.log = '' | ||
|
||
setupData = (data) -> | ||
str = data.toString() | ||
console.log(str) | ||
team.setup.log += str | ||
team.save -> # nothing | ||
|
||
setupError = (err) -> | ||
team.setup.status = 'error' | ||
team.setup.log += "\n\nError: #{err}" | ||
team.save next | ||
|
||
setupCompleted = -> | ||
team.setup.status = 'completed' | ||
team.save next | ||
|
||
coffee = path.join(rootDir, 'node_modules', '.bin', 'coffee') | ||
setupScript = path.join(rootDir, 'scripts', 'setup', 'setup-team.coffee') | ||
setup = spawn coffee, [setupScript, team.slug], cwd: rootDir | ||
setup.stdout.on 'data', setupData | ||
setup.stderr.on 'data', setupData | ||
setup.on 'error', (err) -> setupError(err) | ||
setup.on 'exit', (err) -> | ||
return setupError(err) if err | ||
setupCompleted() | ||
|
||
# atomically select a team that is not being setup | ||
selectTeam = (next) -> | ||
query = { | ||
'setup.status': null, | ||
slug: { $in: ['organizers'] } | ||
} | ||
sort = [] | ||
update = { $set: { 'setup.status': 'processing' }} | ||
options = {} | ||
|
||
Team.collection.findAndModify(query, sort, update, options, next) | ||
|
||
last = (err) -> | ||
if err | ||
console.log(err) | ||
process.exit(1) | ||
else | ||
mongoose.connection.close() | ||
|
||
async.doWhilst processTeam, hasTeam, last |