Module: Gitlab::Scheduling::TaskExecutor

Defined in:
lib/gitlab/scheduling/task_executor.rb

Instance Method Summary collapse

Instance Method Details

#cache_keyObject



25
26
27
# File 'lib/gitlab/scheduling/task_executor.rb', line 25

def cache_key
  cache_key_for_period(cache_period)
end

#cache_key_for_period(period) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/gitlab/scheduling/task_executor.rb', line 33

def cache_key_for_period(period)
  [
    self.class.name.underscore,
    :execute_every,
    period.presence || "-",
    task
  ].join(':')
end

#cache_periodObject



29
30
31
# File 'lib/gitlab/scheduling/task_executor.rb', line 29

def cache_period
  nil
end

#dispatch_event(dispatch_config) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/gitlab/scheduling/task_executor.rb', line 42

def dispatch_event(dispatch_config)
  return unless dispatch_config

  event = dispatch_config[:event]
  data_proc = dispatch_config[:data]
  data = data_proc ? data_proc.call : {}

  Gitlab::EventStore.publish(event.new(data: data))
end

#execute_config_task(task_name, config) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gitlab/scheduling/task_executor.rb', line 6

def execute_config_task(task_name, config)
  execute_every(config[:period]) do
    unless config[:execute] || config[:dispatch]
      raise NotImplementedError, "No execute block or dispatch defined for task #{task_name}"
    end

    break false if config[:if]&.call == false

    instance_exec(&config[:execute]) if config[:execute]

    dispatch_event(config[:dispatch]) if config[:dispatch]
  end
end

#execute_every(period, &block) ⇒ Object



20
21
22
23
# File 'lib/gitlab/scheduling/task_executor.rb', line 20

def execute_every(period, &block)
  key = cache_key_for_period(period)
  Gitlab::Utils::RedisThrottle.execute_every(period, key, &block)
end