From 680a3827db292bcf34ab0aeb773720562866bc3b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 12 Nov 2009 13:45:10 -0800 Subject: [PATCH] backends: internal API updates/cleanups Return values for class methods for Upr::Monitor and UprStatus should synched. Class methods: * error!(upid) method added: sets seen = -1, updates time and returns Status(-like) object * read(upid) no longer updates time * incr(upid, nr) does up increment errored statuses * incr(upid, nr) returns nil on invalid upids * finish(upid) returns nil on invalid upids Additionally the AR example has been updated to include Upr::StatusMethods for instance methods. --- examples/rails_app-2.3.4/app/models/upr_status.rb | 20 ++++++++++++++------ lib/upr/monitor.rb | 13 +++++++++---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/examples/rails_app-2.3.4/app/models/upr_status.rb b/examples/rails_app-2.3.4/app/models/upr_status.rb index 61d39bc..eeefd8b 100644 --- a/examples/rails_app-2.3.4/app/models/upr_status.rb +++ b/examples/rails_app-2.3.4/app/models/upr_status.rb @@ -4,11 +4,7 @@ class UprStatus < ActiveRecord::Base class << self def read(upid) - if rv = find_by_upid(upid) - rv.time = Time.now.to_i - rv.save - rv - end + find_by_upid(upid) end def start(upid, length) @@ -23,7 +19,18 @@ class UprStatus < ActiveRecord::Base def incr(upid, nr) update_all("seen = seen + #{nr.to_i}, time = #{Time.now.to_i}", - { :upid => upid }) + [ "upid = ? AND seen >= 0", upid ]) + end + + def error!(upid) + transaction do + if rv = find_by_upid(upid) + rv.time = Time.now.to_i + rv.seen = -1 + rv.save + rv + end + end end def finish(upid) @@ -44,4 +51,5 @@ class UprStatus < ActiveRecord::Base end end + include Upr::StatusMethods end diff --git a/lib/upr/monitor.rb b/lib/upr/monitor.rb index 2ccb447..bcf3f03 100644 --- a/lib/upr/monitor.rb +++ b/lib/upr/monitor.rb @@ -27,22 +27,27 @@ module Upr end def read(upid) - moneta.update_key(upid, OPT) moneta[upid] end def incr(upid, nr) - status = moneta[upid] - status.seen += nr + status = moneta[upid] or return + status.seen += nr if status.seen >= 0 moneta.store(upid, status, OPT) end def finish(upid) - status = moneta[upid] + status = moneta[upid] or return status.length ||= status.seen status.seen = status.length moneta.store(upid, status, OPT) end + def error!(upid) + status = moneta[upid] or return + status.seen = -1 + moneta.store(upid, status, OPT) + end + end end -- 2.11.4.GIT