Class: Myreplicator::Log

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/myreplicator/log.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_deadsObject

Clear all logs marked running that are not running



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/myreplicator/log.rb', line 138

def self.clear_deads
  logs = Log.where(:state => "running")
  
  if logs.count > 0
    logs.each do |log|
      begin
        Process.getpgid(log.pid) if log.hostname == Socket.gethostname
      rescue Errno::ESRCH
        log.mark_dead
      end
    end
  end
  
end

.clear_stucksObject

Clear all logs marked running that have run for more than 2 hours



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/myreplicator/log.rb', line 109

def self.clear_stucks
  runnings = Log.where(:state => "running")
  news = Log.where(:state => "new")
  logs = runnings + news
  
  if logs.count > 0
    logs.each do |log|
      
      time_start = log.started_at
      now = Time.now()
      if time_start + 2.hour < now
        begin
          Process.kill('KILL', log.pid)
          log.state = "killed"
          log.save!
        rescue Errno::ESRCH
          log.state = "dead"
          log.save!
        end  
      end
    end
  end
  
end

.completed?(*args) ⇒ Boolean

Gets a jobtype, file and export_id returns true if the job is completed

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/myreplicator/log.rb', line 157

def self.completed? *args
  options = args.extract_options!
  log = Log.where(:export_id => options[:export_id],
                  :file => options[:file],
                  :job_type => options[:job_type]).last
  #Kernel.p "===== transport_complete? log ====="
  #Kernel.p log
  if log.nil?
    #return false
    # Bug: no log of tranporter job. Using return true for now
    return true
  else
    return true if log.state == "completed"
  end
  
  return false
end

.run(*args) ⇒ Object

Creates a log object Stores the state and related information about the job File’s names are supposed to be unique



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/myreplicator/log.rb', line 22

def self.run *args
  options = args.extract_options!
  options.reverse_merge!(:started_at => Time.now,
                         :pid => Process.pid,
                         :hostname => Socket.gethostname,
                         :guid => SecureRandom.hex(5),
                         :thread_state => Thread.current.to_s,
                         :state => "new")

  log = Log.create options

  if  !log.running?
    begin
      log.state = "running"
      log.save!

      yield log

      log.state = "completed"
    rescue Exception => e
      log.state = "error"
      log.error = e.message
      log.backtrace =  e.backtrace

    ensure
      log.finished_at = Time.now
      log.save!
    end
  else
    begin
      log.state = "ignored"
    rescue Exception => e
      log.state = "error"
      log.error = e.message
      log.backtrace =  e.backtrace
    ensure
      log.finished_at = Time.now
      log.save!
    end
  end

end

Instance Method Details

#killObject

Kills the job if running Using PID



69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/myreplicator/log.rb', line 69

def kill
  return false unless hostname == Socket.gethostname
  begin
    Process.kill('KILL', pid)
    self.state = "killed"
    self.save!
  rescue Errno::ESRCH
    puts "pid #{pid} does not exist!"
    mark_dead
  end
end

#mark_deadObject



175
176
177
178
# File 'app/models/myreplicator/log.rb', line 175

def mark_dead
  self.state = "dead"
  self.save!
end

#running?Boolean

Checks to see if the PID of the log is active or not

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/myreplicator/log.rb', line 84

def running?
  logs = Log.where(:file => file, 
                   :job_type => job_type, 
                   :state => "running",
                   :export_id => export_id,
                   :hostname => hostname)
  
  if logs.count > 0
    logs.each do |log|
      begin
        Process.getpgid(log.pid)
        puts "still running #{log.file}"
        return true
      rescue Errno::ESRCH
        log.mark_dead
      end
    end
  end
  
  return false
end