Class: Node

Inherits:
Object
  • Object
show all
Includes:
GlobalID::Identification, Mongoid::Document, Mongoid::Timestamps, Mongoid::Uuid, MongoidEnumerable
Defined in:
app/models/node.rb

Defined Under Namespace

Classes: NodeConnectionError

Instance Method Summary collapse

Instance Method Details

#available_slot_with_execution_type(execution_type) ⇒ Object



36
37
38
# File 'app/models/node.rb', line 36

def available_slot_with_execution_type(execution_type)
  available_slots.find_by(execution_type: execution_type)
end

#available_slotsObject



40
41
42
# File 'app/models/node.rb', line 40

def available_slots
  slots.available
end

#destroy_slotsObject



44
45
46
# File 'app/models/node.rb', line 44

def destroy_slots
  slots.destroy_all
end

#register_error(error) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/node.rb', line 52

def register_error(error)
  Rails.logger.info("Registering error in #{self}: #{error}")

  update!(last_error: "#{error} at #{Time.zone.now}")

  if available?
    unstable!
    Rails.logger.debug("#{self} marked as unstable")
  elsif unstable?
    if unstable_period_expired?
      unavailable!
      Rails.logger.debug("#{self} marked as unavailable because the unstable period has expired (last success was at #{last_success_at}). Migrating all tasks.")
      MigrateTasksFromDeadNodeJob.perform_later(node: self)
    else
      Rails.logger.debug("#{self} still unstable until the limit period be expired (last success was at #{last_success_at})")
    end
  end
end

#register_successObject



75
76
77
78
# File 'app/models/node.rb', line 75

def register_success
  Rails.logger.debug("Registering success in #{self}")
  update!(last_success_at: Time.zone.now)
end

#run_with_lock_no_wait(&block) ⇒ Object



86
87
88
# File 'app/models/node.rb', line 86

def run_with_lock_no_wait(&block)
  LockManager.new(type: self.class.to_s, id: id, wait: false, expire: 5.minutes).lock(&block)
end

#runner_service(service) ⇒ Object



48
49
50
# File 'app/models/node.rb', line 48

def runner_service(service)
  Runners::ServicesFactory.fabricate(runner: runner_provider, service: service)
end

#to_sObject



80
81
82
83
84
# File 'app/models/node.rb', line 80

def to_s
  last_success = ", last success at #{last_success_at}" unless available?

  "Node #{name} #{uuid} #{runner_provider} (#{status}#{last_success})"
end

#unstable_period_expired?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/node.rb', line 71

def unstable_period_expired?
  last_success_at && last_success_at < Settings.node_unavailable_after_seconds.seconds.ago
end

#usage_per_execution_typeObject



32
33
34
# File 'app/models/node.rb', line 32

def usage_per_execution_type
  NodeUsagePercentagePerExecutionType.new(self).perform
end