combine.fm/worker.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-10-23 17:58:23 +01:00
import co from 'co';
import kue from 'kue';
import raven from 'raven';
2017-11-11 19:51:39 +00:00
import debuglog from 'debug';
2017-10-23 17:58:23 +01:00
import models from './models';
import services from './lib/services';
2017-11-11 19:51:39 +00:00
const debug = debuglog('combine.fm:worker');
2017-10-23 17:58:23 +01:00
raven.config(process.env.SENTRY_DSN).install();
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);
debug(err);
raven.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);
debug(err);
raven.captureException(err);
return done();
});
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);
});
kue.app.listen(3000);