Skip to content

Writing Custom Conditions

Gurpartap edited this page Dec 25, 2012 · 8 revisions

Writing conditions for Cognizant is easy. Here are a few examples to follow:

Always True

module Cognizant
  class Process
    module Conditions
      class AlwaysTrue < PollCondition
        def run(pid)
          1
        end

        def check(value)
          true
        end
      end
    end
  end
end
# Implementation
process.check :always_true, :every => 2.seconds, :times => 3, :do => :restart

CPU Usage

module Cognizant
  class Process
    module Conditions
      class CpuUsage < PollCondition
        def initialize(options = {})
          @above = options[:above].to_f
        end

        def run(pid)
          Cognizant::System.cpu_usage(pid).to_f
        end

        def check(value)
          value > @above
        end
      end
    end
  end
end
# Implementation
process.check :cpu_usage, :every => 3.seconds, :above => 60, :times => 3, :do => :stop