summaryrefslogtreecommitdiff
path: root/lib/gitlab/phabricator_import/worker_state.rb
blob: 38829e3450950c7c968b5f762f1f3649f51c1ce1 (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
44
45
46
47
# frozen_string_literal: true
module Gitlab
  module PhabricatorImport
    class WorkerState
      def initialize(project_id)
        @project_id = project_id
      end

      def add_job
        redis.with do |r|
          r.pipelined do |pipe|
            pipe.incr(all_jobs_key)
            pipe.expire(all_jobs_key, timeout)
          end
        end
      end

      def remove_job
        redis.with do |r|
          r.decr(all_jobs_key)
        end
      end

      def running_count
        redis.with { |r| r.get(all_jobs_key) }.to_i
      end

      private

      attr_reader :project_id

      def redis
        Gitlab::Redis::SharedState
      end

      def all_jobs_key
        @all_jobs_key ||= "phabricator-import/jobs/project-#{project_id}/job-count"
      end

      def timeout
        # Make sure we get rid of all the information after a job is marked
        # as failed/succeeded
        StuckImportJobsWorker::IMPORT_JOBS_EXPIRATION
      end
    end
  end
end