Class: Abid::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/abid/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ Worker

Returns a new instance of Worker.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/abid/worker.rb', line 3

def initialize(application)
  @application = application
  @pools = {}
  @pool_definitions = {}

  if application.options.always_multitask
    default_thread_num = @application.options.thread_pool_size || \
                         Rake.suggested_thread_count - 1
  else
    default_thread_num = 1
  end
  define(:default, default_thread_num)

  define(:fresh, -1)
end

Instance Method Details

#[](name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/abid/worker.rb', line 25

def [](name)
  return @pools[name] if @pools.include?(name)
  return self[:fresh] if name == :waiter # alias

  unless @pool_definitions.include?(name)
    fail "worker #{name} is not defined"
  end

  if @pool_definitions[name] > 0
    @pools[name] = Concurrent::FixedThreadPool.new(
      @pool_definitions[name],
      idletime: FIXNUM_MAX
    )
  else
    @pools[name] = Concurrent::SimpleExecutorService.new
  end
end

#define(name, thread_count) ⇒ Object



19
20
21
22
23
# File 'lib/abid/worker.rb', line 19

def define(name, thread_count)
  name = name.to_sym
  fail "worker #{name} already defined" if @pool_definitions.include?(name)
  @pool_definitions[name] = thread_count
end

#killObject



50
51
52
53
54
# File 'lib/abid/worker.rb', line 50

def kill
  @pools.each do |_, pool|
    pool.kill
  end
end

#shutdownObject



43
44
45
46
47
48
# File 'lib/abid/worker.rb', line 43

def shutdown
  @pools.each do |_, pool|
    pool.shutdown
    pool.wait_for_termination
  end
end