Class: Allgood::Configuration
- Inherits:
-
Object
- Object
- Allgood::Configuration
- Defined in:
- lib/allgood/configuration.rb
Instance Attribute Summary collapse
-
#checks ⇒ Object
readonly
Returns the value of attribute checks.
-
#default_timeout ⇒ Object
Returns the value of attribute default_timeout.
Instance Method Summary collapse
- #check(name, **options, &block) ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #run_check(&block) ⇒ Object
- #should_run_check?(check) ⇒ Boolean
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
6 7 8 9 |
# File 'lib/allgood/configuration.rb', line 6 def initialize @checks = [] @default_timeout = 10 # Default timeout of 10 seconds end |
Instance Attribute Details
#checks ⇒ Object (readonly)
Returns the value of attribute checks.
3 4 5 |
# File 'lib/allgood/configuration.rb', line 3 def checks @checks end |
#default_timeout ⇒ Object
Returns the value of attribute default_timeout.
4 5 6 |
# File 'lib/allgood/configuration.rb', line 4 def default_timeout @default_timeout end |
Instance Method Details
#check(name, **options, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/allgood/configuration.rb', line 11 def check(name, **, &block) check_info = { name: name, block: block, timeout: [:timeout] || @default_timeout, options: , status: :pending } # Handle rate limiting if [:run] begin check_info[:rate] = parse_run_frequency([:run]) rescue ArgumentError => e check_info[:status] = :skipped check_info[:skip_reason] = "Invalid run frequency: #{e.}" @checks << check_info return end end # Handle environment-specific options if [:only] environments = Array([:only]) unless environments.include?(Rails.env.to_sym) check_info[:status] = :skipped check_info[:skip_reason] = "Only runs in #{environments.join(', ')}" @checks << check_info return end end if [:except] environments = Array([:except]) if environments.include?(Rails.env.to_sym) check_info[:status] = :skipped check_info[:skip_reason] = "This check doesn't run in #{environments.join(', ')}" @checks << check_info return end end # Handle conditional checks if [:if] condition = [:if] unless condition.is_a?(Proc) ? condition.call : condition check_info[:status] = :skipped check_info[:skip_reason] = "Check condition not met" @checks << check_info return end end if [:unless] condition = [:unless] if condition.is_a?(Proc) ? condition.call : condition check_info[:status] = :skipped check_info[:skip_reason] = "Check `unless` condition met" @checks << check_info return end end check_info[:status] = :active @checks << check_info end |
#run_check(&block) ⇒ Object
78 79 80 |
# File 'lib/allgood/configuration.rb', line 78 def run_check(&block) CheckRunner.new.instance_eval(&block) end |
#should_run_check?(check) ⇒ Boolean
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/allgood/configuration.rb', line 82 def should_run_check?(check) return true unless check[:rate] cache_key = "allgood:last_run:#{check[:name].parameterize}" runs_key = "allgood:runs_count:#{check[:name].parameterize}:#{current_period(check[:rate])}" error_key = "allgood:error:#{check[:name].parameterize}" last_result_key = "allgood:last_result:#{check[:name].parameterize}" last_run = Allgood::CacheStore.instance.read(cache_key) period_runs = Allgood::CacheStore.instance.read(runs_key).to_i last_result = Allgood::CacheStore.instance.read(last_result_key) current_period_key = current_period(check[:rate]) stored_period = Allgood::CacheStore.instance.read("allgood:current_period:#{check[:name].parameterize}") # If we're in a new period, reset the counter if stored_period != current_period_key period_runs = 0 Allgood::CacheStore.instance.write("allgood:current_period:#{check[:name].parameterize}", current_period_key) Allgood::CacheStore.instance.write(runs_key, 0) end # If there's an error, wait until next period if previous_error = Allgood::CacheStore.instance.read(error_key) next_period = next_period_start(check[:rate]) rate_info = "Rate limited (#{period_runs}/#{check[:rate][:max_runs]} runs this #{check[:rate][:period]})" check[:skip_reason] = "#{rate_info}. Waiting until #{next_period.strftime('%H:%M:%S %Z')} to retry failed check" return false end # If we haven't exceeded the max runs for this period if period_runs < check[:rate][:max_runs] Allgood::CacheStore.instance.write(cache_key, Time.current) Allgood::CacheStore.instance.write(runs_key, period_runs + 1) true else next_period = next_period_start(check[:rate]) rate_info = "Rate limited (#{period_runs}/#{check[:rate][:max_runs]} runs this #{check[:rate][:period]})" next_run = "Next check at #{next_period.strftime('%H:%M:%S %Z')}" check[:skip_reason] = "#{rate_info}. #{next_run}" false end end |