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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/myreplicator/log.rb', line 99

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

.completed?(*args) ⇒ Boolean

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

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/myreplicator/log.rb', line 118

def self.completed? *args
  options = args.extract_options!
  log = Log.where(:export_id => options[:export_id],
                  :file => options[:export_id],
                  :job_type => options[:job_type]).last
  if log.nil?
    return true
  else
    return true if log.state != "running"
  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
# 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.status,
                         :state => "new")

  log = Log.create options

  unless 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.thread_state = Thread.current.status
      log.save!
    end
  end

end

Instance Method Details

#killObject

Kills the job if running Using PID



59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/myreplicator/log.rb', line 59

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

#mark_deadObject



132
133
134
135
# File 'app/models/myreplicator/log.rb', line 132

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)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/myreplicator/log.rb', line 74

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 #{filepath}"
        return true
      rescue Errno::ESRCH
        log.mark_dead
      end
    end
  end
  
  return false
end