Sha256: df035a04ba78e820edd51402b67fba5fbcc3968fb109d3d32db84b00d19bd669

Contents?: true

Size: 1.74 KB

Versions: 18

Compression:

Stored size: 1.74 KB

Contents

module Naf
  class MachineRunner < NafBase
    # Protect from mass-assignment issue
    attr_accessible :machine_id,
                    :runner_cwd

    #---------------------
    # *** Associations ***
    #+++++++++++++++++++++

    belongs_to :machine,
      class_name: '::Naf::Machine'
    has_many :machine_runner_invocations,
      class_name: '::Naf::MachineRunnerInvocation'

    #--------------------
    # *** Validations ***
    #++++++++++++++++++++

    validates :machine_id,
              :runner_cwd, presence: true
    validates :machine_id, uniqueness: { scope: :runner_cwd }

    #----------------------
    # *** Class Methods ***
    #++++++++++++++++++++++

    def self.enabled
      joins(:machine).
      where("#{::Naf.schema_name}.machines.enabled IS TRUE")
    end

    def self.running
      joins(:machine_runner_invocations).
      where("#{::Naf.schema_name}.machine_runner_invocations.dead_at IS NULL AND
        #{::Naf.schema_name}.machine_runner_invocations.wind_down_at IS NULL")
    end

    def self.winding_down
      joins(:machine_runner_invocations).
      where("#{::Naf.schema_name}.machine_runner_invocations.dead_at IS NULL AND
        #{::Naf.schema_name}.machine_runner_invocations.wind_down_at IS NOT NULL")
    end

    def self.dead_count
      (::Naf::MachineRunner.joins(:machine).where("#{::Naf.schema_name}.machines.enabled IS TRUE").pluck(:machine_id) -
        ::Naf::MachineRunner.running.pluck(:machine_id) -
        ::Naf::MachineRunner.winding_down.pluck(:machine_id)).uniq.count
    end

    #-------------------------
    # *** Instance Methods ***
    #+++++++++++++++++++++++++

    def current_invocation
      machine_runner_invocations.where(dead_at: nil, wind_down_at: nil).order(:id).last
    end

  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
naf-2.1.13 app/models/naf/machine_runner.rb
naf-2.1.12 app/models/naf/machine_runner.rb
naf-2.1.11 app/models/naf/machine_runner.rb
naf-2.1.10 app/models/naf/machine_runner.rb
naf-2.1.9 app/models/naf/machine_runner.rb
naf-2.1.8 app/models/naf/machine_runner.rb
naf-2.1.6 app/models/naf/machine_runner.rb
naf-2.1.5 app/models/naf/machine_runner.rb
naf-2.1.4 app/models/naf/machine_runner.rb
naf-2.1.3 app/models/naf/machine_runner.rb
naf-2.1.2 app/models/naf/machine_runner.rb
naf-2.1.1 app/models/naf/machine_runner.rb
naf-2.1.0 app/models/naf/machine_runner.rb
naf-2.0.4 app/models/naf/machine_runner.rb
naf-2.0.3 app/models/naf/machine_runner.rb
naf-2.0.2 app/models/naf/machine_runner.rb
naf-2.0.1 app/models/naf/machine_runner.rb
naf-2.0.0 app/models/naf/machine_runner.rb