-
-
Notifications
You must be signed in to change notification settings - Fork 326
/
jobs-pickup.js
56 lines (47 loc) · 2.04 KB
/
jobs-pickup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const { send } = require('micro')
const { fetch } = require('../helpers/database')
const { update } = require('../helpers/database')
const { arrayIntersec } = require('../helpers/functions')
const { Mutex } = require('async-mutex');
const mutex = new Mutex();
module.exports = async (req, res) => {
const release = await mutex.acquire();
try{
console.log(`fetching a pickup job for a worker`)
const listing = await fetch()
let queued = []
if(req.params.tags){
queued = listing.filter(job => job.state == 'queued' && job.tags && arrayIntersec(req.params.tags.split(','),job.tags.split(',')).length )
}else{
queued = listing.filter(job => job.state == 'queued')
}
if (queued.length < 1) {
return send(res, 200, {})
}
let job;
if (process.env.NEXRENDER_ORDERING == 'random') {
job = queued[Math.floor(Math.random() * queued.length)];
}
else if (process.env.NEXRENDER_ORDERING == 'newest-first') {
job = queued[queued.length-1];
} else if (process.env.NEXRENDER_ORDERING == 'priority') {
// Get the job with the largest priority number
// This will also sort them by the date, so if 2 jobs have the same
// priority, it will choose the oldest one because that's the original state
// of the array in question
job = queued.sort((a, b) => {
// Quick sanitisation to make sure they're numbers
if (isNaN(a.priority)) a.priority = 0
if (isNaN(b.priority)) b.priority = 0
return b.priority - a.priority
})[0]
}
else { /* fifo (oldest-first) */
job = queued[0];
}
/* update the job locally, and send it to the worker */
send(res, 200, await update(job.uid, { state: 'picked', executor: req.headers["nexrender-name"] || req.headers["x-forwarded-for"] || req.socket.remoteAddress }))
} finally {
release();
}
}