combine.fm/worker.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

2025-05-20 13:58:46 +01:00
import co from "co";
import kue from "kue";
import Sentry from "@sentry/node";
import debuglog from "debug";
import { inspect } from "util";
2017-10-23 17:58:23 +01:00
2025-05-20 13:58:46 +01:00
import models from "./models/index.cjs";
import services from "./lib/services.js";
2017-10-23 17:58:23 +01:00
2025-05-20 13:58:46 +01:00
const debug = debuglog("combine.fm:worker");
2017-11-11 19:51:39 +00:00
2019-03-11 21:03:44 +00:00
Sentry.init({
2025-05-20 13:58:46 +01:00
dsn: process.env.SENTRY_DSN
2019-03-11 21:03:44 +00:00
});
2017-10-23 17:58:23 +01:00
const queue = kue.createQueue({
2025-05-20 13:58:46 +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);
2025-05-20 14:04:45 +01:00
if (!service) {
return;
}
debug(`Searching on: ${service.id}`);
2025-05-20 13:58:46 +01:00
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({
2025-05-20 13:58:46 +01:00
trackId: share.type === "track" ? share.id : null,
albumId: share.type === "album" ? share.id : null,
2017-10-24 12:59:58 +01:00
externalId: match.id.toString(),
service: match.service,
name: match.name,
streamUrl: match.streamUrl,
purchaseUrl: match.purchaseUrl,
artworkSmall: match.artwork.small,
2025-05-20 13:58:46 +01:00
artworkLarge: match.artwork.large
2017-10-24 12:59:58 +01:00
});
} else {
models.match.create({
2025-05-20 13:58:46 +01:00
trackId: share.type === "track" ? share.id : null,
albumId: share.type === "album" ? share.id : null,
2017-10-24 12:59:58 +01:00
externalId: null,
service: match.service,
name: null,
streamUrl: null,
purchaseUrl: null,
artworkSmall: null,
2025-05-20 13:58:46 +01:00
artworkLarge: null
2017-10-24 12:59:58 +01:00
});
}
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
}
2025-05-20 13:58:46 +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
}
2025-05-20 13:58:46 +01:00
queue.process("search", 5, (job, done) => {
2017-10-23 17:58:23 +01:00
search(job.data, done);
});
2025-05-20 13:58:46 +01:00
queue.process("search-backlog", 1, (job, ctx, done) => {
2018-04-08 22:39:50 +01:00
search(job.data, done);
ctx.pause(7000, () => {
2025-05-20 13:58:46 +01:00
console.log("Worker is paused... ");
setTimeout(() => {
ctx.resume();
}, 10000);
2018-04-08 22:39:50 +01:00
});
});
2017-10-23 17:58:23 +01:00
2025-05-20 13:58:46 +01:00
kue.app.listen(3001);