magent 0.1.0 → 0.1.1

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.
data/Manifest.txt CHANGED
@@ -4,10 +4,13 @@ PostInstall.txt
4
4
  README.rdoc
5
5
  Rakefile
6
6
  bin/magent
7
+ examples/comm/run.rb
8
+ examples/comm/worker.rb
7
9
  examples/simple/bot.rb
8
10
  lib/magent.rb
9
11
  lib/magent/actor.rb
10
12
  lib/magent/channel.rb
13
+ lib/magent/generic_channel.rb
11
14
  lib/magent/processor.rb
12
15
  lib/magent/push.rb
13
16
  lib/magent/utils.rb
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.dirname(__FILE__)+"/../../lib/"
4
+ require 'magent'
5
+
6
+ id = "#{rand(16)}#{rand(16)}#{rand(16)}#{rand(16)}"
7
+
8
+ values = (1..5).to_a.map { rand(10) }
9
+ puts values.join(" + ")
10
+ Magent.push("/workers", :sum, id, *values)
11
+
12
+ channel = Magent::GenericChannel.new("+#{id}")
13
+
14
+ loop do
15
+ v = channel.dequeue;
16
+ if v
17
+ $stdout.puts v.inspect
18
+ break
19
+ end
20
+ sleep 0.1
21
+ end
22
+
@@ -0,0 +1,30 @@
1
+ $:.unshift File.dirname(__FILE__)+"/../../lib/"
2
+ require 'magent'
3
+
4
+ # Use: magent /path/to/this/file
5
+
6
+ class Worker
7
+ include Magent::Actor
8
+ channel_name "workers"
9
+ expose :sum
10
+
11
+ def sum(payload)
12
+ id, *args = payload
13
+
14
+ s = args.inject(0) { |v, a| a += v }
15
+ send_to_client(id, {:method => :sum, :result => s})
16
+ end
17
+
18
+ private
19
+ def send_to_client(id, message)
20
+ c = Magent::GenericChannel.new("+#{id}")
21
+ c.enqueue(message)
22
+ end
23
+ end
24
+
25
+ Magent.register(Worker.new)
26
+
27
+ if $0 == __FILE__
28
+ Magent::Processor.new(Magent.current_actor).run!
29
+ end
30
+
data/lib/magent.rb CHANGED
@@ -4,13 +4,14 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'mongo'
5
5
  require 'set'
6
6
  require 'magent/utils'
7
+ require 'magent/generic_channel'
7
8
  require 'magent/channel'
8
9
  require 'magent/push'
9
10
  require 'magent/actor'
10
11
  require 'magent/processor'
11
12
 
12
13
  module Magent
13
- VERSION = '0.1.0'
14
+ VERSION = '0.1.1'
14
15
 
15
16
  def self.connection
16
17
  @@connection ||= Mongo::Connection.new(nil, nil, :auto_reconnect => true)
@@ -1,15 +1,7 @@
1
1
  module Magent
2
- class Channel
3
- def initialize(name)
4
- @name = name
5
-
6
- if !collection.find_one({:_id => @name}, {:fields => [:_id]})
7
- collection.save({:_id => @name, :messages => [], :errors => []})
8
- end
9
- end
10
-
2
+ class Channel < GenericChannel
11
3
  def enqueue(message, args)
12
- collection.update({:_id => @name}, {:$push => {:messages => [message, args]}}, :repsert => true)
4
+ super([message, args])
13
5
  end
14
6
 
15
7
  def failed(info)
@@ -23,38 +15,8 @@ module Magent
23
15
  error_collection.find({:channel_id => @name}, {:offset => (page-1)*per_page, :limit => per_page, :sort => ["created_at"]})
24
16
  end
25
17
 
26
- def dequeue
27
- Magent.database.eval(%@
28
- function dequeue() {
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;
35
- }
36
- @)
37
- end
38
-
39
- def collection
40
- self.class.collection
41
- end
42
-
43
18
  def error_collection
44
19
  @error_collection ||= Magent.database.collection("errors")
45
20
  end
46
-
47
- def self.collection
48
- @collection ||= Magent.database.collection("channels")
49
- end
50
-
51
- def self.all(&block)
52
- cursor = collection.find({}, :fields => [:_id])
53
- if block_given?
54
- cursor.map {|c| name = c["_id"]; yield name; name }
55
- else
56
- cursor.map {|c| c["_id"] }
57
- end
58
- end
59
21
  end # Channel
60
22
  end
@@ -0,0 +1,45 @@
1
+ module Magent
2
+ class GenericChannel
3
+ def initialize(name)
4
+ @name = name
5
+
6
+ if !collection.find_one({:_id => @name}, {:fields => [:_id]})
7
+ collection.save({:_id => @name, :messages => []})
8
+ end
9
+ end
10
+
11
+ def enqueue(message)
12
+ collection.update({:_id => @name}, {:$push => {:messages => message}}, :repsert => true)
13
+ end
14
+
15
+ def dequeue
16
+ Magent.database.eval(%@
17
+ function dequeue() {
18
+ var selector = {_id: '#{@name}'};
19
+ var q = db.channels.findOne(selector, {messages: 1 });
20
+ var m = q.messages[0];
21
+ if(m)
22
+ db.channels.update(selector, { $pop: { messages : -1 } })
23
+ return m;
24
+ }
25
+ @)
26
+ end
27
+
28
+ def collection
29
+ self.class.collection
30
+ end
31
+
32
+ def self.collection
33
+ @collection ||= Magent.database.collection("channels")
34
+ end
35
+
36
+ def self.all(&block)
37
+ cursor = collection.find({}, :fields => [:_id])
38
+ if block_given?
39
+ cursor.map {|c| name = c["_id"]; yield name; name }
40
+ else
41
+ cursor.map {|c| c["_id"] }
42
+ end
43
+ end
44
+ end # GenericChannel
45
+ end
data/magent.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{magent}
5
- s.version = "0.1.0"
5
+ s.version = "0.1.1"
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"]
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
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/simple/bot.rb", "lib/magent.rb", "lib/magent/actor.rb", "lib/magent/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/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"]
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"]
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cuadrado
@@ -50,10 +50,13 @@ files:
50
50
  - README.rdoc
51
51
  - Rakefile
52
52
  - bin/magent
53
+ - examples/comm/run.rb
54
+ - examples/comm/worker.rb
53
55
  - examples/simple/bot.rb
54
56
  - lib/magent.rb
55
57
  - lib/magent/actor.rb
56
58
  - lib/magent/channel.rb
59
+ - lib/magent/generic_channel.rb
57
60
  - lib/magent/processor.rb
58
61
  - lib/magent/push.rb
59
62
  - lib/magent/utils.rb