Class: JIJI::Service::AgentService

Inherits:
Object
  • Object
show all
Includes:
AgentUtil
Defined in:
lib/jiji/service/agent_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AgentUtil

#safe

Instance Attribute Details

#agent_dirObject

Returns the value of attribute agent_dir.



208
209
210
# File 'lib/jiji/service/agent_service.rb', line 208

def agent_dir
  @agent_dir
end

#agent_registryObject

Returns the value of attribute agent_registry.



206
207
208
# File 'lib/jiji/service/agent_service.rb', line 206

def agent_registry
  @agent_registry
end

#daoObject

Returns the value of attribute dao.



210
211
212
# File 'lib/jiji/service/agent_service.rb', line 210

def dao
  @dao
end

#process_managerObject

Returns the value of attribute process_manager.



207
208
209
# File 'lib/jiji/service/agent_service.rb', line 207

def process_manager
  @process_manager
end

#server_loggerObject

Returns the value of attribute server_logger.



211
212
213
# File 'lib/jiji/service/agent_service.rb', line 211

def server_logger
  @server_logger
end

#shared_lib_dirObject

Returns the value of attribute shared_lib_dir.



209
210
211
# File 'lib/jiji/service/agent_service.rb', line 209

def shared_lib_dir
  @shared_lib_dir
end

Instance Method Details

#add_file(path, body) ⇒ Object

ファイルを追加する



52
53
54
55
56
57
# File 'lib/jiji/service/agent_service.rb', line 52

def add_file( path, body )
  check_path path
  @dao.add( path, body )
  @agent_registry.load( path )
  :success
end

#get_file(path) ⇒ Object

ファイルの内容を取得する



23
24
25
26
# File 'lib/jiji/service/agent_service.rb', line 23

def get_file( path )
  check_path path
  @dao.get( path )
end

#list_agent(process_id) ⇒ Object

プロセスに登録されているエージェントの一覧を得る



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/jiji/service/agent_service.rb', line 171

def list_agent( process_id )
  p = process_manager.get( process_id )
  safe(4) {
    p.agent_manager.collect.map! {|entry|
      props = entry[1].agent.properties
      {
        :name=>entry[0],
        :properties=>entry[1].agent.property_infos.map! {|info|
          prop = props.find() {|e| e[0] == info.id.to_s }
          { :id=>info.id.to_s, :info=>info, :value=> prop ? prop[1] : nil }
        },
        :description=>entry[1].agent.description,
        :active=>entry[1].active
      }
    }
  }
end

#list_agent_classObject

エージェントの一覧を取得する



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jiji/service/agent_service.rb', line 30

def list_agent_class
  @agent_registry.inject([]) {|list,name|
    next unless name =~ /([^@]+)@([^@]+)/
    list << {
      :class_name=>$1,
      :file_name=>$2,
      :properties=>@agent_registry.get_property_infos(name),
      :description=>@agent_registry.get_description(name)
    }
    list
  }
end

#list_files(path) ⇒ Object

ファイルの一覧を取得する



17
18
19
20
# File 'lib/jiji/service/agent_service.rb', line 17

def list_files( path )
  check_path path
  @dao.list( path )
end

#mkcol(path) ⇒ Object

フォルダを作成する



164
165
166
167
168
# File 'lib/jiji/service/agent_service.rb', line 164

def mkcol( path  )
  check_path path
  @dao.mkcol( path )
  :success
end

#move(files, to) ⇒ Object

ファイルを移動する



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
130
131
132
133
# File 'lib/jiji/service/agent_service.rb', line 98

