Class: JIJI::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/observer.rb

Overview

ワーカースレッド

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(observer, logger) ⇒ Worker

Returns a new instance of Worker.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jiji/observer.rb', line 73

def initialize( observer, logger )
  @logger = logger
  @alive = true
  @alive_mutex = Mutex.new
  @q = Queue.new
  @thread = Thread.start {
    while( !@q.empty? || alive? )
      JIJI::Util.log_if_error( @logger ) {
        rates = @q.pop 
        observer.next_rates( rates ) if rates != nil
      }
    end
  }
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



102
103
104
# File 'lib/jiji/observer.rb', line 102

def thread
  @thread
end

Instance Method Details

#alive=(value) ⇒ Object



95
96
97
98
99
100
# File 'lib/jiji/observer.rb', line 95

def alive=(value)
  @alive_mutex.synchronize {
    @alive = value
    @q.push nil # 眠れるスレッドをたたき起こす。
  }
end

#alive?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/jiji/observer.rb', line 90

def alive?
  @alive_mutex.synchronize {
    @alive
  }
end

#next_rates(rates) ⇒ Object



87
88
89
# File 'lib/jiji/observer.rb', line 87

def next_rates( rates )
  @q.push rates
end