Skip to content

Commit

Permalink
improve NewTask check; misc. style and code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
alexch committed Jul 28, 2010
1 parent a4c25e6 commit c9a8598
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,5 @@
.bundle
log
config/env.rb
*.db
*.db-journal
39 changes: 25 additions & 14 deletions app.rb
Expand Up @@ -8,20 +8,24 @@ class SentryApp < Sinatra::Base

configure :production do
DataMapper::Logger.new($stdout, :debug)
measure "Upgrading DB" do
DataMapper.auto_upgrade!
end
end

configure do
DataMapper.setup(:default, ENV['DATABASE_URL'] || 'sqlite3::memory:')
configure :test do
DataMapper.setup(:default, 'sqlite3::memory:')
measure "Wiping DB" do
DataMapper.auto_migrate!
end
end

# workaround for bug http://datamapper.lighthouseapp.com/projects/20609/tickets/1289-autoupgrades-fail-on-sti-models
if ENV['DATABASE_URL']
measure "Upgrading DB" do
DataMapper.auto_upgrade!
end
else
measure "Wiping DB" do
DataMapper.auto_migrate!
end
configure :development do
DataMapper.setup(:default, 'sqlite3:development.db')
# unfortunately, we can't preserve data between dev server runs, due to
# bug http://datamapper.lighthouseapp.com/projects/20609/tickets/1289-autoupgrades-fail-on-sti-models
measure "Wiping DB" do
DataMapper.auto_migrate!
end
end

Expand All @@ -47,16 +51,18 @@ class SentryApp < Sinatra::Base
redirect "/"
end

# magic

get "/work" do
x= capturing_output do
# x= capturing_output do
Delayed::Job.all.each do |job|
puts "invoking #{job.inspect}"
job.invoke_job
job.destroy
end
# Delayed::Worker.new.work_off # this would do only the ones that need it
end
logger.info x
# end
# logger.info x
redirect "/"
end

Expand Down Expand Up @@ -93,4 +99,9 @@ class SentryApp < Sinatra::Base
Cron.summon.stop
redirect "/"
end

get "/wipe" do
DataMapper.auto_migrate!
redirect "/"
end
end
37 changes: 25 additions & 12 deletions lib/new_task.rb
@@ -1,53 +1,66 @@
# todo:
# fetch from site (JSON GET)
# fetch content from site (JSON GET) in parallel; fail if either email or content is missing
# check that comment was added
# unit test
class NewTask < Check

def default_params
super.merge("wait_for" => 60)
super.merge("period" => 30, "limit" => 600, "to" => "new@cohuman.com")
end

def subject
"task #{param("key")}"
end

def run
# todo: allow "run_in" to take a method name to be passed in to run!, so it can invoke it and then catch errors
# todo: allow "run_again" to take a method name to be passed in to run!, so we don't need this switch
if param("key")
check_inbox
else
send_email
end
end

def wait_for
param("wait_for").to_i.seconds
def period
param("period").to_i
end

def send_email
param("key", "#{Time.now.to_i}/#{rand(10000)}")
OutgoingMessage.new(
:to => "new@cohuman.com",
:to => param("to"),
:subject => subject,
:body => subject
).deliver
run_again(wait_for.seconds)
run_again(period.seconds)
PENDING
end

def duration
(Time.now - created_at).to_i
end

def limit
param("limit").to_i
end

def check_inbox
ok = false
Receiver.new.scan do |message|
if message.subject.include? param("key")
ok = true
message.delete
self.reason = "received confirmation email after #{duration} seconds"
return OK
end
end
if ok
OK

self.reason = "did not receive confirmation email after #{duration} seconds"
if duration >= limit
return FAILED
else
self.reason = "did not receive confirmation email within #{wait_for} seconds"
FAILED
run_again(period.seconds)
return PENDING
end
end

end
9 changes: 5 additions & 4 deletions lib/receiver.rb
Expand Up @@ -9,7 +9,7 @@ class Receiver
include ExceptionReporting

attr_reader :config, :debug

