Welcome to mirror list, hosted at ThFree Co, Russian Federation.

archive_base.rb « workers « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6b8b38a824079f1a30d825475ba4a0acae5ab6e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

module Workers
  class ArchiveBase < Base
    sidekiq_options queue: :low

    include Diaspora::Logging

    def perform(*args)
      if currently_running_archive_jobs >= AppConfig.settings.archive_jobs_concurrency.to_i
        logger.info "Already the maximum number of parallel archive jobs running, " \
                    "scheduling #{self.class}:#{args} in 5 minutes."
        self.class.perform_in(5.minutes + rand(30), *args)
      else
        perform_archive_job(*args)
      end
    end

    private

    def perform_archive_job(_args)
      raise NotImplementedError, "You must override perform_archive_job"
    end

    def currently_running_archive_jobs
      Sidekiq::Workers.new.count do |process_id, thread_id, work|
        !(Process.pid.to_s == process_id.split(":")[1] && Thread.current.object_id.to_s(36) == thread_id) &&
          ArchiveBase.subclasses.map(&:to_s).include?(work["payload"]["class"])
      end
    rescue Redis::CannotConnectError
      # If code gets to this point and there is no Redis conenction, we're
      # running in a Test environment and have not mocked Sidekiq::Workers, so
      # we're not testing the concurrency-limiting behavior.
      # There is no way a production pod will run into this code, as diaspora*
      # refuses to start without redis.
      0
    end
  end
end