Sha256: 5c5465eb194e02f6c9e35967b41268d18a3f8efac7571e417d5d84ad3aa45bf0

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module SoarThreadWorker
  class ThreadWorker
    attr_accessor :event_handler
    attr_accessor :thread

    def self.create_worker(event_handler)
      ThreadWorker.new(event_handler)
    end

    def self.error(message)
      $stderr.puts "ERROR [ThreadWorker] #{message}"
    end

    def initialize(event_handler: nil)
      @api_mutex = Mutex.new
      @stopping = false
      @event_handler = event_handler
    end

    def running?
      (not @thread.nil?) and @thread.alive?
    end

    def start
      @api_mutex.synchronize {
        begin
          return if (running?)
          @stopping = false
          create_thread()
        rescue Exception => e
          ThreadWorker::error("Exception #{e} in start")
          raise
        end
      }
    end

    def stop(immediate: false)
      @api_mutex.synchronize {
        @stopping = true
        @thread.kill if (not @thread.nil?) and immediate
      }
    end

    def execute
      #Inversion of control: override this method to do the work you need.
      #Return true if after execution the thread should stop, false if it
      #should continue running
      false
    end

    private

    def run
      while not @stopping
        stop if execute
        be_nice
      end
    end

    def create_thread
      @thread = Thread.new do
        Thread.abort_on_exception=true
        run
      end
    end

    def be_nice
      sleep(0.01)
      Thread.pass
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
soar_thread_worker-0.2.1 lib/soar_thread_worker/thread_worker.rb