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
29
30
31
32
33
# File 'lib/jiji/process_manager.rb', line 11

def initialize( registry )
  @registry = registry
  @executor = registry.back_test_process_executor
  @rmt = @registry.rmt_process
  @back_tests = {}
  @logger = registry.server_logger

  # 既存のバックテスト一覧を読み込む
  Dir.glob( "#{registry.process_dir}/*" ) {|d|
    next unless File.directory? d
    next unless File.exist? "#{d}/#{JIJI::ProcessInfo::PROPERTY_FILE}"
    begin
      id = File.basename(d)
      next if id == "rmt"
      info = @registry.process_info( id )
      process = @registry.process_info(id)
      process.load # 保存しておいた設定情報をロード
      @back_tests[id] = process
    rescue
      @logger.error $!
    end
  }
end

Instance Attribute Details

#confObject

Returns the value of attribute conf.



149
150
151
# File 'lib/jiji/process_manager.rb', line 149

def conf
  @conf
end

#executorObject

Returns the value of attribute executor.



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

def executor
  @executor
end

#registryObject

Returns the value of attribute registry.



148
149
150
# File 'lib/jiji/process_manager.rb', line 148

def registry
  @registry
end

#rmtObject

Returns the value of attribute rmt.



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

def rmt
  @rmt
end

Instance Method Details

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

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jiji/process_manager.rb', line 98

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
  }
  return start_process( id, props )
end

#delete_back_test(id) ⇒ Object

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



139
140
141
142
143
144
145
146
# File 'lib/jiji/process_manager.rb', line 139

def delete_back_test( id )
  unless id == "rmt" || @back_tests.key?(id)
    raise UserError.new( JIJI::ERROR_NOT_FOUND, "process not found")
  end
  @executor.delete(id)
  @back_tests.delete( id )
  FileUtils.rm_rf "#{@registry.process_dir}/#{id}"
end

#each(&block) ⇒ Object

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



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

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

#get(id) ⇒ Object

プロセスを取得する



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

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

#restart_test(id, agent_properties = nil) ⇒ Object

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



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

def restart_test( id, agent_properties=nil )
  unless id == "rmt" || @back_tests.key?(id)
    raise UserError.new( JIJI::ERROR_NOT_FOUND, "process not found")
  end
  p = get(id)
  new_id = UUIDTools::UUID.random_create().to_s # 別のIDを再割り当てする。
  props = {
    "id"=>new_id,
    "name"=>p["name"],
    "memo"=>p["memo"],
    "create_date"=>Time.now.to_i,
    "start_date"=>p["start_date"],
    "end_date"=>p["end_date"],
    "agents"=>agent_properties || p["agents"],
    "state"=>:WAITING
  }
  result = start_process( new_id, props )
  delete_back_test( id )
  return result
end

#set(process_id, setting) ⇒ Object

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jiji/process_manager.rb', line 71

def set( process_id, setting )
  p = get( process_id )
  failed = {}
  setting.each_pair {|k,v|
    case k
      when "trade_enable"
        # バックテストの変更は許可しない。
        unless  process_id == "rmt"
          raise UserError.new( JIJI::ERROR_ILLEGAL_ARGUMENTS, "illegal id.id=#{process_id}" )
        end
        value = ( v == "true" || v == true )
        @rmt.trade_enable = value
        @rmt.info["trade_enable"] = value
      when "agents"
        p = get( process_id )
        if ( process_id == "rmt" )
          failed = @rmt.set_agents(v)
        else
          failed = @executor.set_agents(process_id, v)
        end
        p["agents"] = create_success_agent_setting( p["agents"], v, failed )
    end
  }
  failed
end

#startObject

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



36
37
38
39
40
41
42
43
44
45
# File 'lib/jiji/process_manager.rb', line 36

def start
  if @conf.get([:collector,:collect], true )
    begin
      @rmt.load_agents( true )
      @rmt.start
    rescue
      @rmt.logger.error $!
    end
  end
end

#stopObject

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



48
49
50
51
52
53
# File 'lib/jiji/process_manager.rb', line 48

def stop
  @stoped = true
  @registry.operator( "rmt", false, nil).stop
  @rmt.stop
  @executor.stop
end