Class: Myreplicator::Log
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Myreplicator::Log
- Defined in:
- app/models/myreplicator/log.rb
Class Method Summary collapse
-
.clear_deads ⇒ Object
Clear all logs marked running that are not running.
- .clear_olds ⇒ Object
-
.clear_stucks ⇒ Object
Clear all logs marked running that have run for more than 2 hours.
-
.completed?(*args) ⇒ Boolean
Gets a jobtype, file and export_id returns true if the job is completed.
-
.run(*args) ⇒ Object
Creates a log object Stores the state and related information about the job File’s names are supposed to be unique.
Instance Method Summary collapse
-
#kill ⇒ Object
Kills the job if running Using PID.
- #mark_dead ⇒ Object
-
#running? ⇒ Boolean
Checks to see if the PID of the log is active or not.
Class Method Details
.clear_deads ⇒ Object
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_olds ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/models/myreplicator/log.rb', line 153 def self.clear_olds logs = Myreplicator::Log.where("started_at < date_sub(now(), interval 2 day)") if logs.count > 0 logs.each do |log| begin log.destroy end end end end |
.clear_stucks ⇒ Object
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
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/models/myreplicator/log.rb', line 170 def self.completed? *args = args. log = Log.where(:export_id => [:export_id], :file => [:file], :job_type => [: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 = args. .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 if !log.running? begin log.state = "running" log.save! yield log log.state = "completed" rescue Exception => e log.state = "error" log.error = e. 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. log.backtrace = e.backtrace ensure log.finished_at = Time.now log.save! end end end |
Instance Method Details
#kill ⇒ Object
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_dead ⇒ Object
188 189 190 191 |
# File 'app/models/myreplicator/log.rb', line 188 def mark_dead self.state = "dead" self.save! end |
#running? ⇒ Boolean
Checks to see if the PID of the log is active or not
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 |