Sha256: 874636f2f3bae3d85db65e1db73dfc282821f5e4e109e81a4470ba652cf9dbfe

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 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
            error_message = "#{ex.class}: #{ex.message}\n#{ex.backtrace.join("\n")}"
            Asynchronic.logger.error('Asynchronic') { error_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

3 entries across 3 versions & 1 rubygems

Version Path
asynchronic-4.0.3 lib/asynchronic/garbage_collector.rb
asynchronic-4.0.2 lib/asynchronic/garbage_collector.rb
asynchronic-4.0.1 lib/asynchronic/garbage_collector.rb