Module: ActionSubscriber::Babou

Defined in:
lib/action_subscriber/babou.rb

Class Method Summary collapse

Class Method Details

.auto_pop!Object

Class Methods



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/action_subscriber/babou.rb', line 7

def self.auto_pop!
  @pop_mode = true
  reload_active_record
  ::ActionSubscriber.setup_default_connection!
  sleep_time = ::ActionSubscriber.configuration.pop_interval.to_i / 1000.0

  ::ActionSubscriber.start_queues
  logger.info "Action Subscriber is popping messages every #{sleep_time} seconds."

  # How often do we want the timer checking for new pops
  # since we included an eager popper we decreased the
  # default check interval to 100m
  while true
    ::ActionSubscriber.auto_pop! unless shutting_down?
    sleep sleep_time
    break if shutting_down?
  end
end

.loggerObject



48
49
50
# File 'lib/action_subscriber/babou.rb', line 48

def self.logger
  ::ActionSubscriber::Logging.logger
end

.pop?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/action_subscriber/babou.rb', line 26

def self.pop?
  !!@pop_mode
end

.prowl?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/action_subscriber/babou.rb', line 44

def self.prowl?
  !!@prowl_mode
end

.reload_active_recordObject



52
53
54
55
56
# File 'lib/action_subscriber/babou.rb', line 52

def self.reload_active_record
  if defined?(::ActiveRecord::Base) && !::ActiveRecord::Base.connected?
    ::ActiveRecord::Base.establish_connection
  end
end

.shutting_down?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/action_subscriber/babou.rb', line 58

def self.shutting_down?
  !!@shutting_down
end

.start_subscribersObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/action_subscriber/babou.rb', line 30

def self.start_subscribers
  @prowl_mode = true
  reload_active_record
  ::ActionSubscriber.setup_default_connection!

  ::ActionSubscriber.start_subscribers
  logger.info "Action Subscriber connected"

  while true
    sleep 1.0 #just hang around waiting for messages
    break if shutting_down?
  end
end

.stop_receving_messages!Object



62
63
64
65
66
67
68
# File 'lib/action_subscriber/babou.rb', line 62

def self.stop_receving_messages!
  @shutting_down = true
  ::Thread.new do
    ::ActionSubscriber.stop_subscribers!
    logger.info "stopped all subscribers"
  end.join
end

.stop_server!Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/action_subscriber/babou.rb', line 70

def self.stop_server!
  # this method is called from within a TRAP context so we can't use the logger
  puts "Stopping server..."
  wait_loops = 0
  ::ActionSubscriber::Babou.stop_receving_messages!

  while ::ActionSubscriber::Threadpool.busy? && wait_loops < ::ActionSubscriber.configuration.seconds_to_wait_for_graceful_shutdown
    puts "waiting for threadpools to empty:"
    ::ActionSubscriber::Threadpool.pools.each do |name, pool|
      puts "  -- #{name} (remaining: #{pool.busy_size})" if pool.busy_size > 0
    end
    Thread.pass
    wait_loops = wait_loops + 1
    sleep 1
  end

  puts "threadpool empty. Shutting down"
end