<commit_before>namespace :pub_sub do
  desc 'Poll the queue for updates'
  task poll: [:environment, :subscribe] do
    worker_concurrency.times.map do |idx|
      # Give each thread some time to load to avoid circular reference errors (class-loading is not threadsafe)
      # [FIXME] ^ (written by a previous developer) likely refers to Breaker, though I still am not convinced there is a race condition.
      sleep idx*10
      Thread.new { start_poll_thread }
    end.each(&:join)
  end

  desc 'Subscribe to the topics defined in the config'
  task subscribe: :environment do
    PubSub::Subscriber.subscribe
  end

  def start_poll_thread
    PubSub::Poller.new.poll
  rescue => e
    NewRelic::Agent.notice_error(e) if defined?(NewRelic)
    PubSub.logger.error("Unknown error polling subscriptions: #{e.inspect}")
    PubSub.logger.error(e.backtrace)
    retry
  end

  # How many threads to use for workers
  def worker_concurrency
    ENV.fetch('PUB_SUB_WORKER_CONCURRENCY', 2).to_i
  end
end
<commit_msg>Reduced startup delta between workers from 10 to 5 seconds<commit_after>namespace :pub_sub do
  desc 'Poll the queue for updates'
  task poll: [:environment, :subscribe] do
    worker_concurrency.times.map do |idx|
      # Give each thread some time to load to avoid circular reference errors (class-loading is not threadsafe)
      # [FIXME] ^ (written by a previous developer) likely refers to Breaker, though I still am not convinced there is a race condition.
      sleep idx*5
      Thread.new { start_poll_thread }
    end.each(&:join)
  end

  desc 'Subscribe to the topics defined in the config'
  task subscribe: :environment do
    PubSub::Subscriber.subscribe
  end

  def start_poll_thread
    PubSub::Poller.new.poll
  rescue => e
    NewRelic::Agent.notice_error(e) if defined?(NewRelic)
    PubSub.logger.error("Unknown error polling subscriptions: #{e.inspect}")
    PubSub.logger.error(e.backtrace)
    retry
  end

  # How many threads to use for workers
  def worker_concurrency
    ENV.fetch('PUB_SUB_WORKER_CONCURRENCY', 2).to_i
  end
end

{'fraction_non_alphanumeric': 0.0789980732177264, 'fraction_numerical': 0.0038535645472061657, 'mean_word_length': 3.6990950226244346, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3720480', 'n_tokens_mistral': 645, 'n_tokens_neox': 630, 'n_words': 217}