magent 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,13 +3,14 @@ require 'magent'
3
3
 
4
4
  # Use: magent /path/to/this/file
5
5
 
6
- Magent.push("/bot", :echo, "hello, world")
7
- Magent.push("/bot", :do_task, "File", :exist?, "/etc/passwd")
8
- Magent.push("/bot", :echo, "Press ctrl+c to close")
6
+ Magent.push("/bots", :echo, "hello, world")
7
+ Magent.push("/bots", :do_task, "File", :exist?, "/etc/passwd")
8
+ Magent.push("/bots", :echo, "Press ctrl+c to close")
9
+ Magent.push("/bots", :do_not_exist, "you should not see this message")
9
10
 
10
11
  class Bot
11
12
  include Magent::Actor
12
-
13
+ channel_name "bots"
13
14
  expose :echo, :do_task
14
15
 
15
16
  def echo(payload)
@@ -30,3 +31,4 @@ Magent.register(Bot.new)
30
31
  if $0 == __FILE__
31
32
  Magent::Processor.new(Magent.current_actor).run!
32
33
  end
34
+
@@ -10,7 +10,7 @@ require 'magent/actor'
10
10
  require 'magent/processor'
11
11
 
12
12
  module Magent
13
- VERSION = '0.0.1'
13
+ VERSION = '0.0.2'
14
14
 
15
15
  def self.connection
16
16
  @@connection ||= Mongo::Connection.new
@@ -13,6 +13,10 @@ module Magent
13
13
  end
14
14
  end
15
15
 
16
+ def channel_name(name = nil)
17
+ @channel_name ||= (name || Magent::Utils.underscore(self.name))
18
+ end
19
+
16
20
  def actions
17
21
  @actions ||= Set.new
18
22
  end
@@ -24,7 +28,7 @@ module Magent
24
28
 
25
29
  def channel
26
30
  @channel ||= begin
27
- channel_name = "/"+Magent::Utils.underscore(self.name)
31
+ channel_name = "/"+self.channel_name
28
32
  Channel.new(channel_name)
29
33
  end
30
34
  end
@@ -4,7 +4,7 @@ module Magent
4
4
  @name = name
5
5
 
6
6
  if !collection.find_one({:_id => @name}, {:fields => [:_id]})
7
- collection.save({:_id => @name, :messages => []})
7
+ collection.save({:_id => @name, :messages => [], :errors => []})
8
8
  end
9
9
  end
10
10
 
@@ -12,15 +12,26 @@ module Magent
12
12
  collection.update({:_id => @name}, {:$push => {:messages => [message, args]}}, :repsert => true)
13
13
  end
14
14
 
15
+ def failed(info)
16
+ error_collection.save(info.merge({:channel_id => @name, :created_at => Time.now.utc}))
17
+ end
18
+
19
+ def errors(conds = {})
20
+ page = conds.delete(:page) || 1
21
+ per_page = conds.delete(:per_page) || 10
22
+
23
+ error_collection.find({:channel_id => @name}, {:offset => (page-1)*per_page, :limit => per_page, :sort => ["created_at"]})
24
+ end
25
+
15
26
  def dequeue
16
27
  Magent.database.eval(%@
17
28
  function dequeue() {
18
- return db.eval(function() {
19
- var q = db.channels.findOne({_id: '#{@name}'});
20
- var m = q.messages.shift();
21
- db.channels.save(q); //slow
22
- return m;
23
- });
29
+ var selector = {_id: '#{@name}'};
30
+ var q = db.channels.findOne(selector, {messages: 1 });
31
+ var m = q.messages[0];
32
+ if(m)
33
+ db.channels.update(selector, { $pop: { messages : -1 } })
34
+ return m;
24
35
  }
25
36
  @)
26
37
  end
@@ -29,6 +40,10 @@ module Magent
29
40
  self.class.collection
30
41
  end
31
42
 
43
+ def error_collection
44
+ @error_collection ||= Magent.database.collection("errors")
45
+ end
46
+
32
47
  def self.collection
33
48
  @collection ||= Magent.database.collection("channels")
34
49
  end
@@ -22,7 +22,17 @@ module Magent
22
22
  else
23
23
  delay = 0
24
24
  $stderr.puts "#{@actor.class}##{method}(#{payload.inspect})"
25
- @actor.send(method, payload) # TODO: what if method is not defined?
25
+ begin
26
+ if @actor.class.actions.include?(method)
27
+ @actor.send(method, payload)
28
+ else
29
+ $stderr.puts "Unknown action: #{method} (payload=#{payload.inspect})"
30
+ end
31
+ rescue Exception => e
32
+ $stderr.puts "Error while executing #{method.inspect} #{payload.inspect}"
33
+ $stderr.puts "#{e.to_s} #{e.backtrace.join("\t\n")}"
34
+ @actor.class.channel.failed(:message => e.message, :method => method, :payload => payload, :backtrace => e.backtrace)
35
+ end
26
36
  end
27
37
 
28
38
  sleep delay
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{magent}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
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-03}
9
+ s.date = %q{2009-10-10}
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"]
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.0.1
4
+ version: 0.0.2
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-04 00:00:00 -05:00
12
+ date: 2009-10-10 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency