Class: JIJI::ProcessManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ ProcessManager

Returns a new instance of ProcessManager.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jiji/process_manager.rb', line 11

def initialize( registry )
  @registry = registry
  @rmt = @registry.rmt_process
  @back_tests = {}

  @mutex = Mutex.new
  @running = nil # 実行中のテスト
  @waiting = [] # 待機中のテスト

  # 既存のバックテスト一覧を読み込む
  Dir.glob( "#{registry.process_dir}/*" ) {|d|
    next unless File.directory? d
    next unless File.exist? "#{d}/props.yaml"
    id = File.basename(d)
    next if id == "rmt"
    @back_tests[id] = @registry.backtest_process(id, nil )
  }
end

Instance Attribute Details

#confObject

Returns the value of attribute conf.



194
195
196
# File 'lib/jiji/process_manager.rb', line 194

def conf
  @conf
end

#registryObject

Returns the value of attribute registry.



193
194
195
# File 'lib/jiji/process_manager.rb', line 193

def registry
  @registry
end

Instance Method Details

#create_back_test(name, memo, start_date, end_date, agent_properties) ⇒ Object

バックテストプロセスを新規に作成する



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/jiji/process_manager.rb', line 87

def create_back_test( name, memo, start_date, end_date, agent_properties )

  id = UUIDTools::UUID.random_create().to_s

  # プロパティを記録
  props = {
    "id"=>id,
    "name"=>name,
    "memo"=>memo,
    "create_date"=>Time.now.to_i,
    "start_date"=>start_date.to_i,
    "end_date"=>end_date.to_i,
    "agents"=>agent_properties,
    "state"=>:WAITING
  }
  begin
    btp = @registry.backtest_process(id, props )
    @mutex.synchronize {
      if @running == nil
        @running = btp
        @running.collector.listeners << self
        @running.start
      else
        @waiting << btp
      end
    }
  rescue Exception
    begin
      btp.stop
    rescue Exception
    ensure
      FileUtils.rm_rf "#{@registry.process_dir}/#{id}"
    end
    raise $!
  end

  @back_tests[id] = btp
  return {
   "id"=>id,
   "name"=>name,
   "create_date"=>Time.now.to_i
  }
end

#delete_back_test(id) ⇒ Object

バックテストプロセスを削除するログファイルもすべて削除。



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/jiji/process_manager.rb', line 156

def delete_back_test( id )
  unless id == "rmt" || @back_tests.key?(id)
    raise UserError.new( JIJI::ERROR_NOT_FOUND, "process not found")
  end
  @mutex.synchronize {
    if @running != nil && @running["id"] == id
      @running.collector.listeners.delete(self)
      @running.stop 
      unless @waiting.empty?
        @running = @waiting.shift
        @running.collector.listeners << self
        @running.start
      else
        @running = nil
      end
    else
      # 待機中であればキューから除外。
      @waiting = @waiting.reject{|i| i.id == id }
    end
  }
  @back_tests.delete( id )
  FileUtils.rm_rf "#{@registry.process_dir}/#{id}"
end

#each(&block) ⇒ Object

バックテストプロセスを列挙する



57
58
59
60
61
# File 'lib/jiji/process_manager.rb', line 57

def each( &block )
  @back_tests.each_pair {|k,v|
    yield v
  }
end

#get(id) ⇒ Object

プロセスを取得する



64
65
66
67
68
69
# File 'lib/jiji/process_manager.rb', line 64

def get( id )
  unless id == "rmt" || @back_tests.key?(id)
    raise UserError.new( JIJI::ERROR_NOT_FOUND, "process not found")
  end
  id == "rmt" ? @rmt : @back_tests[id]
end

#on_finished(state, now) ⇒ Object

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



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/jiji/process_manager.rb', line 181

def on_finished( state, now )
  @mutex.synchronize {
    unless @waiting.empty?
      @running = @waiting.shift
      @running.collector.listeners << self
      @running.start
    else
      @running = nil
    end
  }
end

#restart_test(id, agent_properties) ⇒ Object

バックテストプロセスを再起動する



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/jiji/process_manager.rb', line 132

def restart_test( id, agent_properties )
  unless id == "rmt" || @back_tests.key?(id)
    raise UserError.new( JIJI::ERROR_NOT_FOUND, "process not found")
  end
  p = get(id)
  FileUtils.rm_rf back_test_dir(id)
  props = {
    "id"=>id,
    "name"=>p["name"],
    "memo"=>p["memo"],
    "create_date"=>p["create_date"],
    "start_date"=>p["start_date"],
    "end_date"=>p["end_date"],
    "agent_properties"=>agent_properties
  }
  btp = @registry.back_test_process(id, props )
  btp.start

  @back_tests[id] = btp
  id
end

#set(process_id, setting) ⇒ Object

プロセスの設定を更新する。



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

def set( process_id, setting )
  p = get( process_id );
  setting.each_pair {|k,v|
    case k
      when "name"
        p["name"] = v
      when "trade_enable"
        p["trade_enable"] = v if process_id == "rmt" # バックテストの変更は許可しない。
      when "agents"
        p["agents"] = v
    end
  }
end

#startObject

RMTプロセスをスタートする



31
32
33
34
35
# File 'lib/jiji/process_manager.rb', line 31

def start
  if @conf.get([:collector,:collect], true )
    @rmt.start
  end
end

#stopObject

すべてのプロセスを停止する



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jiji/process_manager.rb', line 38

def stop
  @stoped = true
  @registry.operator( "rmt", false, nil).stop
  @rmt.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