Class: Karafka::Pro::Processing::Schedulers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/pro/processing/schedulers/base.rb

Overview

Note:

All the ‘on_` methods can be redefined with a non-thread-safe versions without locks if needed, however when doing so, ensure that your scheduler is stateless.

Base for all the Pro custom schedulers

It wraps the Scheduler API with mutex to ensure, that during scheduling we do not start scheduling other work that could impact the decision making in between multiple subscription groups running in separate threads.

Direct Known Subclasses

Default

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ Base



21
22
23
24
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 21

def initialize(queue)
  @queue = queue
  @mutex = Mutex.new
end

Instance Method Details

#clear(_group_id) ⇒ Object

By default schedulers are stateless, so nothing to clear.



122
123
124
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 122

def clear(_group_id)
  nil
end

#manageObject

Should manage scheduling on jobs state changes

By default does nothing as default schedulers are stateless



108
109
110
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 108

def manage
  nil
end

#on_clear(group_id) ⇒ Object

Runs clearing under mutex



115
116
117
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 115

def on_clear(group_id)
  @mutex.synchronize { clear(group_id) }
end

#on_manageObject

Runs the manage tick under mutex



101
102
103
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 101

def on_manage
  @mutex.synchronize { manage }
end

#on_schedule_consumption(jobs_array) ⇒ Object

Runs the consumption jobs scheduling flow under a mutex



39
40
41
42
43
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 39

def on_schedule_consumption(jobs_array)
  @mutex.synchronize do
    schedule_consumption(jobs_array)
  end
end

#on_schedule_idle(jobs_array) ⇒ Object

Runs the idle jobs scheduling flow under a mutex



77
78
79
80
81
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 77

def on_schedule_idle(jobs_array)
  @mutex.synchronize do
    schedule_idle(jobs_array)
  end
end

#on_schedule_periodic(jobs_array) ⇒ Object

Runs the periodic jobs scheduling flow under a mutex



88
89
90
91
92
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 88

def on_schedule_periodic(jobs_array)
  @mutex.synchronize do
    schedule_periodic(jobs_array)
  end
end

#on_schedule_revocation(jobs_array) ⇒ Object

Runs the revocation jobs scheduling flow under a mutex



59
60
61
62
63
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 59

def on_schedule_revocation(jobs_array)
  @mutex.synchronize do
    schedule_revocation(jobs_array)
  end
end

#on_schedule_shutdown(jobs_array) ⇒ Object

Runs the shutdown jobs scheduling flow under a mutex



68
69
70
71
72
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 68

def on_schedule_shutdown(jobs_array)
  @mutex.synchronize do
    schedule_shutdown(jobs_array)
  end
end

#schedule_consumption(_jobs_array) ⇒ Object

Should schedule the consumption jobs

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 50

def schedule_consumption(_jobs_array)
  raise NotImplementedError, 'Implement in a subclass'
end

#schedule_fifo(jobs_array) ⇒ Object Also known as: schedule_revocation, schedule_shutdown, schedule_idle, schedule_periodic

Schedules any jobs provided in a fifo order



28
29
30
31
32
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 28

def schedule_fifo(jobs_array)
  jobs_array.each do |job|
    @queue << job
  end
end