magent 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/simple/bot.rb +3 -0
- data/lib/magent.rb +2 -1
- data/lib/magent/actor.rb +30 -0
- data/lib/magent/processor.rb +36 -10
- data/magent.gemspec +2 -2
- metadata +2 -2
data/examples/simple/bot.rb
CHANGED
data/lib/magent.rb
CHANGED
@@ -10,7 +10,7 @@ require 'magent/actor'
|
|
10
10
|
require 'magent/processor'
|
11
11
|
|
12
12
|
module Magent
|
13
|
-
VERSION = '0.0
|
13
|
+
VERSION = '0.1.0'
|
14
14
|
|
15
15
|
def self.connection
|
16
16
|
@@connection ||= Mongo::Connection.new(nil, nil, :auto_reconnect => true)
|
@@ -30,3 +30,4 @@ module Magent
|
|
30
30
|
end
|
31
31
|
|
32
32
|
Magent.database = 'magent'
|
33
|
+
|
data/lib/magent/actor.rb
CHANGED
@@ -3,6 +3,7 @@ module Magent
|
|
3
3
|
def self.included(klass)
|
4
4
|
klass.class_eval do
|
5
5
|
extend Actor::ClassMethods
|
6
|
+
include Actor::InstanceMethods
|
6
7
|
end
|
7
8
|
end
|
8
9
|
|
@@ -32,6 +33,35 @@ module Magent
|
|
32
33
|
Channel.new(channel_name)
|
33
34
|
end
|
34
35
|
end
|
36
|
+
|
37
|
+
def tasks
|
38
|
+
@tasks ||= []
|
39
|
+
end
|
40
|
+
|
41
|
+
def at_least_every(seconds, &block)
|
42
|
+
tasks << {:every => seconds, :last_time => Time.now, :block => block}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module InstanceMethods
|
47
|
+
def _run_tasks
|
48
|
+
tasks = self.class.tasks
|
49
|
+
|
50
|
+
return false if tasks.empty?
|
51
|
+
performed = false
|
52
|
+
|
53
|
+
tasks.each do |task|
|
54
|
+
delta = Time.now - task[:last_time]
|
55
|
+
|
56
|
+
if delta >= task[:every]
|
57
|
+
task[:last_time] = Time.now
|
58
|
+
instance_eval(&task[:block])
|
59
|
+
performed = true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
performed
|
64
|
+
end
|
35
65
|
end
|
36
66
|
end # Actor
|
37
67
|
|
data/lib/magent/processor.rb
CHANGED
@@ -4,6 +4,7 @@ module Magent
|
|
4
4
|
|
5
5
|
def initialize(actor)
|
6
6
|
@actor = actor
|
7
|
+
@shutdown = false
|
7
8
|
|
8
9
|
@actor.class.actions.each do |action|
|
9
10
|
if !@actor.respond_to?(action)
|
@@ -13,30 +14,55 @@ module Magent
|
|
13
14
|
end
|
14
15
|
|
15
16
|
def run!
|
17
|
+
processed_messages = 0
|
16
18
|
delay = 0
|
19
|
+
|
20
|
+
trap('TERM') { shutdown!; exit 0 }
|
21
|
+
trap('SIGINT') { shutdown!; exit 0 }
|
22
|
+
|
17
23
|
loop do
|
18
|
-
|
24
|
+
break if @shutdown
|
25
|
+
|
26
|
+
delay = 0 if @actor._run_tasks
|
19
27
|
|
20
|
-
|
28
|
+
@method, @payload = @actor.class.channel.dequeue
|
29
|
+
|
30
|
+
if @method.nil?
|
21
31
|
delay += 0.1 if delay <= 5
|
22
32
|
else
|
23
33
|
delay = 0
|
24
|
-
$stderr.puts "#{@actor.class}##{method}(#{payload.inspect})"
|
34
|
+
$stderr.puts "#{@actor.class}##{@method}(#{@payload.inspect})"
|
25
35
|
begin
|
26
|
-
if @actor.class.actions.include?(method)
|
27
|
-
|
36
|
+
if @actor.class.actions.include?(@method)
|
37
|
+
processed_messages += 1
|
38
|
+
@actor.send(@method, @payload)
|
39
|
+
|
40
|
+
if processed_messages > 20
|
41
|
+
processed_messages = 0
|
42
|
+
GC.start
|
43
|
+
end
|
28
44
|
else
|
29
|
-
$stderr.puts "Unknown action: #{method} (payload=#{payload.inspect})"
|
45
|
+
$stderr.puts "Unknown action: #{@method} (payload=#{@payload.inspect})"
|
30
46
|
end
|
47
|
+
rescue SystemExit
|
31
48
|
rescue Exception => e
|
32
|
-
$stderr.puts "Error while executing #{method.inspect} #{payload.inspect}"
|
33
|
-
$stderr.puts "#{e.to_s}
|
34
|
-
@actor.class.channel.failed(:message => e.message, :method => method, :payload => payload, :backtrace => e.backtrace)
|
49
|
+
$stderr.puts "Error while executing #{@method.inspect} #{@payload.inspect}"
|
50
|
+
$stderr.puts "#{e.to_s}\n#{e.backtrace.join("\t\n")}"
|
51
|
+
@actor.class.channel.failed(:message => e.message, :method => @method, :payload => @payload, :backtrace => e.backtrace, :date => Time.now.utc)
|
52
|
+
ensure
|
53
|
+
@method, @payload = nil
|
35
54
|
end
|
36
55
|
end
|
37
|
-
|
38
56
|
sleep delay
|
39
57
|
end
|
40
58
|
end
|
59
|
+
|
60
|
+
def shutdown!
|
61
|
+
@shutdown = true
|
62
|
+
$stderr.puts "Shutting down..."
|
63
|
+
if @method
|
64
|
+
@actor.class.channel.enqueue(@method, @payload)
|
65
|
+
end
|
66
|
+
end
|
41
67
|
end #Processor
|
42
68
|
end
|
data/magent.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{magent}
|
5
|
-
s.version = "0.0
|
5
|
+
s.version = "0.1.0"
|
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-
|
9
|
+
s.date = %q{2009-10-14}
|
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
|
4
|
+
version: 0.1.0
|
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-
|
12
|
+
date: 2009-10-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|