-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfixmissing.js
73 lines (61 loc) · 1.61 KB
/
fixmissing.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import co from 'co';
import kue from 'kue';
import debuglog from 'debug';
import models from './models';
import services from './lib/services';
const debug = debuglog('combine.fm:fixmissing');
const queue = kue.createQueue({
redis: process.env.REDIS_URL,
});
debug('Fixing missing');
const serviceIds = [];
services.forEach((service) => {
serviceIds.push(service.id);
});
const query = {
include: [
{ model: models.artist },
{ model: models.match },
],
order: [
['createdAt', 'DESC'],
],
};
function search(data) {
const { share } = data;
const service = services.find(item => data.service.id === item.id);
debug(`Matching ${share.name} on ${data.service.id}`);
const job = queue.create('search-backlog', { title: `Matching ${share.name} on ${service.id}`, share, service })
.attempts(3)
.backoff({ type: 'exponential' })
.save((err) => {
debug(err || `JobID: ${job.id}`);
});
}
function* find(model) {
const items = yield models[model].findAll(query);
items.forEach((item) => {
let unmatched = serviceIds;
item.matches.forEach((match) => {
unmatched = unmatched.filter(id => match.service !== id);
});
if (unmatched.length > 0) {
debug(`Matching ${unmatched.join(', ')}`);
unmatched.forEach((toMatch) => {
search({ share: item, service: { id: toMatch } });
});
} else {
debug(`No broken matches for ${item.name}`);
}
});
return Promise.resolve();
}
co(function* main() {
yield find('album');
yield find('track');
setTimeout(() => {
process.exit(0);
}, 1000);
}).catch((err) => {
debug(err.stack);
});