combine.fm/worker.js

83 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-10-23 17:58:23 +01:00
import co from 'co';
import kue from 'kue';
2020-01-18 10:19:20 +00:00
import Sentry from '@sentry/node';
2017-11-11 19:51:39 +00:00
import debuglog from 'debug';
2018-04-08 17:22:19 +01:00
import { inspect } from 'util';
2017-10-23 17:58:23 +01:00
2020-01-18 10:19:20 +00:00
import models from './models/index.cjs';
import services from './lib/services.js';
2017-10-23 17:58:23 +01:00
2017-11-11 19:51:39 +00:00
const debug = debuglog('combine.fm:worker');
2019-03-11 21:03:44 +00:00
Sentry.init({
dsn: process.env.SENTRY_DSN,
});
2017-10-23 17:58:23 +01:00
const queue = kue.createQueue({
2017-10-23 19:26:49 +01:00
redis: process.env.REDIS_URL,
2017-10-23 17:58:23 +01:00
});
function search(data, done) {
const share = data.share;
const service = services.find(item => data.service.id === item.id);
debug(`Searching on: ${service.id}`);
co(function* gen() { // eslint-disable-line no-loop-func
2017-10-24 12:59:58 +01:00
try {
const match = yield service.search(share);
if (match.id) {
models.match.create({
trackId: share.type === 'track' ? share.id : null,
albumId: share.type === 'album' ? share.id : null,
externalId: match.id.toString(),
service: match.service,
name: match.name,
streamUrl: match.streamUrl,
purchaseUrl: match.purchaseUrl,
artworkSmall: match.artwork.small,
artworkLarge: match.artwork.large,
});
} else {
models.match.create({
trackId: share.type === 'track' ? share.id : null,
albumId: share.type === 'album' ? share.id : null,
externalId: null,
service: match.service,
name: null,
streamUrl: null,
purchaseUrl: null,
artworkSmall: null,
artworkLarge: null,
});
}
return done();
} catch (err) {
debug(`Error searching on: ${service.id}`);
debug(share);
2018-04-08 17:22:19 +01:00
debug(inspect(err, { depth: 5 }));
2019-03-11 21:03:44 +00:00
Sentry.captureException(err);
2017-10-24 12:59:58 +01:00
return done(err);
2017-10-23 17:58:23 +01:00
}
}).catch((err) => {
debug(`Error searching on: ${service.id}`);
2017-11-11 19:51:39 +00:00
debug(share);
2018-04-08 17:22:19 +01:00
debug(inspect(err, { depth: 5 }));
2019-03-11 21:03:44 +00:00
Sentry.captureException(err);
2018-04-08 17:22:19 +01:00
return done(err);
});
2017-10-23 17:58:23 +01:00
}
queue.process('search', 5, (job, done) => {
2017-10-23 17:58:23 +01:00
search(job.data, done);
});
2018-04-08 22:39:50 +01:00
queue.process('search-backlog', 1, (job, ctx, done) => {
search(job.data, done);
ctx.pause(7000, () => {
console.log('Worker is paused... ');
setTimeout(() => { ctx.resume(); }, 10000);
});
});
2017-10-23 17:58:23 +01:00
kue.app.listen(3000);