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



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

def self.current
  local_machine
end

.downObject



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

def self.down
  where(marked_down: true)
end

.enabledObject


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



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

def self.enabled
  where(enabled: true)
end

.hostnameObject



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

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

.include_deleted(filter) ⇒ Object



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

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)


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

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



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

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

.local_machineObject



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

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

.machine_ip_addressObject



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

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

.upObject



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

def self.up
  where(marked_down: false)
end

.with_affinity(affinity_short_name) ⇒ Object



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

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



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

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)


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

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

#hostnameObject



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

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

#is_stale?(period) ⇒ Boolean

Returns:

  • (Boolean)


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

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 *** +++++++++++++++++++++++++



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

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

#machine_loggerObject



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

def machine_logger
  af_logger(self.class.name)
end

#mark_aliveObject



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

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

#mark_checked_scheduleObject



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

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

#mark_down(by_machine) ⇒ Object



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

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



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

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



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

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



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

def mark_up
  self.marked_down = false
  save!
end

#pickle(pickler) ⇒ Object



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

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



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

def short_name_if_it_exist
  short_name || server_name
end

#to_sObject



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

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



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

def unlock_for_runner_use
  advisory_unlock
end