Class: RailsFastCache::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-fast-cache/scheduler.rb

Constant Summary collapse

EXECUTOR_OPTIONS =
{
  min_threads: ENV.fetch('RAILS_MAX_THREADS', 3).to_i,
  max_threads: ENV.fetch('RAILS_MAX_THREADS', 3).to_i,
  max_queue: ENV.fetch('RAILS_FAST_CACHE_MAX_QUEUE', 100).to_i,
  fallback_policy: :caller_runs
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.



14
15
16
17
18
19
# File 'lib/rails-fast-cache/scheduler.rb', line 14

def initialize
  @executor = Concurrent::ThreadPoolExecutor.new(**EXECUTOR_OPTIONS)
  @inflight = Concurrent::AtomicFixnum.new(0)
  @idle = Concurrent::Event.new
  @idle.set
end

Instance Method Details

#flush(timeout = nil) ⇒ Object



31
32
33
# File 'lib/rails-fast-cache/scheduler.rb', line 31

def flush(timeout = nil)
  @idle.wait(timeout)
end

#post(&block) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/rails-fast-cache/scheduler.rb', line 21

def post(&block)
  @inflight.increment
  @idle.reset
  @executor.post do
    block.call
  ensure
    @idle.set if @inflight.decrement.zero?
  end
end

#shutdown(wait: true) ⇒ Object



35
36
37
38
# File 'lib/rails-fast-cache/scheduler.rb', line 35

def shutdown(wait: true)
  @executor.shutdown
  @executor.wait_for_termination if wait
end