<commit_before>namespace :pub_sub do
  desc 'Poll the queue for updates'
  task poll: [:environment, :subscribe] do
    worker_concurrency.times.map do
      sleep 5 # Allow things to load to avoid circular reference errors (loading classes ain't threadsafe)
      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>Start workers with a delay proportional to its sequence number<commit_after>namespace :pub_sub do
  desc 'Poll the queue for updates'
  task poll: [:environment, :subscribe] do
    worker_concurrency.times.map do |idx|
      sleep idx*5 # Allow things to load to avoid circular reference errors (loading classes ain't threadsafe)
      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.08262108262108261, 'fraction_numerical': 0.002279202279202279, 'mean_word_length': 3.8913649025069637, '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': '7634743', 'n_tokens_mistral': 559, 'n_tokens_neox': 544, 'n_words': 167}