Sha256: f28adb8a39c3fa35422adb130908f0e8b235b52db5eac90c8dd50b418e85c33b

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 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
      (::Naf::MachineRunner.joins(:machine).where("#{::Naf.schema_name}.machines.enabled IS TRUE").pluck(:machine_id) -
        ::Naf::MachineRunner.running.pluck(:machine_id)).uniq
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
naf-1.1.4 app/models/naf/machine_runner.rb
naf-1.1.3 app/models/naf/machine_runner.rb
naf-1.1.2 app/models/naf/machine_runner.rb
naf-1.1.1 app/models/naf/machine_runner.rb
naf-1.1.0 app/models/naf/machine_runner.rb