Module: Cronitor
- Defined in:
- lib/cronitor.rb,
lib/cronitor/error.rb,
lib/cronitor/config.rb,
lib/cronitor/monitor.rb,
lib/cronitor/version.rb
Defined Under Namespace
Classes: ConfigurationError, Error, Monitor, ValidationError
Constant Summary collapse
- MONITOR_TYPES =
[ TYPE_JOB = 'job', TYPE_HEARTBEAT = 'heartbeat', TYPE_CHECK = 'check' ].freeze
- YAML_KEYS =
MONITOR_TYPES.map { |t| "#{t}s" }
- VERSION =
'5.1.0'
Class Attribute Summary collapse
-
.api_key ⇒ Object
Returns the value of attribute api_key.
-
.api_version ⇒ Object
Returns the value of attribute api_version.
-
.auto_discover_sidekiq ⇒ Object
Returns the value of attribute auto_discover_sidekiq.
-
.config ⇒ Object
Returns the value of attribute config.
-
.environment ⇒ Object
Returns the value of attribute environment.
-
.logger ⇒ Object
Returns the value of attribute logger.
-
.ping_timeout ⇒ Object
Returns the value of attribute ping_timeout.
-
.timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
- .apply_config(rollback: false) ⇒ Object
- .configure(&block) ⇒ Object
- .job(key, &block) ⇒ Object
- .monitor_api_url ⇒ Object
- .read_config(path = nil) ⇒ Object
- .symbolize_keys(obj) ⇒ Object
- .validate_config ⇒ Object
Class Attribute Details
.api_key ⇒ Object
Returns the value of attribute api_key.
12 13 14 |
# File 'lib/cronitor/config.rb', line 12 def api_key @api_key end |
.api_version ⇒ Object
Returns the value of attribute api_version.
12 13 14 |
# File 'lib/cronitor/config.rb', line 12 def api_version @api_version end |
.auto_discover_sidekiq ⇒ Object
Returns the value of attribute auto_discover_sidekiq.
12 13 14 |
# File 'lib/cronitor/config.rb', line 12 def auto_discover_sidekiq @auto_discover_sidekiq end |
.config ⇒ Object
Returns the value of attribute config.
12 13 14 |
# File 'lib/cronitor/config.rb', line 12 def config @config end |
.environment ⇒ Object
Returns the value of attribute environment.
12 13 14 |
# File 'lib/cronitor/config.rb', line 12 def environment @environment end |
.logger ⇒ Object
Returns the value of attribute logger.
12 13 14 |
# File 'lib/cronitor/config.rb', line 12 def logger @logger end |
.ping_timeout ⇒ Object
Returns the value of attribute ping_timeout.
12 13 14 |
# File 'lib/cronitor/config.rb', line 12 def ping_timeout @ping_timeout end |
.timeout ⇒ Object
Returns the value of attribute timeout.
12 13 14 |
# File 'lib/cronitor/config.rb', line 12 def timeout @timeout end |
Class Method Details
.apply_config(rollback: false) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cronitor.rb', line 32 def self.apply_config(rollback: false) conf = read_config # allow a significantly longer timeout on requests that are sending full yaml config. min 30 seconds. timeout = Cronitor.timeout < 30 ? 30 : Cronitor.timeout monitors = Monitor.put(monitors: conf, format: Cronitor::Monitor::Formats::YAML, rollback: rollback, timeout: timeout) count = 0 # step through the different monitor types and count up all the returned configurations Cronitor::YAML_KEYS.each do |k| count += (monitors[k]&.count || 0) end puts("#{count} monitors #{rollback ? 'validated' : 'synced to Cronitor'}.") rescue ValidationError => e Cronitor.logger&.error(e) end |
.configure(&block) ⇒ Object
14 15 16 |
# File 'lib/cronitor/config.rb', line 14 def configure(&block) block.call(self) end |
.job(key, &block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cronitor.rb', line 52 def self.job(key, &block) monitor = Monitor.new(key) series = Time.now.to_f monitor.ping(state: 'run', series: series) begin block.call monitor.ping(state: 'complete', series: series) rescue StandardError => e monitor.ping(state: 'fail', message: e.[[0, e..length - 1600].max..-1], series: series) raise e end end |
.monitor_api_url ⇒ Object
66 67 68 |
# File 'lib/cronitor.rb', line 66 def self.monitor_api_url 'https://cronitor.io/api/monitors' end |
.read_config(path = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cronitor.rb', line 16 def self.read_config(path = nil) Cronitor.config = path || Cronitor.config unless Cronitor.config raise ConfigurationError.new( "Must include a path by setting Cronitor.config or passing a path to read_config e.g. \ Cronitor.read_config('./cronitor.yaml')" ) end conf = YAML.safe_load(File.read(Cronitor.config)) conf.each do |k, _v| raise ConfigurationError.new("Invalid configuration variable: #{k}") unless Cronitor::YAML_KEYS.include?(k) end conf end |
.symbolize_keys(obj) ⇒ Object
220 221 222 223 224 225 |
# File 'lib/cronitor/monitor.rb', line 220 def self.symbolize_keys(obj) obj.inject({}) do |memo, (k, v)| memo[k.to_sym] = v memo end end |
.validate_config ⇒ Object
48 49 50 |
# File 'lib/cronitor.rb', line 48 def self.validate_config apply_config(rollback: true) end |