{"size": 1370, "ext": "js", "max_stars_repo_path": "src/worker.js", "max_stars_repo_name": "ni554n/m0rpheus", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/worker.js", "max_issues_repo_name": "ni554n/m0rpheus", "max_issues_repo_issues_event_min_datetime": "2020-09-22T21:32:15.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-16T22:10:53.000Z", "max_forks_repo_path": "src/worker.js", "max_forks_repo_name": "ni554n/m0rpheus", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 31.8604651163, "max_line_length": 87, "alphanum_fraction": 0.7072992701}
const throng = require("throng");
const Queue = require("bull");

const downloader = require("./scripts/downloader");
const uploader = require("./scripts/uploader");
const notifier = require("./scripts/notifier");

// Connect to a local redis instance locally, and the Heroku-provided URL in production
const REDIS_URL = process.env.REDIS_URL || "redis://127.0.0.1:6379";

const QUEUE_NAME = process.env.QUEUE_NAME || "queue";

// Spin up multiple processes to handle jobs to take advantage of more CPU cores
// See: https://devcenter.heroku.com/articles/node-concurrency for more info
const workerCount = process.env.WEB_CONCURRENCY || 1;

function workerStartFunction() {
  const workQueue = new Queue(QUEUE_NAME, REDIS_URL);

  workQueue.process(100, async (job) => {
    const { mgaAccessToken, trackUrl, trackName } = job.data;

    try {
      const downloadedMusicPaths = await downloader.downloadMusic(trackUrl);

      downloadedMusicPaths.forEach((path) => {
        uploader.upload(path.path, mgaAccessToken);
      });
    } catch (error) {
      notifier.push(`Failed ~ ${trackName}`, error);
    }
  });
}

// Initialize the clustered worker process
// See: https://devcenter.heroku.com/articles/node-concurrency for more info
try {
  // @ts-ignore
  throng({ worker: workerStartFunction, count: workerCount });
} catch (error) {
  console.error(error);
}
