Class: JIJI::Command

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

Overview

コマンドラインツール

Constant Summary collapse

JIJI_DIR_NAME =

設定ファイル置き場

"~/.jiji"

Instance Method Summary collapse

Instance Method Details

#run(args) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/jiji/command.rb', line 129

def run( args )
  case  args[0]
    when "start"; start
    when "stop";  stop
    when "setting"; setting
    when "restart"
      stop
      start
    else
      name = File.basename( File.expand_path( $0 ))
      puts "usage : #{name} ( setting | start | stop | restart )"
  end
end

#settingObject

初期化



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
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
# File 'lib/jiji/command.rb', line 49

def setting
  h = HighLine.new
  
  # アクセス先証券会社
  JIJI::Plugin::Loader.new.load
  mng = JIJI::Plugin::SecuritiesPluginManager.new
  plugins = mng.all
  index = 0
  str = plugins.map {|p| "#{index+=1} : #{p.display_name.to_s}" }.join( "\n    " ) 
  value = h.ask("> Please select a securities.\n    " + str)
  unless value =~ /\d+/
    puts "[ERROR] setting failed.( Illegal value. vlaue=#{value} )"
    return
  end
  begin
    type = plugins[value.to_i-1]
    unless type
      puts "[ERROR] setting failed.( Illegal value. vlaue=#{value} )"
      return
    end
  rescue Exception
    puts "[ERROR] setting failed.( Illegal value. vlaue=#{value} )"
    return
  end
  
  # 入力
  values = {:type=>type.plugin_id}
  type.input_infos.each {|i|
     value = i.secure ? h.ask("> #{i.description}") {|q| q.echo = '*' } : h.ask("> #{i.description}")
     if i.validator && error = i.validator.call( value )
       puts "[ERROR] setting failed.( #{error}. value=#{value} )"
       return
     end
     values[i.key.to_sym] = value
  }
  dir  = h.ask("> Please input a data directory of jiji. (default: #{JIJI_DIR_NAME} )")
  dir = !dir || dir.empty? ? JIJI_DIR_NAME : dir

  port = h.ask('> Please input a server port. (default: 7000 )')
  port = !port || port.empty? ? "7000" : port
  unless port =~ /\d+/
    puts "[ERROR] setting failed.( illegal port number. port=#{port} )"
    return
  end

  # ディレクトリ作成
  begin
    puts ""
    ex_dir = File.expand_path(JIJI_DIR_NAME)
    mkdir ex_dir
    open( "#{ex_dir}/base", "w" ) {|f|
      f << dir
    }
    puts "create. #{ex_dir}/base"

    # ベースディレクトリの作成
    dir = File.expand_path(dir)
    mkdir(dir) if ( dir != ex_dir )
    mkdir("#{dir}/conf")

    # 設定ファイル
    open( "#{dir}/conf/configuration.yaml", "w" ) {|f|
      f << YAML.dump( {
        :server => { :port=>port.to_i },
        :securities => values
      } )
    }
    FileUtils.chmod(0600, "#{dir}/conf/configuration.yaml")

    # サンプルエージェント
    copy_base_files( dir, h  )
  rescue Exception
    puts "[ERROR] setting failed.(#{$!.to_s})"
    return
  end

  puts "Setting was completed!"
end

#startObject

サービスを開始



20
21
22
23
24
25
26
27
28
29
# File 'lib/jiji/command.rb', line 20

def start
  begin
    puts "jiji started."
    s = JIJI::FxServer.new( data_dir )
    s.start
  rescue Exception
    puts "[ERROR] start failed.(#{$!.to_s})"
    return
  end
end

#stopObject

サービスを停止



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jiji/command.rb', line 32

def stop
  begin
    host = ARGV[1] || "localhost"
    conf = JIJI::Registry.new(data_dir)[:conf]
    port = conf.get([:server,:port], 7000).to_i
    service = JSONBroker:: JsonRpcRequestor.new( "system", "http://#{host}:#{port}" )
    service.shutdown
    sleep 10 # 停止完了を待つ。
    puts "jiji stopped."
  rescue Exception
    puts "[ERROR] stop failed.(#{$!.to_s})"
    return
  end

end