Class: JIJI::Process

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

Overview

プロセス

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, process_dir, agent_manager, logger, props = nil, registry = nil, rmt = false) ⇒ Process

コンストラクタ再起動後の復元の場合、プロパティを指定しないこと。この場合設定ファイルからロードされる。



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jiji/process.rb', line 18

def initialize( id, process_dir, agent_manager, logger, props=nil, registry=nil, rmt=false )

  is_recreate = props == nil
  @logger = logger
  @registry = registry
  @id = id
  @agent_manager = agent_manager
  @process_dir = process_dir
  @started_mutex = Mutex.new
  @started_mutex.synchronize {
    @started= false
  }
  FileUtils.mkdir_p dir

  prop_file = "#{dir}/props.yaml"
  if !is_recreate
    @props = props
    @props["agents"] = [] unless @props.key? "agents"
    save_props
  else
    # 既存のプロセスの場合、ファイルから設定値を読み込む
    @props = {}
    lock {
      if ( File.exist? prop_file )
        @props = YAML.load_file prop_file
      end
    }
    @props["agents"] = [] unless @props.key? "agents"
  end

  # RMTまたは新規作成の場合はエージェントをロード
  # 再起動後の再作成時は、アウトプットのみ作成。
  @outputs = {}
  if rmt || !is_recreate
    load_agent(rmt) # リアル取引の再起動時は発生したエラーを無視する
  else
    if @props && @props["agents"]
      @props["agents"].each {|v|
        @outputs[v["id"]] = @registry.output( @id, v["name"] )
      }
    end
  end

  # 取引の有効状態を更新
  @agent_manager.operator.trade_enable =
    @props["trade_enable"] ? true : false

end

Instance Attribute Details

#agent_managerObject

Returns the value of attribute agent_manager.



155
156
157
# File 'lib/jiji/process.rb', line 155

def agent_manager
  @agent_manager
end

#collectorObject

Returns the value of attribute collector.



152
153
154
# File 'lib/jiji/process.rb', line 152

def collector
  @collector
end

#idObject

Returns the value of attribute id.



150
151
152
# File 'lib/jiji/process.rb', line 150

def id
  @id
end

#loggerObject

Returns the value of attribute logger.



157
158
159
# File 'lib/jiji/process.rb', line 157

def logger
  @logger
end

#observer_managerObject

Returns the value of attribute observer_manager.



153
154
155
# File 'lib/jiji/process.rb', line 153

def observer_manager
  @observer_manager
end

#outputsObject

Returns the value of attribute outputs.



156
157
158
# File 'lib/jiji/process.rb', line 156

def outputs
  @outputs
end

#process_dirObject

Returns the value of attribute process_dir.



154
155
156
# File 'lib/jiji/process.rb', line 154

def process_dir
  @process_dir
end

#propsObject

Returns the value of attribute props.



151
152
153
# File 'lib/jiji/process.rb', line 151

def props
  @props
end

#registryObject

Returns the value of attribute registry.



158
159
160
# File 'lib/jiji/process.rb', line 158

def registry
  @registry
end

Instance Method Details

#[](k) ⇒ Object



139
140
141
# File 'lib/jiji/process.rb', line 139

def [](k)
  @props[k]
end

#[]=(k, v) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/jiji/process.rb', line 116

def []=(k,v)
  if ( k == "agents" )
    # エージェントの設定が更新された
    # 削除対象を特定するため、登録済みエージェントのIDのセットを作成
    set = agent_manager.inject(Set.new) { |s, pair| s << pair[0]  }
    v.each {|item|
      # プロパティの更新 / 対応するエージェントが存在しなければ作成。
      set_agent_properties( item["id"], item["properties"], item["class"] )
      set.delete item["id"]
    }
    # Mapに含まれていないエージェントは削除
    set.each { |id|
      agent_manager.remove( id )
      outputs.delete( id )
    }
  end
  if ( k == "trade_enable" )
    # 取引の有効状態を更新
    @agent_manager.operator.trade_enable = v ? true : false
  end
  @props[k] = v
  save_props
end

#on_finished(state, now) ⇒ Object

コレクターの終了通知を受け取る



144
145
146
147
# File 'lib/jiji/process.rb', line 144

def on_finished( state, now )
  self["state"] = state
  agent_manager.flush( now )
end

#progressObject



106
107
108
109
110
111
112
113
114
# File 'lib/jiji/process.rb', line 106

def progress
  # 再起動後のバックテストは、コレクターが起動しない
  # 進捗は常に100%とする
  if !@started
    100
  else
    collector.progress
  end
end

#startObject



67
68
69
70
71
72
73
74
75
# File 'lib/jiji/process.rb', line 67

def start
  @started_mutex.synchronize {
    @started= true
    collector.listeners << self
    collector.start
  }
  # 状態を覚えておく
  self["state"] = collector.state
end

#stateObject



96
97
98
99
100
101
102
103
104
# File 'lib/jiji/process.rb', line 96

def state
  # 再起動後のバックテストは、コレクターが起動しない
  # 状態は記録されたデータから返す
  if !@started
    return props["state"] ? props["state"] : :CANCELED
  else
    return collector.state
  end
end

#stopObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jiji/process.rb', line 77

def stop
  @started_mutex.synchronize {
    if @started  # 起動していない場合は何もしない
      observer_manager.stop
      collector.stop

      # 状態を覚えておく
      self["state"] = collector.state
      @started = false
    else
      # 待機中の場合、キャンセル状態にする。
      if props["state"] == :WAITING
       self["state"] = :CANCELED
      end
    end
  }
  collector.logger.close
end