{"size": 2600, "ext": "js", "max_stars_repo_path": "packages/collector/test/tracing/messaging/bull/sender.js", "max_stars_repo_name": "gebhardtr/nodejs", "max_stars_repo_stars_event_min_datetime": "2016-08-31T06:16:09.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-23T08:30:21.000Z", "max_issues_repo_path": "packages/collector/test/tracing/messaging/bull/sender.js", "max_issues_repo_name": "gebhardtr/nodejs", "max_issues_repo_issues_event_min_datetime": "2016-07-08T13:35:32.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-10T05:34:31.000Z", "max_forks_repo_path": "packages/collector/test/tracing/messaging/bull/sender.js", "max_forks_repo_name": "gebhardtr/nodejs", "max_forks_repo_forks_event_min_datetime": "2017-02-05T12:41:07.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-19T06:33:03.000Z"}
{"max_stars_count": 62, "max_issues_count": 110, "max_forks_count": 35, "avg_line_length": 29.2134831461, "max_line_length": 115, "alphanum_fraction": 0.6396153846}
/*
 * (c) Copyright IBM Corp. 2021
 * (c) Copyright Instana Inc. and contributors 2021
 */

'use strict';

require('../../../../')();
const logPrefix = `Bull (${process.pid}):\t`;
const Queue = require('bull');
const redisServer = process.env.REDIS_SERVER || 'redis://127.0.0.1:6379';
const queueName = process.env.BULL_QUEUE_NAME || 'nodejs-team';
const express = require('express');
const port = process.env.APP_PORT || 3215;
const bullJobName = process.env.BULL_JOB_NAME || 'steve';

const app = express();

const sender = new Queue(queueName, redisServer);

function getJobData(testId, bulkIndex, withError) {
  const rnd = Math.random() * 1e9;
  if (bulkIndex) {
    return {
      testId,
      bulkIndex,
      name: `I am in a bulk; ${rnd};`,
      withError
    };
  }
  return {
    testId,
    name: `Job with random number: ${rnd}`,
    withError
  };
}

const log = require('@instana/core/test/test_util/log').getLogger(logPrefix);

// curl -X POST "http://127.0.0.1:3215/send?jobName=true&bulk=false&repeat=true"
app.post('/send', (request, response) => {
  const testId = request.query.testId || 'none';
  const repeat = request.query.repeat === 'true';
  const jobName = request.query.jobName === 'true';
  const bulk = request.query.bulk === 'true';
  const withError = request.query.withError === 'true';

  /** @type {import('bull').JobOptions} */
  const options = {};
  let addFunction = 'add';
  const functionParams = [];
  const bulkedJobs = [];

  if (repeat && !bulk) {
    options.repeat = {
      every: 100,
      limit: 2
    };
  }

  if (bulk) {
    addFunction = 'addBulk';
    bulkedJobs.push(
      { name: bullJobName, data: getJobData(testId, 1, withError) },
      { name: bullJobName, data: getJobData(testId, 2, withError) }
      // As per https://github.com/OptimalBits/bull/issues/1731, addBulk does not properly support repeatable jobs.
      // Newer versions of Bull forbid this case completely, and older versions don't handle the case properly.
      // { name: bullJobName, data: getJobData(testId, true), opts: { repeat: { every: 500, limit: 1 } } }
    );
    functionParams.push(bulkedJobs);
  } else if (jobName) {
    functionParams.push(bullJobName, getJobData(testId, null, withError), options);
  } else {
    functionParams.push(getJobData(testId, null, withError), options);
  }

  sender[addFunction].apply(sender, functionParams);

  response.send({
    status: `Job${repeat || bulk ? 's' : ''} sent`
  });
});

app.get('/', (_req, res) => {
  res.send('ok');
});

app.listen(port, () => log(`Bull sender app listening on port ${port}`));
