Skip to content
Snippets Groups Projects
Commit 954c8086 authored by Jorn van de Beek's avatar Jorn van de Beek
Browse files

Measure cron calls

parent a96139c1
Branches
Tags
No related merge requests found
Pipeline #45173 passed
...@@ -17,4 +17,5 @@ group :test do ...@@ -17,4 +17,5 @@ group :test do
gem 'responders' gem 'responders'
gem 'rspec-instrumentation-matcher' gem 'rspec-instrumentation-matcher'
gem 'rspec-rails' gem 'rspec-rails'
gem 'pry'
end end
...@@ -188,6 +188,7 @@ DEPENDENCIES ...@@ -188,6 +188,7 @@ DEPENDENCIES
delayed_job_active_record delayed_job_active_record
fakefs fakefs
guard-rspec (~> 4.2.6) guard-rspec (~> 4.2.6)
pry
rake rake
responders responders
roqua-support! roqua-support!
...@@ -199,4 +200,4 @@ DEPENDENCIES ...@@ -199,4 +200,4 @@ DEPENDENCIES
timecop timecop
BUNDLED WITH BUNDLED WITH
1.16.6 1.17.3
require 'active_support/concern'
module Roqua module Roqua
module Probes module Probes
module BaseProbe module BaseProbe
def enable extend ActiveSupport::Concern
new.tap do |probe|
probe_sym = probe.class.to_s.to_sym class_methods do
Appsignal::Minutely.probes.register(probe_sym, probe) unless Appsignal::Minutely.probes[probe_sym] def enable
new.tap do |probe|
probe_sym = probe.class.to_s.to_sym
Appsignal::Minutely.probes.register(probe_sym, probe) unless Appsignal::Minutely.probes[probe_sym]
end
end end
end end
# do not override me, implement probes by implementing the #run method
def call
run
Appsignal.increment_counter("probe.call.completed.#{self.class.name.demodulize.underscore}", 1)
end
end end
end end
end end
...@@ -3,13 +3,13 @@ require_relative 'base_probe' ...@@ -3,13 +3,13 @@ require_relative 'base_probe'
module Roqua module Roqua
module Probes module Probes
class DelayedJobProbe class DelayedJobProbe
extend BaseProbe include BaseProbe
def backlog_count def backlog_count
Delayed::Job.where(locked_at: nil).where('run_at < ?', Time.zone.now).count Delayed::Job.where(locked_at: nil).where('run_at < ?', Time.zone.now).count
end end
def call def run
Appsignal.set_gauge('delayed_job_backlog_count', backlog_count) Appsignal.set_gauge('delayed_job_backlog_count', backlog_count)
end end
end end
......
...@@ -3,7 +3,7 @@ require_relative 'base_probe' ...@@ -3,7 +3,7 @@ require_relative 'base_probe'
module Roqua module Roqua
module Probes module Probes
class MonitoringProbe class MonitoringProbe
extend BaseProbe include BaseProbe
def incomplete_jobs def incomplete_jobs
Roqua::Scheduling::CronJob.where('completed_at IS NULL OR completed_at < next_run_at') Roqua::Scheduling::CronJob.where('completed_at IS NULL OR completed_at < next_run_at')
...@@ -19,7 +19,7 @@ module Roqua ...@@ -19,7 +19,7 @@ module Roqua
(longest_delay_in_seconds / 1.minute).to_i (longest_delay_in_seconds / 1.minute).to_i
end end
def call def run
Appsignal.set_gauge('scheduler_delay_in_minutes', longest_delay_in_minutes) Appsignal.set_gauge('scheduler_delay_in_minutes', longest_delay_in_minutes)
end end
end end
......
...@@ -3,9 +3,9 @@ require_relative 'base_probe' ...@@ -3,9 +3,9 @@ require_relative 'base_probe'
module Roqua module Roqua
module Probes module Probes
class SchedulingProbe class SchedulingProbe
extend BaseProbe include BaseProbe
def call def run
ActiveRecord::Base.establish_connection ActiveRecord::Base.establish_connection
require_relative Rails.root.join('config', 'schedule.rb') require_relative Rails.root.join('config', 'schedule.rb')
Roqua::Scheduling::Scheduler.new.ping Roqua::Scheduling::Scheduler.new.ping
......
...@@ -42,6 +42,7 @@ class Roqua::Scheduling::Scheduler ...@@ -42,6 +42,7 @@ class Roqua::Scheduling::Scheduler
task = schedule.tasks[cron_job.name] task = schedule.tasks[cron_job.name]
task.run task.run
Appsignal.increment_counter("scheduler.run_task.completed.#{task.name}", 1)
cron_job.update completed_at: Time.now, next_run_at: task.next_run_at cron_job.update completed_at: Time.now, next_run_at: task.next_run_at
end end
end end
...@@ -7,7 +7,7 @@ require 'roqua/probes/delayed_job_probe' ...@@ -7,7 +7,7 @@ require 'roqua/probes/delayed_job_probe'
describe Roqua::Probes::DelayedJobProbe do describe Roqua::Probes::DelayedJobProbe do
before { Timecop.freeze } before { Timecop.freeze }
after { Timecop.return } after { Timecop.return }
subject(:probe) { Roqua::Probes::DelayedJobProbe.new } subject(:probe) { described_class.new }
describe 'backlog_count' do describe 'backlog_count' do
context 'if a single job unlocked job exists that has a run_at in the past' do context 'if a single job unlocked job exists that has a run_at in the past' do
...@@ -24,12 +24,17 @@ describe Roqua::Probes::DelayedJobProbe do ...@@ -24,12 +24,17 @@ describe Roqua::Probes::DelayedJobProbe do
end end
end end
describe 'call' do describe '#call' do
it 'sends the correct metric to Appsignal' do it 'sends the correct metric to Appsignal' do
expect(probe).to receive(:backlog_count).and_return(12) expect(probe).to receive(:backlog_count).and_return(12)
expect(Appsignal).to receive(:set_gauge).with('delayed_job_backlog_count', 12) expect(Appsignal).to receive(:set_gauge).with('delayed_job_backlog_count', 12)
probe.call probe.call
end end
it 'increments the probe call counter' do
expect(Appsignal).to receive(:increment_counter).with('probe.call.completed.delayed_job_probe', 1)
probe.call
end
end end
context '.enable' do context '.enable' do
......
...@@ -4,21 +4,26 @@ require 'timecop' ...@@ -4,21 +4,26 @@ require 'timecop'
require 'roqua/probes/delayed_job_probe' require 'roqua/probes/delayed_job_probe'
describe Roqua::Probes::DelayedJobProbe do describe Roqua::Probes::MonitoringProbe do
before { Timecop.freeze } before { Timecop.freeze }
after { Timecop.return } after { Timecop.return }
subject(:probe) { Roqua::Probes::MonitoringProbe.new } subject(:probe) { described_class.new }
before do before do
Roqua::Scheduling::CronJob.delete_all Roqua::Scheduling::CronJob.delete_all
end end
describe 'call' do describe '#call' do
it 'sends data to AppSignal' do it 'sends data to AppSignal' do
expect(Appsignal).to receive(:set_gauge).with('scheduler_delay_in_minutes', 10) expect(Appsignal).to receive(:set_gauge).with('scheduler_delay_in_minutes', 10)
expect(probe).to receive(:longest_delay_in_minutes).and_return(10) expect(probe).to receive(:longest_delay_in_minutes).and_return(10)
probe.call probe.call
end end
it 'increments the probe call counter' do
expect(Appsignal).to receive(:increment_counter).with('probe.call.completed.monitoring_probe', 1)
probe.call
end
end end
describe 'longest_delay' do describe 'longest_delay' do
......
...@@ -72,16 +72,23 @@ describe Roqua::Scheduling::Scheduler do ...@@ -72,16 +72,23 @@ describe Roqua::Scheduling::Scheduler do
describe 'callbacks' do describe 'callbacks' do
let(:callback_spy) { spy(:callback) } let(:callback_spy) { spy(:callback) }
it 'get executed' do before do
Roqua::Scheduling::Schedule.setup do |cron| Roqua::Scheduling::Schedule.setup do |cron|
cron.add_task 'hourly', next_run_at: proc { 1.minute.ago } do cron.add_task 'hourly', next_run_at: proc { 1.minute.ago } do
callback_spy.execute callback_spy.execute
end end
end end
end
it 'get executed' do
expect(callback_spy).to receive(:execute) expect(callback_spy).to receive(:execute)
subject.ping subject.ping
end end
it 'counts task calls' do
expect(Appsignal).to receive(:increment_counter).with('scheduler.run_task.completed.hourly', 1)
subject.ping
end
end end
describe 'when running multiple times' do describe 'when running multiple times' do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment