Skip to content

Commit

Permalink
script to setup all teams
Browse files Browse the repository at this point in the history
finishes #69
  • Loading branch information
Gerad Suyderhoud committed Nov 2, 2013
1 parent 088c095 commit 6731434
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
3 changes: 3 additions & 0 deletions models/team.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ TeamSchema = module.exports = new mongoose.Schema
type: Number
default: 0
judgeVisitedAt: Date
setup:
status: String
log: String
TeamSchema.plugin require('../lib/use-timestamps')
TeamSchema.index updatedAt: -1
TeamSchema.index 'entry.url': 1
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup/reset-team.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = resetTeam = (team, next) ->
return next()
return next(err) if err
switch res.state
when 'running', 'provisioning', 'stopped', 'deleted'
when 'running', 'provisioning', 'stopping', 'stopped', 'deleted'
console.log team.slug, "#{res.state} (#{i * secs}s)"
setTimeout check, secs * 1000
i += 1
Expand Down
77 changes: 77 additions & 0 deletions scripts/setup/setup-teams.coffee
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

0 comments on commit 6731434

Please sign in to comment.