about summary refs log tree commit homepage
path: root/lib/upr/monitor.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/upr/monitor.rb')
-rw-r--r--lib/upr/monitor.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/upr/monitor.rb b/lib/upr/monitor.rb
new file mode 100644
index 0000000..0c83eef
--- /dev/null
+++ b/lib/upr/monitor.rb
@@ -0,0 +1,41 @@
+# -*- encoding: binary -*-
+module Upr
+
+  # Keeps track of the status of all currently processing uploads
+  # This uses any {Moneta}[http://github.com/wycats/moneta]
+  # store to monitor upload progress.
+  #
+  # Usage (in config.ru with Moneta::Memory store):
+  #   require 'upr'
+  #   require 'moneta/memory'
+  #   use Upr, :backend => Upr::MonetaMonitor.new(Moneta::Memory.new)
+  #   run YourApplication.new
+  class Monitor < Struct.new(:moneta)
+    # nuke anything not read/updated in 10 seconds
+    OPT = { :expires_in => 10 }
+
+    def initialize(moneta_store = nil)
+      super
+      if moneta_store.nil?
+        require 'moneta/memory' # moneta does not autoload :<
+        self.moneta = Moneta::Memory.new
+      end
+    end
+
+    def start(upid, length)
+      moneta.store(upid, Status.new(0, length), OPT)
+    end
+
+    def read(upid)
+      moneta.update_key(upid, OPT)
+      moneta[upid]
+    end
+
+    def incr(upid, nr)
+      status = moneta[upid]
+      status.seen += nr
+      moneta.store(upid, status, OPT)
+    end
+
+  end
+end