backends: internal API updates/cleanups
[upr.git] / examples / rails_app-2.3.4 / app / models / upr_status.rb
blobeeefd8bac6a4a532fab2d6887acb30d11b1fbfd0
1 class UprStatus < ActiveRecord::Base
2   cattr_accessor :gc_cutoff
3   @@gc_cutoff = 10
5   class << self
6     def read(upid)
7       find_by_upid(upid)
8     end
10     def start(upid, length)
11       # this must be a find_or_create_by since some users have twitchy
12       # fingers and hit the upload button prematurely
13       find_or_create_by_upid(upid) do |x|
14         x.length = length
15         x.time = Time.now.to_i
16         x.seen = 0
17       end
18     end
20     def incr(upid, nr)
21       update_all("seen = seen + #{nr.to_i}, time = #{Time.now.to_i}",
22                  [ "upid = ? AND seen >= 0", upid ])
23     end
25     def error!(upid)
26       transaction do
27         if rv = find_by_upid(upid)
28           rv.time = Time.now.to_i
29           rv.seen = -1
30           rv.save
31           rv
32         end
33       end
34     end
36     def finish(upid)
37       transaction do
38         if rv = find_by_upid(upid)
39           rv.time = Time.now.to_i
40           rv.length ||= rv.seen
41           rv.seen = rv.length
42           rv.save
43           rv
44         end
45       end
46     end
48     def gc
49       cutoff = Time.now.to_i - @@gc_cutoff
50       delete_all "time < #{cutoff}"
51     end
52   end
54   include Upr::StatusMethods
55 end