Class: Asynchronic::GarbageCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/asynchronic/garbage_collector.rb

Instance Method Summary collapse

Constructor Details

#initialize(environment, timeout) ⇒ GarbageCollector

Returns a new instance of GarbageCollector.



4
5
6
7
8
9
# File 'lib/asynchronic/garbage_collector.rb', line 4

def initialize(environment, timeout)
  @environment = environment
  @timeout = timeout
  @running = false
  @conditions = {}
end

Instance Method Details

#add_condition(name, &block) ⇒ Object



42
43
44
# File 'lib/asynchronic/garbage_collector.rb', line 42

def add_condition(name, &block)
  conditions[name] = block
end

#conditions_namesObject



50
51
52
# File 'lib/asynchronic/garbage_collector.rb', line 50

def conditions_names
  conditions.keys
end

#remove_condition(name) ⇒ Object



46
47
48
# File 'lib/asynchronic/garbage_collector.rb', line 46

def remove_condition(name)
  conditions.delete name
end

#startObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/asynchronic/garbage_collector.rb', line 11

def start
  Asynchronic.logger.info('Asynchronic') { 'Starting GC' }

  Signal.trap('QUIT') { stop }

  @running = true

  while @running
    processes = environment.processes

    processes.each(&:abort_if_dead)

    conditions.each do |name, condition|
      Asynchronic.logger.info('Asynchronic') { "Running GC - #{name}" }
      begin
        processes.select(&condition).each(&:destroy)
      rescue => ex
        error_message = "#{ex.class}: #{ex.message}\n#{ex.backtrace.join("\n")}"
        Asynchronic.logger.error('Asynchronic') { error_message }
      end
    end

    wait
  end
end

#stopObject



37
38
39
40
# File 'lib/asynchronic/garbage_collector.rb', line 37

def stop
  Asynchronic.logger.info('Asynchronic') { 'Stopping GC' }
  @running = false
end