Class: WorkflowManager::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/workflow_manager/server.rb

Defined Under Namespace

Classes: Config, KyotoDB, PStoreDB

Constant Summary collapse

@@config =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



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
# File 'lib/workflow_manager/server.rb', line 82

def initialize
  @interval = config.interval
  @resubmit = config.resubmit
  extension = NO_KYOTO ? '.pstore' : '.kch'
  db_mode = NO_KYOTO ? 'PStore' : 'KyotoCabinet'
  @db_stat = File.join(config.db_dir, 'statuses'+extension)
  @db_logs  = File.join(config.db_dir, 'logs'+extension)

  @log_dir = File.expand_path(config.log_dir)
  @db_dir  = File.expand_path(config.db_dir)
  FileUtils.mkdir_p @log_dir unless File.exist?(@log_dir)
  FileUtils.mkdir_p @db_dir unless File.exist?(@db_dir)
  #@statuses = KyotoCabinet::DB.new
  @statuses = NO_KYOTO ? PStoreDB.new(@db_stat) : KyotoDB.new(@db_stat)
  #@logs = KyotoCabinet::DB.new 
  @logs = NO_KYOTO ? PStoreDB.new(@db_logs) : KyotoDB.new(@db_logs)
  @system_log = File.join(@log_dir, "system.log")
  @mutex = Mutex.new
  @cluster = config.cluster
  puts("DB = #{db_mode}")
  puts("Cluster = #{@cluster.name}")
  log_puts("DB = #{db_mode}")
  log_puts("Cluster = #{@cluster.name}")
  log_puts("Server starts")
end

Class Method Details

.configObject



40
41
42
# File 'lib/workflow_manager/server.rb', line 40

def self.config
  @@config
end

.config=(config) ⇒ Object



37
38
39
# File 'lib/workflow_manager/server.rb', line 37

def self.config=(config)
  @@config = config
end

.configure {|@@config| ... } ⇒ Object

Yields:



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/workflow_manager/server.rb', line 46

def self.configure
  @@config = Config.new
  # default values
  @@config.log_dir  = LOG_DIR
  @@config.db_dir   = DB_DIR
  @@config.interval = INTERVAL  # interval to check jobs, [s]
  @@config.resubmit = RESUBMIT  # how many times at maximum to resubmit when job fails
  yield(@@config)
  if @@config.cluster
    @@config.cluster.log_dir = File.expand_path(@@config.log_dir)
  end
  @@config
end

Instance Method Details

#configObject



43
44
45
# File 'lib/workflow_manager/server.rb', line 43

def config
  @@config ||= WorkflowManager.configure{}
end

#copy_commands(org_dir, dest_parent_dir) ⇒ Object



110
111
112
# File 'lib/workflow_manager/server.rb', line 110

def copy_commands(org_dir, dest_parent_dir)
  @cluster.copy_commands(org_dir, dest_parent_dir)
end

#get_log(job_id, with_err = false) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/workflow_manager/server.rb', line 249

def get_log(job_id, with_err=false)
  log_file = nil
  @logs.transaction do |logs|
    log_file = logs[job_id.to_s]
  end
  log_data = if log_file and File.exist?(log_file)
               "__STDOUT LOG__\n\n" + File.read(log_file)
             else
               'no log file'
             end
  if with_err
    err_file = log_file.gsub(/_o\.log/,'_e.log')
    if err_file and File.exist?(err_file)
      log_data << "\n\n__STDERR LOG__\n\n"
      log_data << File.read(err_file)
    end
  end
  log_data
end

#get_script(job_id) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/workflow_manager/server.rb', line 268

def get_script(job_id)
  script_file = nil
  @logs.transaction do |logs|
    script_file = logs[job_id.to_s]
  end
  if script_file
    script_file = script_file.gsub(/_o\.log/,'')
  end
  script = if script_file and File.exist?(script_file)
             File.read(script_file)
           else
             'no script file'
           end
  script
end

#helloObject



107
108
109
# File 'lib/workflow_manager/server.rb', line 107

def hello
  'hello, '+ @cluster.name
end

#job_list(with_results = false, project_number = nil) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/workflow_manager/server.rb', line 232

def job_list(with_results=false, project_number=nil)
  s = []
  #@statuses.open(@db_stat)
  @statuses.transaction do |statuses|
    statuses.each do |key, value|
      if project_number 
        if x = value.split(/,/)[4].to_i==project_number.to_i
          s << [key, value]
        end
      else
        s << [key, value]
      end
    end
  #@statuses.close
  end
  s.sort.reverse.map{|v| v.join(',')}.join("\n")
end

#log_puts(str) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/workflow_manager/server.rb', line 113

def log_puts(str)
  time = Time.now.strftime("[%Y.%m.%d %H:%M:%S]")
  @mutex.synchronize do
     open(@system_log, "a") do |out|
       out.print time + " " + str + "\n"
     end
  end
