-
Notifications
You must be signed in to change notification settings - Fork 22
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
Add temporary machines support #134
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -409,20 +409,32 @@ containersAPI.put({ | |
title: 'Create a container', | ||
|
||
handler (request, response) { | ||
const { user } = request; | ||
if (!user) { | ||
response.statusCode = 403; // Forbidden | ||
response.json({ error: 'Unauthorized' }, null, 2); | ||
return; | ||
} | ||
|
||
const projectId = request.query.project; | ||
if (!(projectId in db.get('projects'))) { | ||
response.statusCode = 404; // Not Found | ||
response.json({ error: 'Project not found' }, null, 2); | ||
return; | ||
} | ||
|
||
const { user, session } = request; | ||
if (!user) { | ||
if (!session) { | ||
return; | ||
} | ||
|
||
machines.spawnTemporary(session.id, projectId, (error, machine) => { | ||
if (error) { | ||
response.statusCode = 500; // Internal Server Error | ||
response.json({ error: 'Could not create container' }, null, 2); | ||
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. Might want to return after than, in order not to end a response twice. 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. Whoops, good catch! |
||
return; | ||
} | ||
response.json({ | ||
container: machine.docker.container | ||
}, null, 2); | ||
}); | ||
return; | ||
} | ||
|
||
machines.spawn(user, projectId, (error, machine) => { | ||
if (error) { | ||
log('[fail] could not spawn machine', error); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const db = require('./db'); | ||
const log = require('./log'); | ||
let taskCounter = 0; // Used to generate unique task IDs | ||
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. Nit: I would really prefer tasks to be a separate pull request. 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. Moved to #140 |
||
|
||
exports.tasks = db.get('tasks'); | ||
exports.taskTypes = new Map(); | ||
|
||
exports.check = function () { | ||
const now = Date.now(); | ||
for (const task of Object.values(exports.tasks)) { | ||
if (now > Number(task.date)) { | ||
exports.execute(task); | ||
} | ||
} | ||
}; | ||
|
||
exports.addType = function (type, task) { | ||
if (exports.taskTypes.has(type)) { | ||
throw new Error('[fail] task', task, 'already exists'); | ||
} | ||
|
||
exports.taskTypes.set(type, task); | ||
}; | ||
|
||
exports.add = function (date, type, data) { | ||
taskCounter++; | ||
|
||
const msSince1970 = date.getTime(); | ||
const taskId = `${type}-${msSince1970}-${taskCounter}`; | ||
|
||
const task = { | ||
id: taskId, | ||
date: msSince1970, | ||
type, | ||
data, | ||
}; | ||
|
||
exports.tasks[taskId] = task; | ||
db.save(); | ||
return task; | ||
}; | ||
|
||
exports.remove = function (id) { | ||
delete exports.tasks[id]; | ||
db.save(); | ||
}; | ||
|
||
exports.execute = function ({ type, data, id }) { | ||
const task = exports.taskTypes.get(type); | ||
log('[ok] Task', id, 'executed', data); | ||
task(data); | ||
exports.remove(id); | ||
}; |
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.
Maybe log the error to know what's happening? E.g.
log('[fail] creating temporary container', error)
.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.
It's already logged inside
_spawn
in machines.js