Add workers/get-work
This commit is contained in:
parent
8fd76b88b6
commit
10f75b5d0d
@ -18,5 +18,6 @@ router.get("/", function(req, res) {
|
||||
})
|
||||
|
||||
router.use(require("./mods"))
|
||||
router.use("/workers", require("./workerapi"))
|
||||
|
||||
module.exports = router
|
||||
|
36
webapp/controllers/workerapi.js
Normal file
36
webapp/controllers/workerapi.js
Normal 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
|
@ -38,6 +38,18 @@ var Mod = sequelize.define("mod", {
|
||||
User.hasMany(Mod)
|
||||
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")
|
||||
function convertRowToMod(row) {
|
||||
var mod = new CMod(row.user.username)
|
||||
@ -64,6 +76,8 @@ function convertRowToMod(row) {
|
||||
async.parallel([
|
||||
function(callback) { User.sync().then(callback) },
|
||||
function(callback) { Mod.sync().then(callback) },
|
||||
function(callback) { Worker.sync().then(callback) },
|
||||
function(callback) { Work.sync().then(callback) },
|
||||
function() {
|
||||
User.findOrCreate({
|
||||
where: {
|
||||
@ -98,7 +112,26 @@ async.parallel([
|
||||
approved: true
|
||||
}
|
||||
}).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 = {
|
||||
User: User,
|
||||
Mod: Mod,
|
||||
Work: Work,
|
||||
Worker: Worker,
|
||||
convertRowToMod: convertRowToMod
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user