Add workers/get-work

This commit is contained in:
rubenwardy 2017-01-19 12:58:30 +00:00
parent 8fd76b88b6
commit 10f75b5d0d
3 changed files with 72 additions and 0 deletions

View File

@ -18,5 +18,6 @@ router.get("/", function(req, res) {
}) })
router.use(require("./mods")) router.use(require("./mods"))
router.use("/workers", require("./workerapi"))
module.exports = router module.exports = router

View File

@ -0,0 +1,36 @@
"use strict"
const express = require("express")
const router = express.Router()
router.get("/get-work", function(req, res) {
var worker_token = req.query.token
var db = req.app.get("db")
db.Worker.findOne({
where: {
token: worker_token
}
}).then(function(worker) {
db.Work.findAll({
limit: 1,
where: {
workerId: null
}
}).then(function(jobs) {
if (jobs && jobs.length == 1) {
var job = jobs[0]
job.workerId = worker.id
job.save()
res.send({
author: job.author,
basename: job.basename,
type: job.work_type
})
} else {
res.send({})
}
})
})
})
module.exports = router

View File

@ -38,6 +38,18 @@ var Mod = sequelize.define("mod", {
User.hasMany(Mod) User.hasMany(Mod)
Mod.belongsTo(User) Mod.belongsTo(User)
var Worker = sequelize.define("worker", {
token: Sequelize.STRING(128)
})
var Work = sequelize.define("work", {
author: Sequelize.STRING(100),
basename: Sequelize.STRING(100),
work_type: Sequelize.ENUM("fetch", "scan", "forum")
})
Work.belongsTo(Worker)
const CMod = require("./../../common/mod") const CMod = require("./../../common/mod")
function convertRowToMod(row) { function convertRowToMod(row) {
var mod = new CMod(row.user.username) var mod = new CMod(row.user.username)
@ -64,6 +76,8 @@ function convertRowToMod(row) {
async.parallel([ async.parallel([
function(callback) { User.sync().then(callback) }, function(callback) { User.sync().then(callback) },
function(callback) { Mod.sync().then(callback) }, function(callback) { Mod.sync().then(callback) },
function(callback) { Worker.sync().then(callback) },
function(callback) { Work.sync().then(callback) },
function() { function() {
User.findOrCreate({ User.findOrCreate({
where: { where: {
@ -98,7 +112,26 @@ async.parallel([
approved: true approved: true
} }
}).then(function(mod) { }).then(function(mod) {
Worker.findOrCreate({
where: {
token: "foobar"
},
defaults: {}
}).then(function(worker) {
worker = worker[0]
Work.findOrCreate({
where: {
author: "rubenwardy",
basename: "awards",
},
defaults: {
work_type: "fetch"
}
}).then(function(mod) {
})
})
}) })
}) })
}]) }])
@ -106,5 +139,7 @@ async.parallel([
module.exports = { module.exports = {
User: User, User: User,
Mod: Mod, Mod: Mod,
Work: Work,
Worker: Worker,
convertRowToMod: convertRowToMod convertRowToMod: convertRowToMod
} }