def initialize(opts = {}, &block)
@config = !!opts[:config]
@debug = !!opts[:debug]
Expand Down Expand Up @@ -40,6 +40,7 @@ def scan(&block)
n = messages.size
c = e = 0
messages.each do |message_id|
received_message = nil
rfc822 = imap.fetch(message_id, "RFC822")[0].attr["RFC822"]
begin
received_message = receive(rfc822)
Expand All @@ -51,7 +52,7 @@ def scan(&block)
say "Error processing message #{message_id}: #{exception.class.name}: #{exception.message}"
e += 1
ensure
if received_message.delete?
if received_message && received_message.delete?
say "Deleting message #{message_id}"
imap.store(message_id, "+FLAGS", [:Deleted])
end
Expand All @@ -65,7 +66,7 @@ def scan(&block)
rescue Net::IMAP::NoResponseError, Net::IMAP::ByeResponseError, Errno::ENOTCONN
# ignore
rescue Exception => e
log_exception(e)
report_exception(e)
end
say "Processed #{c.inspect} of #{n.inspect} emails received (#{e} errors)."
end
Expand All @@ -89,5 +90,5 @@ def perform
def say message
puts "#{Time.now} - #{message}" # was: logger.info(message) but that wasn't working
end

end
14 changes: 9 additions & 5 deletions setup.rb
Expand Up @@ -56,15 +56,19 @@ def send_at(time, method, *args)
DataMapper.finalize
DataMapper::Model.raise_on_save_failure = true


# utility methods
def capturing_output
original_stdout = $stdout
$stdout = StringIO.new
original = $stdout
captured = StringIO.new
$stdout = captured
yield
$stdout.string
captured.string
ensure
$stdout = original_stdout
# allow nesting
if original.is_a? StringIO
original << captured.string
end
$stdout = original
end

# workaround for activesupport vs. json_pure vs. Ruby 1.8 glitch
Expand Down
7 changes: 6 additions & 1 deletion todo.txt
@@ -1,5 +1,10 @@
todo
-----

notifications via exceptional
notifications via email (state change)
stop/restart a check

declared params, with description and default, per class
inherited declared params
typed params (e.g. int)

25 changes: 18 additions & 7 deletions views/main.rb
Expand Up @@ -17,17 +17,23 @@ def page_title
/* sentry main page */
body{ padding: 1em; }
h1 { font-size: 18pt; margin-top: .5em; margin-bottom: .25em; }
h2 { font-size: 14pt; margin-top: .5em; margin-bottom: .25em; }
h1, h2 {
margin-top: .5em; margin-bottom: .25em; padding: .25em .25em 0;
border-bottom: 1px solid #222222; background: #EEEEFF;
}
h1 { font-size: 18pt; }
h2 { font-size: 14pt; }
/* styled tables */
td, th { border: 2px solid gray; }
td, th { padding: 2px; }
th { background-color: #EEE; text-align: left; }
/* check table */
td.param, th.param { border: 1px solid gray; }
td.param { width: 100%; }
td, th { padding: 2px; }
td.ok { color: green; }
td.failed { color: red; }
th { background-color: #EEE; text-align: left; }
td.outcome { font-weight: bold; padding: 2px 4px; }
/* magic buttons */
Expand Down Expand Up @@ -96,8 +102,8 @@ def check_run_cells(check)
end

def params_table(params)
params.each_pair do |key, value|
table :width => "100%" do
table :width => "100%" do
params.each_pair do |key, value|
tr do
th(:class => "param") { text key }
td(:class => "param") { text value }
Expand Down Expand Up @@ -128,14 +134,19 @@ def body_content
end
li do
form :action => "/work", :method => "get" do
input :type => :submit, :value => "Work Off"
input :type => :submit, :value => "Work Jobs"
end
end
li do
form :action => "/sample", :method => "get" do
input :type => :submit, :value => "Sample"
end
end
li do
form :action => "/wipe", :method => "get" do
input :type => :submit, :value => "Wipe DB"
end
end
end
end

Expand Down
8 changes: 4 additions & 4 deletions views/new_check.rb
Expand Up @@ -58,11 +58,11 @@ def params_row(check_class)
tr :class => ["params_row", check_type] do
th "params"
td do
check_class.new.default_params.each_pair do |param_name, param_value|
table :class => "#{check_type}_params" do
table :class => "#{check_type}_params" do
check_class.new.default_params.each_pair do |param_name, param_value|
tr do
th param_name
td do
th param_name, :class => "param"
td :class => "param" do
input :name => "#{check_type}[#{param_name}]", :value => param_value
end
end
Expand Down

0 comments on commit c9a8598

Please sign in to comment.