Skip to content

Commit

Permalink
Dashboard warning when clockwork doesn't seem to be running
Browse files Browse the repository at this point in the history
  • Loading branch information
nlalonde committed Mar 21, 2013
1 parent 40962c8 commit 8cc7f3c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/models/admin_dashboard_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.fetch
def as_json
@json ||= {
reports: REPORTS.map { |type| Report.find(type) },
problems: [rails_env_check, host_names_check, gc_checks].compact
problems: [rails_env_check, host_names_check, gc_checks, clockwork_check].compact
}.merge(
SiteSetting.version_checks? ? {version_check: DiscourseUpdates.check_version} : {}
)
Expand All @@ -26,4 +26,8 @@ def host_names_check
def gc_checks
I18n.t("dashboard.gc_warning") if ENV['RUBY_GC_MALLOC_LIMIT'].nil?
end

def clockwork_check
I18n.t('dashboard.clockwork_warning') unless Jobs::ClockworkHeartbeat.is_clockwork_running?
end
end
1 change: 1 addition & 0 deletions config/clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ module Clockwork
every(1.minute, 'calculate_score')
every(20.minutes, 'calculate_view_counts')
every(1.day, 'version_check')
every(1.minute, 'clockwork_heartbeat')
end
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ en:
rails_env_warning: "Your server is running in %{env} mode."
host_names_warning: "Your config/database.yml file is using the default localhost hostname. Update it to use your site's hostname."
gc_warning: 'Your server is using default ruby garbage collection parameters, which will not give you the best performance. Read this topic on performance tuning: <a href="http://meta.discourse.org/t/tuning-ruby-and-rails-for-discourse/4126">Tuning Ruby and Rails for Discourse</a>.'
clockwork_warning: 'Clockwork is not running. Ensure that a clockwork process is always running so that important jobs can be scheduled. <a href="https://github.com/tomykaira/clockwork">Learn about clockwork here</a>.'

site_settings:
default_locale: "The default language of this Discourse instance (ISO 639-1 Code)"
Expand Down
31 changes: 31 additions & 0 deletions lib/jobs/clockwork_heartbeat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Jobs
class ClockworkHeartbeat < Jobs::Base

def execute(args)
$redis.set last_heartbeat_at_key, Time.now.to_i
end

def self.is_clockwork_running?
if time = ClockworkHeartbeat.new.last_heartbeat_at
time > 2.minutes.ago
else
false
end
end

def last_heartbeat_at
if time_int = $redis.get(last_heartbeat_at_key)
Time.at(time_int.to_i)
else
nil
end
end

private

def last_heartbeat_at_key
'clockwork:last_heartbeat_at'
end

end
end
26 changes: 26 additions & 0 deletions spec/components/jobs/clockwork_heartbeat_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'
require 'jobs'

describe Jobs::ClockworkHeartbeat do

describe '#is_clockwork_running?' do

subject { Jobs::ClockworkHeartbeat.is_clockwork_running? }

it 'returns false if last_heartbeat_at is nil' do
Jobs::ClockworkHeartbeat.any_instance.stubs(:last_heartbeat_at).returns(nil)
subject.should be_false
end

it 'returns false if last_heartbeat_at is more than 2 minutes ago' do
Jobs::ClockworkHeartbeat.any_instance.stubs(:last_heartbeat_at).returns(10.minutes.ago)
subject.should be_false
end

it 'returns true if last_heartbeat_at is more recent than 2 minutes ago' do
Jobs::ClockworkHeartbeat.any_instance.stubs(:last_heartbeat_at).returns(Time.zone.now)
subject.should be_true
end
end

end
14 changes: 14 additions & 0 deletions spec/models/admin_dashboard_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@
end
end

describe 'clockwork_check' do
subject { AdminDashboardData.new.clockwork_check }

it 'returns nil when clockwork is running' do
Jobs::ClockworkHeartbeat.stubs(:is_clockwork_running?).returns(true)
subject.should be_nil
end

it 'returns a string when clockwork is not running' do
Jobs::ClockworkHeartbeat.stubs(:is_clockwork_running?).returns(false)
subject.should_not be_nil
end
end

end

0 comments on commit 8cc7f3c

Please sign in to comment.