Sha256: 9998c3ae861137a277d4b65ebbcce72c306a1a164d572e538565e2bb2749b179

Contents?: true

Size: 1.22 KB

Versions: 9

Compression:

Stored size: 1.22 KB

Contents

module Asynchronic
  class GarbageCollector

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

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

      Signal.trap('QUIT') { stop }

      @running = true

      while @running
        processes = environment.processes

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

        wait
      end
    end

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

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

    def remove_condition(name)
      conditions.delete name
    end

    def conditions_names
      conditions.keys
    end

    private

    attr_reader :environment, :timeout, :conditions

    def wait
      Asynchronic.logger.info('Asynchronic') { 'Sleeping GC' }
      sleep timeout
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
asynchronic-2.0.1 lib/asynchronic/garbage_collector.rb
asynchronic-2.0.0 lib/asynchronic/garbage_collector.rb
asynchronic-1.6.3 lib/asynchronic/garbage_collector.rb
asynchronic-1.6.2 lib/asynchronic/garbage_collector.rb
asynchronic-1.6.1 lib/asynchronic/garbage_collector.rb
asynchronic-1.6.0 lib/asynchronic/garbage_collector.rb
asynchronic-1.5.2 lib/asynchronic/garbage_collector.rb
asynchronic-1.5.1 lib/asynchronic/garbage_collector.rb
asynchronic-1.5.0 lib/asynchronic/garbage_collector.rb