Class: Naf::Machine

Inherits:
NafBase show all
Includes:
Af::Application::SafeProxy, PgAdvisoryLocker
Defined in:
app/models/naf/machine.rb

Constant Summary collapse

IP_REGEX =
/^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$/

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NafBase

full_table_name_prefix

Class Method Details

.currentObject



111
112
113
# File 'app/models/naf/machine.rb', line 111

def self.current
  local_machine
end

.downObject



72
73
74
# File 'app/models/naf/machine.rb', line 72

def self.down
  where(marked_down: true)
end

.enabledObject


*** Class Methods *** ++++++++++++++++++++++



64
65
66
# File 'app/models/naf/machine.rb', line 64

def self.enabled
  where(enabled: true)
end

.hostnameObject



99
100
101
102
103
104
105
# File 'app/models/naf/machine.rb', line 99

def self.hostname
  begin
    Socket.gethostname
  rescue StandardError
    "local"
  end
end

.include_deleted(filter) ⇒ Object



125
126
127
128
129
130
131
# File 'app/models/naf/machine.rb', line 125

def self.include_deleted(filter)
  if !filter
    where(deleted: false)
  else
    where({})
  end
end

.is_it_time_to_check_schedules?(check_period) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
# File 'app/models/naf/machine.rb', line 119

def self.is_it_time_to_check_schedules?(check_period)
  time = Naf::Machine.last_time_schedules_were_checked

  time.nil? || time < (Time.zone.now - check_period)
end

.last_time_schedules_were_checkedObject



115
116
117
# File 'app/models/naf/machine.rb', line 115

def self.last_time_schedules_were_checked
  self.maximum(:last_checked_schedules_at)
end

.local_machineObject



107
108
109
# File 'app/models/naf/machine.rb', line 107

def self.local_machine
  where(server_address: machine_ip_address).first
end

.machine_ip_addressObject



91
92
93
94
95
96
97
# File 'app/models/naf/machine.rb', line 91

def self.machine_ip_address
  begin
    Socket::getaddrinfo(hostname, "echo", Socket::AF_INET)[0][3]
  rescue StandardError
    "127.0.0.1"
  end
end

.upObject



68
69
70
# File 'app/models/naf/machine.rb', line 68

def self.up
  where(marked_down: false)
end

.with_affinity(affinity_short_name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/naf/machine.rb', line 76

def self.with_affinity(affinity_short_name)
  sql = <<-SQL
     exists (select
               1
             from
               #{::Naf::MachineAffinitySlot.table_name} as mas
             inner join #{::Naf::Affinity.table_name} as a on
               a.id = mas.affinity_id and
               a.affinity_short_name = ?
             where
               mas.machine_id = #{::Naf::Machine.table_name}.id)
  SQL
  return where([sql, affinity_short_name])
end

Instance Method Details

#affinityObject



225
226
227
228
229
# File 'app/models/naf/machine.rb', line 225

def affinity
  return ::Naf::Affinity.
    where(affinity_classification_id: ::Naf::AffinityClassification.machine.id,
          affinity_name: self.id.to_s).first
end

#correct_server_address?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'app/models/naf/machine.rb', line 167

def correct_server_address?
  server_address.present? and IP_REGEX =~ server_address
end

#hostnameObject



247
248
249
250
251
252
253
# File 'app/models/naf/machine.rb', line 247

def hostname
  if server_name.present?
    server_name
  else
    server_address
  end
end

#is_stale?(period) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
197
# File 'app/models/naf/machine.rb', line 193

def is_stale?(period)
  # if last_seen_alive_at is nil then the runner has not been started yet -- hold off
  # claiming it is stale until the runner is run at least once.
  self.last_seen_alive_at.present? && self.last_seen_alive_at < (Time.zone.now - period)
end

#lock_for_runner_use(&block) ⇒ Object


*** Instance Methods *** +++++++++++++++++++++++++



137
138
139
# File 'app/models/naf/machine.rb', line 137

def lock_for_runner_use(&block)
  advisory_lock(&block)
end

#machine_loggerObject



145
146
147
# File 'app/models/naf/machine.rb', line 145

def machine_logger
  af_logger(self.class.name)
end

#mark_aliveObject



176
177
178
179
# File 'app/models/naf/machine.rb', line 176

def mark_alive
  self.last_seen_alive_at = Time.zone.now
  save!
end

#mark_checked_scheduleObject



171
172
173
174
# File 'app/models/naf/machine.rb', line 171

def mark_checked_schedule
  self.last_checked_schedules_at = Time.zone.now
  save!
end

#mark_down(by_machine) ⇒ Object



186
187
188
189
190
191
# File 'app/models/naf/machine.rb', line 186

def mark_down(by_machine)
  self.marked_down = true
  self.marked_down_by_machine_id = by_machine.id
  self.marked_down_at = Time.zone.now
  save!
end

#mark_machine_down(by_machine) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'app/models/naf/machine.rb', line 215

def mark_machine_down(by_machine)
  marking_at = Time.zone.now
  machine_logger.alarm "#{by_machine.try(:id)} marking #{self} as down at #{marking_at}"
  self.marked_down = true
  self.marked_down_by_machine_id = by_machine.try(:id)
  self.marked_down_at = marking_at
  save!
  mark_processes_as_dead(by_machine)
end

#mark_processes_as_dead(by_machine) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/models/naf/machine.rb', line 199

def mark_processes_as_dead(by_machine)
  ::Naf::RunningJob.where(created_at: (Time.zone.now - Naf::HistoricalJob::JOB_STALE_TIME)..Time.zone.now).
    where("request_to_terminate = false").
    started_on(self).each do |job|

    marking_at = Time.zone.now
    machine_logger.alarm "#{by_machine.try(:id)} marking #{job} as dead at #{marking_at}"
    job.request_to_terminate = job.historical_job.request_to_terminate = true
    job.marked_dead_by_machine_id = job.historical_job.marked_dead_by_machine_id = by_machine.try(:id)
    job.marked_dead_at = job.historical_job.marked_dead_at = marking_at
    job.historical_job.finished_at = marking_at
    job.save!
    job.historical_job.save!
  end
end

#mark_upObject



181
182
183
184
# File 'app/models/naf/machine.rb', line 181

def mark_up
  self.marked_down = false
  save!
end

#pickle(pickler) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
# File 'app/models/naf/machine.rb', line 235

def pickle(pickler)
  pickler.generic_pickle(self, nil, [
                                     :created_at,
                                     :updated_at,
                                     :last_checked_schedules_at,
                                     :last_seen_alive_at,
                                     :marked_down,
                                     :marked_down_by_machine_id,
                                     :marked_down_at
                                    ])
end

#short_name_if_it_existObject



231
232
233
# File 'app/models/naf/machine.rb', line 231

def short_name_if_it_exist
  short_name || server_name
end

#to_sObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/naf/machine.rb', line 149

def to_s
  components = []
  if enabled
    components << "ENABLED"
  else
    components << "DISABLED"
  end
  components << "DOWN!" if marked_down
  components << "id: #{id}"
  components << "address: #{server_address}"
  components << "name: \"#{server_name}\"" unless server_name.blank?
  components << "pool size: #{thread_pool_size}"
  components << "last checked schedules: #{last_checked_schedules_at}"
  components << "last seen: #{last_seen_alive_at}"

  return "::Naf::Machine<#{components.join(', ')}>"
end

#unlock_for_runner_useObject



141
142
143
# File 'app/models/naf/machine.rb', line 141

def unlock_for_runner_use
  advisory_unlock
end