magent 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,9 @@ Rakefile
6
6
  bin/magent
7
7
  examples/comm/run.rb
8
8
  examples/comm/worker.rb
9
+ examples/error/error.rb
9
10
  examples/simple/bot.rb
11
+ examples/stats/stats.rb
10
12
  lib/magent.rb
11
13
  lib/magent/actor.rb
12
14
  lib/magent/channel.rb
@@ -8,13 +8,18 @@ Simple job queue system based on mongodb
8
8
 
9
9
  == FEATURES/PROBLEMS:
10
10
 
11
+ * fast (enough?)
12
+ * simple
13
+ * scalable
14
+
11
15
  == SYNOPSIS:
12
16
 
13
17
  see examples/
14
18
 
15
19
  == REQUIREMENTS:
16
20
 
17
- * mongodb-ruby-driver
21
+ * mongodb >= 1.0
22
+ * mongo >= 0.15.1 (gem install mongo -s http://gemcutter.org )
18
23
 
19
24
  == INSTALL:
20
25
 
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ $hoe = Hoe.spec 'magent' do
14
14
  self.developer 'David Cuadrado', 'krawek@gmail.com'
15
15
  self.post_install_message = 'PostInstall.txt'
16
16
  self.rubyforge_name = self.name
17
- self.extra_deps = [['mongo','>= 0.15.1']]
17
+ self.extra_deps = [['mongo','>= 0.16']]
18
18
 
19
19
  end
20
20
 
@@ -0,0 +1,33 @@
1
+ $:.unshift File.dirname(__FILE__)+"/../../lib/"
2
+ require 'magent'
3
+
4
+ Magent.push("/errors", :fail, "this is a fail")
5
+
6
+ class Error
7
+ include Magent::Actor
8
+
9
+ channel_name "errors"
10
+ expose :fail
11
+
12
+ def fail(payload)
13
+ @count ||= 0
14
+ errors = self.class.channel.errors
15
+
16
+ errors.each do |error|
17
+ @count += 1
18
+ $stderr.puts "Retrying: #{error["method"]}(#{error["payload"].inspect})"
19
+ self.class.channel.retry_error(error)
20
+ end
21
+
22
+ if @count == 0
23
+ raise payload.inspect
24
+ end
25
+ end
26
+ end
27
+
28
+ Magent.register(Error.new)
29
+
30
+ if $0 == __FILE__
31
+ Magent::Processor.new(Magent.current_actor).run!
32
+ end
33
+
@@ -0,0 +1,27 @@
1
+ $:.unshift File.dirname(__FILE__)+"/../../lib/"
2
+ require 'magent'
3
+
4
+ Magent.push("/stats", :calc)
5
+ Magent.push("/stats", :calc)
6
+ Magent.push("/stats", :calc)
7
+ Magent.push("/stats", :calc)
8
+
9
+ class Stats
10
+ include Magent::Actor
11
+
12
+ channel_name "stats"
13
+ expose :calc
14
+
15
+ def calc(payload)
16
+ $stderr.puts "messages in queue: #{self.class.channel.queue_count}"
17
+ $stderr.puts "total messages count: #{self.class.channel.message_count}"
18
+ $stderr.puts "total errors: #{self.class.channel.error_count}"
19
+ end
20
+ end
21
+
22
+ Magent.register(Stats.new)
23
+
24
+ if $0 == __FILE__
25
+ Magent::Processor.new(Magent.current_actor).run!
26
+ end
27
+
@@ -11,7 +11,7 @@ require 'magent/actor'
11
11
  require 'magent/processor'
12
12
 
13
13
  module Magent
14
- VERSION = '0.1.2'
14
+ VERSION = '0.1.3'
15
15
 
16
16
  def self.connection
17
17
  @@connection ||= Mongo::Connection.new(nil, nil, :auto_reconnect => true)
@@ -55,7 +55,12 @@ module Magent
55
55
 
56
56
  if delta >= task[:every]
57
57
  task[:last_time] = Time.now
58
- instance_eval(&task[:block])
58
+ begin
59
+ instance_eval(&task[:block])
60
+ rescue Exception => e
61
+ $stderr.puts "Failed periodical task: #{e.message}"
62
+ $stderr.puts e.backtrace.join("\n\t")
63
+ end
59
64
  performed = true
60
65
  end
61
66
  end
@@ -8,11 +8,29 @@ module Magent
8
8
  error_collection.save(info.merge({:channel_id => @name, :created_at => Time.now.utc}))
9
9
  end
10
10
 
11
+ def error_count
12
+ error_collection.find({:channel_id => @name}).count()
13
+ end
14
+
11
15
  def errors(conds = {})
12
16
  page = conds.delete(:page) || 1
13
17
  per_page = conds.delete(:per_page) || 10
14
18
 
15
- error_collection.find({:channel_id => @name}, {:offset => (page-1)*per_page, :limit => per_page, :sort => ["created_at"]})
19
+ error_collection.find({:channel_id => @name}, {:skip => (page-1)*per_page, :limit => per_page, :sort => [["created_at", "descending"]]})
20
+ end
21
+
22
+ def remove_error(error_id)
23
+ object_id = error_id
24
+ if error_id.kind_of?(String)
25
+ object_id = Mongo::ObjectID.from_string(error_id)
26
+ end
27
+
28
+ self.error_collection.remove(:_id => object_id, :channel_id => @name)
29
+ end
30
+
31
+ def retry_error(error)
32
+ remove_error(error["_id"])
33
+ enqueue(error["method"], error["payload"])
16
34
  end
17
35
 
18
36
  def error_collection
@@ -9,7 +9,26 @@ module Magent
9
9
  end
10
10
 
11
11
  def enqueue(message)
12
- collection.update({:_id => @name}, {:$push => {:messages => message}}, :repsert => true)
12
+ collection.update({:_id => @name}, {:$push => {:messages => message}, :$inc => {:message_count => 1}}, :repsert => true)
13
+ end
14
+
15
+ def message_count
16
+ channel = collection.find({:_id => @name}, :fields => [:message_count]).next_object
17
+ if channel
18
+ channel["message_count"] || 0
19
+ else
20
+ 0
21
+ end
22
+ end
23
+
24
+ def queue_count
25
+ Magent.database.eval(%@
26
+ function queue_count() {
27
+ var selector = {_id: '#{@name}'};
28
+ var q = db.channels.findOne(selector, {messages: 1 });
29
+ return q.messages.length;
30
+ }
31
+ @)
13
32
  end
14
33
 
15
34
  def dequeue
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{magent}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["David Cuadrado"]
9
- s.date = %q{2009-10-14}
9
+ s.date = %q{2009-11-08}
10
10
  s.default_executable = %q{magent}
11
11
  s.description = %q{Simple job queue system based on mongodb}
12
12
  s.email = ["krawek@gmail.com"]
13
13
  s.executables = ["magent"]
14
14
  s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt"]
15
- s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "bin/magent", "examples/comm/run.rb", "examples/comm/worker.rb", "examples/simple/bot.rb", "lib/magent.rb", "lib/magent/actor.rb", "lib/magent/channel.rb", "lib/magent/generic_channel.rb", "lib/magent/processor.rb", "lib/magent/push.rb", "lib/magent/utils.rb", "magent.gemspec", "script/console", "test/test_helper.rb", "test/test_magent.rb"]
15
+ s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "bin/magent", "examples/comm/run.rb", "examples/comm/worker.rb", "examples/error/error.rb", "examples/simple/bot.rb", "examples/stats/stats.rb", "lib/magent.rb", "lib/magent/actor.rb", "lib/magent/channel.rb", "lib/magent/generic_channel.rb", "lib/magent/processor.rb", "lib/magent/push.rb", "lib/magent/utils.rb", "magent.gemspec", "script/console", "test/test_helper.rb", "test/test_magent.rb"]
16
16
  s.homepage = %q{http://github.com/dcu/magent}
17
17
  s.post_install_message = %q{PostInstall.txt}
18
18
  s.rdoc_options = ["--main", "README.rdoc"]
@@ -27,14 +27,14 @@ Gem::Specification.new do |s|
27
27
  s.specification_version = 3
28
28
 
29
29
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
30
- s.add_runtime_dependency(%q<mongo>, [">= 0.15.1"])
30
+ s.add_runtime_dependency(%q<mongo>, [">= 0.16"])
31
31
  s.add_development_dependency(%q<hoe>, [">= 2.3.3"])
32
32
  else
33
- s.add_dependency(%q<mongo>, [">= 0.15.1"])
33
+ s.add_dependency(%q<mongo>, [">= 0.16"])
34
34
  s.add_dependency(%q<hoe>, [">= 2.3.3"])
35
35
  end
36
36
  else
37
- s.add_dependency(%q<mongo>, [">= 0.15.1"])
37
+ s.add_dependency(%q<mongo>, [">= 0.16"])
38
38
  s.add_dependency(%q<hoe>, [">= 2.3.3"])
39
39
  end
40
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cuadrado
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-14 00:00:00 -05:00
12
+ date: 2009-11-08 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.15.1
23
+ version: "0.16"
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe
@@ -52,7 +52,9 @@ files:
52
52
  - bin/magent
53
53
  - examples/comm/run.rb
54
54
  - examples/comm/worker.rb
55
+ - examples/error/error.rb
55
56
  - examples/simple/bot.rb
57
+ - examples/stats/stats.rb
56
58
  - lib/magent.rb
57
59
  - lib/magent/actor.rb
58
60
  - lib/magent/channel.rb