Sha256: e2f3ef16c13e126bf02f045f0fc5a0443a00a4de9ee1c385b61ab2d48bd0494a

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 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

        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
            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

5 entries across 5 versions & 1 rubygems

Version Path
asynchronic-4.0.0 lib/asynchronic/garbage_collector.rb
asynchronic-3.0.3 lib/asynchronic/garbage_collector.rb
asynchronic-3.0.2 lib/asynchronic/garbage_collector.rb
asynchronic-3.0.1 lib/asynchronic/garbage_collector.rb
asynchronic-3.0.0 lib/asynchronic/garbage_collector.rb