end

#start_monitoring(submit_command, user = 'sushi lover', resubmit = 0, script = '', project_number = 0, sge_options = '', log_dir = '') ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/workflow_manager/server.rb', line 121

def start_monitoring(submit_command, user = 'sushi lover', resubmit = 0, script = '', project_number = 0, sge_options='', log_dir = '')
  log_puts("monitoring: script=" + submit_command + " user=" + user + " resubmit=" + resubmit.to_s + " project=" + project_number.to_s + " sge option=" + sge_options + " log dir=" + log_dir.to_s)

  #warn submit_command
  #
  # TODO: analyze arguments
  #
  job_id, log_file, command = @cluster.submit_job(submit_command, script, sge_options)
  log_puts("submit: " + job_id + " " + command)

  #
  # monitor worker
  #
  if job_id and log_file
    monitor_worker = Thread.new(job_id, log_file, submit_command, user, resubmit, script, project_number, sge_options, log_dir) do |t_job_id, t_log_file, t_submit_command, t_user, t_resubmit, t_script, t_project_number, t_sge_options, t_log_dir|
      loop do
        status = success_or_fail(t_job_id, t_log_file) 
        script_name = File.basename(submit_command).split(/-/).first
        #@statuses.open(@db_stat)
        @statuses.transaction do |statuses|
        #start_time = if stat = @statuses[t_job_id] and stat = stat.split(/,/) and time = stat[2]
          start_time = if stat = statuses[t_job_id] and stat = stat.split(/,/) and time = stat[2]
                         time
                       end
          time = if start_time 
                   if status == 'success' or status == 'fail'
                     start_time + '/' + Time.now.strftime("%Y-%m-%d %H:%M:%S")
                   else
                     start_time
                   end
                 else
                   Time.now.strftime("%Y-%m-%d %H:%M:%S")
                 end
        #@statuses[t_job_id] = [status, script_name, time, user, project_number].join(',')
          statuses[t_job_id] = [status, script_name, time, user, project_number].join(',')
        #@statuses.close
        end
        @logs.transaction do |logs|
          logs[t_job_id] = t_log_file
        end
        #warn t_job_id + " " + status
        if status == 'success'
          log_puts(status + ": " + t_job_id)
          unless t_log_dir.empty?
            copy_commands(t_log_file, t_log_dir).each do |command|
              log_puts(command)
              system command
            end
            err_file = t_log_file.gsub('_o.log','_e.log')
            copy_commands(err_file, t_log_dir).each do |command|
              log_puts(command)
              system command
            end
          end
          Thread.current.kill
        elsif status == 'fail'
          log_puts(status + ": " + t_job_id)
          #
          # TODO: re-submit
          #
          if t_resubmit < RESUBMIT
            log_puts("resubmit: " + t_job_id)
            resubmit_job_id = start_monitoring(t_submit_command, t_user, t_resubmit + 1, t_script, t_project_number, t_sge_options)
            script_name = File.basename(submit_command).split(/-/).first
            #@statuses.open(@db_stat)
            @statuses.transaction do |statuses|
              statuses[t_job_id] = ["resubmit: " + resubmit_job_id.to_s, script_name, Time.now.strftime("%Y-%m-%d %H:%M:%S"), t_user, t_project_number].join(',')
            #@statuses.close
            end
          else
            log_puts("fail: " + t_job_id)
          end
          unless t_log_dir.empty?
            copy_commands(t_log_file, t_log_dir).each do |command|
              log_puts(command)
              system command
            end
            err_file = t_log_file.gsub('_o.log','_e.log')
            copy_commands(err_file, t_log_dir).each do |command|
              log_puts(command)
              system command
            end
          end
          Thread.current.kill
        end
        sleep @interval
      end 
    end
    job_id.to_i
  end
end

#status(job_id, new_status = nil) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/workflow_manager/server.rb', line 212

def status(job_id, new_status=nil)
  stat = nil
  #@statuses.open(@db_stat)
  @statuses.transaction do |statuses|
    if new_status and stat = statuses[job_id.to_s]
      status_list = ['success', 'running', 'fail']
      if status_list.include?(new_status)
        items = stat.split(/,/)
        items.shift
        items.unshift(new_status)
        stat = items.join(',')
        statuses[job_id.to_s] = stat
      end
    else
      stat = statuses[job_id.to_s]
    end
  end
  #@statuses.close
  stat
end

#success_or_fail(job_id, log_file) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/workflow_manager/server.rb', line 283

def success_or_fail(job_id, log_file)
  job_running = @cluster.job_running?(job_id)
  job_ends = @cluster.job_ends?(log_file)
  msg = if job_running
          'running'
        elsif job_ends
          'success'
        else
          'fail'
        end
  msg
end