def move( files, to  )
  check_path to
  result = {:success=>{},:failed=>{}}
  targets = {}
  files.each {|path|
    begin
      check_path path
      targets[path] =  @dao.directory?(path) \
        ? targets[path] = @dao.list( path, true ).map{|item| item[:path]} \
        : [path]
    rescue Exception
      result[:failed][path] = mk_error_info(path,$!)
    end
  }
  targets.each_pair {|path,files|
    begin
      @dao.move( from, to )
      result[:success][path] = {:path=>path}
    rescue Exception
      result[:failed][path] = mk_error_info(path,$!)
      next
    end
    begin
      files.each {|f|
        @agent_registry.unload(f)
      }
      @dao.list( "#{to}/#{File.basename(from)}", true ) {|f|
        @agent_registry.load(f)
      }
    rescue Exception
      #ロードで発生したエラーはログ出力のみして握る
      server_logger.error $!
    end
  }
  result
end

#off(process_id, name) ⇒ Object

プロセスのエージェントを一時的に無効化する



190
191
192
193
194
# File 'lib/jiji/service/agent_service.rb', line 190

def off( process_id, name )
  p = process_manager.get( process_id )
  p.agent_manager.off( name )
  :success
end

#on(process_id, name) ⇒ Object

プロセスのエージェントの無効化を解除する



196
197
198
199
200
# File 'lib/jiji/service/agent_service.rb', line 196

def on( process_id, name )
  p = process_manager.get( process_id )
  p.agent_manager.on( name )
  :success
end

#on?(process_id, name) ⇒ Boolean

プロセスのエージェントの無効化状態を取得する

Returns:

  • (Boolean)


202
203
204
205
# File 'lib/jiji/service/agent_service.rb', line 202

def on?( process_id, name )
  p = process_manager.get( process_id )
  p.agent_manager.on?( name )
end

#put_file(path, body) ⇒ Object

ファイルを追加/更新する



44
45
46
47
48
49
# File 'lib/jiji/service/agent_service.rb', line 44

def put_file( path, body )
  check_path path
  @dao.put( path, body )
  @agent_registry.load( path )
  :success
end

#remove(paths) ⇒ Object

ファイルを削除する



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
# File 'lib/jiji/service/agent_service.rb', line 60

def remove( paths )
  result = {:success=>{},:failed=>{}}
  targets = {}
  paths.each {|path|
    begin
      check_path path
      # agents, sharedは削除できない
      if path =~ /^(#{@agent_dir}|#{@shared_lib_dir})$/
        raise JIJI::UserError.new( ERROR_ILLEGAL_NAME, "illegal path. path=#{path}" )
      end
      targets[path] =  @dao.directory?(path) \
        ? targets[path] = @dao.list( path, true ).map{|item| item[:path]} \
        : [path]
    rescue Exception
      result[:failed][path] = mk_error_info(path,$!)
    end
  }
  targets.each_pair {|path, files|
    begin
      @dao.delete( path )
      result[:success][path] = {:path=>path}
    rescue Exception
      result[:failed][path] = mk_error_info(path,$!)
      next
    end
    begin
      files.each {|f|
        @agent_registry.unload(f)
      }
    rescue Exception
      #ロードで発生したエラーはログ出力のみして握る
      server_logger.error $!
    end
  }
  result
end

#rename(path, new_name) ⇒ Object

ファイルを移動する



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/jiji/service/agent_service.rb', line 137

def rename( path, new_name )
  check_path path
  # agents, sharedはリネームできない
  if path =~ /^(#{@agent_dir}|#{@shared_lib_dir})$/
    raise JIJI::UserError.new( ERROR_ILLEGAL_NAME, "illegal path. path=#{path}" )
  end
  new_path = "#{File.dirname(path)}/#{new_name}"
  files = @dao.directory?(path) ? @dao.list( path, true ).reject{|item| 
    item[:type] == :directory 
  }.map {|item|
    item[:path]
  } : [path]
  @dao.rename( path, new_path )
  begin
    files.each {|f|
      server_logger.info f
      @agent_registry.unload(f)
      @agent_registry.load( f.sub( path, new_path ))
    }
  rescue Exception
    #ロードで発生したエラーはログ出力のみして握る
    server_logger.error $!
  end
  :success
end