Class: JIJI::BackTestProcessExecutor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBackTestProcessExecutor

Returns a new instance of BackTestProcessExecutor.



189
190
191
192
193
# File 'lib/jiji/process_manager.rb', line 189

def initialize
  @running = nil
  @mutex = Mutex.new
  @waiting = []
end

Instance Attribute Details

#registryObject

Returns the value of attribute registry.



275
276
277
# File 'lib/jiji/process_manager.rb', line 275

def registry
  @registry
end

Instance Method Details

#<<(info) ⇒ Object

実行対象とするプロセスを追加する



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/jiji/process_manager.rb', line 196

def <<(info)
  begin
    btp = @registry.backtest_process(info )
    btp.load_agents( false )
    @mutex.synchronize {
      if @running == nil
        @running = btp
        @running.collector.listeners << self
        @running.start
      else
        @waiting << btp
      end
    }
  rescue Exception
    begin
      btp.stop if btp
    rescue Exception
    ensure
      FileUtils.rm_rf "#{@process_dir}/#{info.process_id}"
    end
    raise $!
  end
end

#delete(id) ⇒ Object

プロセスを削除する



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/jiji/process_manager.rb', line 237

def delete(id)
  @mutex.synchronize {
    if @running != nil && @running.info.process_id == id
      #実行中であれば停止して次に
      @running.collector.listeners.delete(self)
      @running.stop
      run_next
    else
      # 待機中であればキューから除外。
      @waiting = @waiting.reject{|i| i.info.process_id == id }
    end
  }
end

#get(id) ⇒ Object

idに対応するプロセスがあれば取得する



260
261
262
263
264
265
266
# File 'lib/jiji/process_manager.rb', line 260

def get(id)
  if @running != nil && @running.info.process_id == id
    return @running
  else
    return @waiting.find{|p|p.info.process_id == id}
  end
end

#on_finished(state, time) ⇒ Object

プロセスの実行終了通知を受け取る



269
270
271
272
273
# File 'lib/jiji/process_manager.rb', line 269

def on_finished(state, time)
  @mutex.synchronize {
    run_next
  }
end

#set_agents(id, agents) ⇒ Object

エージエントの設定を更新する



252
253
254
255
256
257
# File 'lib/jiji/process_manager.rb', line 252

def set_agents(id, agents)
  @mutex.synchronize {
    p = get(id)
    p ? p.set_agents(agents) : {}
  }
end

#stopObject

全てのプロセスの実行を停止する



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/jiji/process_manager.rb', line 221

def stop
  @mutex.synchronize {
    @waiting.each {|i|
      i.collector.listeners.delete(self)
      i.stop
    }
    @waiting.clear
    if @running != nil
      @running.collector.listeners.delete(self)
      @running.stop
      @running = nil
    end
  }
end