acts_as_executor 1.0.0.beta2 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/acts_as_executor.gemspec +8 -1
  2. data/lib/acts_as_executor.rb +18 -31
  3. data/lib/acts_as_executor/common/future_task.rb +2 -2
  4. data/lib/acts_as_executor/common/instance_support_methods.rb +15 -0
  5. data/lib/acts_as_executor/executor/factory.rb +18 -22
  6. data/lib/acts_as_executor/executor/model/class_methods.rb +33 -0
  7. data/lib/acts_as_executor/executor/model/instance_methods.rb +87 -0
  8. data/lib/acts_as_executor/executor/model/instance_support_methods.rb +36 -0
  9. data/lib/acts_as_executor/task/clazz.rb +26 -0
  10. data/lib/acts_as_executor/task/model/class_methods.rb +13 -0
  11. data/lib/acts_as_executor/task/model/instance_methods.rb +64 -0
  12. data/lib/acts_as_executor/task/model/instance_support_methods.rb +32 -0
  13. data/lib/acts_as_executor/validators/class_exists_validator.rb +1 -1
  14. data/lib/acts_as_executor/validators/class_includes_validator.rb +10 -0
  15. data/lib/acts_as_executor/version.rb +1 -1
  16. data/lib/generators/{executor_generator.rb → acts_as_executor_generator.rb} +1 -5
  17. data/lib/generators/templates/migration/create_executors.rb +2 -2
  18. data/spec/acts_as_executor_spec.rb +22 -0
  19. data/spec/common/future_task_spec.rb +21 -0
  20. data/spec/common/instance_support_methods_spec.rb +25 -0
  21. data/spec/common/units_spec.rb +19 -0
  22. data/spec/executor/factory_spec.rb +31 -0
  23. data/spec/executor/model/class_methods_spec.rb +49 -0
  24. data/spec/executor/model/instance_methods_spec.rb +150 -0
  25. data/spec/executor/model/instance_support_methods_spec.rb +70 -0
  26. data/spec/spec_helper.rb +8 -0
  27. data/spec/support/blueprints.rb +20 -0
  28. data/spec/support/classes.rb +55 -0
  29. data/spec/support/custom_matchers.rb +26 -0
  30. data/spec/support/database_connection.rb +4 -0
  31. data/spec/task/clazz_spec.rb +42 -0
  32. data/spec/task/model/class_methods_spec.rb +13 -0
  33. data/spec/task/model/instance_methods_spec.rb +80 -0
  34. data/spec/task/model/instance_support_methods_spec.rb +56 -0
  35. data/spec/task/schedules_spec.rb +11 -0
  36. data/spec/validators/class_exists_validator_spec.rb +21 -0
  37. data/spec/validators/class_includes_validator_spec.rb +21 -0
  38. metadata +88 -21
  39. data/README.rdoc +0 -67
  40. data/lib/acts_as_executor/executor/kinds.rb +0 -16
  41. data/lib/acts_as_executor/executor/model/actions.rb +0 -65
  42. data/lib/acts_as_executor/executor/model/associations.rb +0 -11
  43. data/lib/acts_as_executor/executor/model/attributes.rb +0 -16
  44. data/lib/acts_as_executor/executor/model/base.rb +0 -24
  45. data/lib/acts_as_executor/executor/model/helpers.rb +0 -25
  46. data/lib/acts_as_executor/executor/model/validations.rb +0 -13
  47. data/lib/acts_as_executor/task/model/actions.rb +0 -34
  48. data/lib/acts_as_executor/task/model/associations.rb +0 -11
  49. data/lib/acts_as_executor/task/model/attributes.rb +0 -16
  50. data/lib/acts_as_executor/task/model/base.rb +0 -15
  51. data/lib/acts_as_executor/task/model/helpers.rb +0 -14
  52. data/lib/acts_as_executor/task/model/validations.rb +0 -16
  53. data/lib/generators/templates/initializer/acts_as_executor.rb +0 -4
@@ -0,0 +1,8 @@
1
+ require "acts_as_executor"
2
+
3
+ require "machinist/active_record"
4
+
5
+ require "support/database_connection"
6
+ require "support/classes"
7
+ require "support/blueprints"
8
+ require "support/custom_matchers"
@@ -0,0 +1,20 @@
1
+ InstanceSupportMethods.blueprint do
2
+ end
3
+
4
+ Executor.blueprint do
5
+ id { "#{sn}" }
6
+ name { "name#{sn}" }
7
+ max_tasks { 1 }
8
+ end
9
+
10
+ ExecutorTask.blueprint do
11
+ id { "#{sn}" }
12
+ executor
13
+ clazz { "Clazz" }
14
+ arguments { {:attribute_one => "attribute_one_value", :attribute_two => "attribute_two_value"} }
15
+ end
16
+
17
+ Clazz.blueprint do
18
+ arguments { {:attribute_one => "attribute_one_value", :attribute_two => "attribute_two_value"} }
19
+ object.stub :execute
20
+ end
@@ -0,0 +1,55 @@
1
+ # Rails
2
+ Object.const_set "Rails", Class.new
3
+
4
+ # Logger
5
+ class Logger
6
+ def debug message = nil, █ end
7
+ def info message = nil, █ end
8
+ def warn message = nil, █ end
9
+ def error message = nil, █ end
10
+ def fatal message = nil, █ end
11
+ end
12
+
13
+ # InstanceSupportMethods
14
+ Object.const_set "InstanceSupportMethods", Class.new
15
+ InstanceSupportMethods.send :extend, Machinist::Machinable
16
+ InstanceSupportMethods.send :include, ActsAsExecutor::Common::InstanceSupportMethods
17
+
18
+ # Executor
19
+ Object.const_set "Executor", Class.new(ActiveRecord::Base)
20
+ ActiveRecord::Schema.define do
21
+ self.verbose = false
22
+
23
+ create_table :executors, :force => true do |t|
24
+ t.string :name
25
+ t.integer :max_tasks
26
+ t.boolean :schedulable
27
+ end
28
+ end
29
+ Executor.acts_as_executor
30
+ Executor.log = Logger.new
31
+
32
+ # ExecutorWithoutActsAsExecutor
33
+ Object.const_set "ExecutorWithoutActsAsExecutor", Class.new(ActiveRecord::Base)
34
+
35
+ # ExecutorTask
36
+ Object.const_set "ExecutorTask", Class.new(ActiveRecord::Base)
37
+ ActiveRecord::Schema.define do
38
+ self.verbose = false
39
+
40
+ create_table :executor_tasks, :force => true do |t|
41
+ t.integer :executor_id
42
+ t.string :clazz
43
+ t.string :arguments
44
+ t.string :schedule
45
+ t.integer :start
46
+ t.integer :every
47
+ t.string :units
48
+ end
49
+ end
50
+ ExecutorTask.acts_as_executor_task
51
+
52
+ # Clazz
53
+ Object.const_set "Clazz", Class.new
54
+ Clazz.send :extend, Machinist::Machinable
55
+ Clazz.send :include, ActsAsExecutor::Task::Clazz
@@ -0,0 +1,26 @@
1
+ RSpec::Matchers.define :allow_public_access_for_methods do |*methods|
2
+ match_for_should do |object|
3
+ responded_to_all = true
4
+ methods.each do |method|
5
+ responded_to_all = false unless object.respond_to? method
6
+ end
7
+ responded_to_all
8
+ end
9
+
10
+ match_for_should_not do |object|
11
+ not_responded_to_all = true
12
+ methods.each do |method|
13
+ not_responded_to_all = false if object.respond_to? method
14
+ end
15
+ not_responded_to_all
16
+ end
17
+ end
18
+
19
+ def log_statement executor_name, statement
20
+ "\"" + executor_name + "\" " + statement
21
+ end
22
+
23
+ def log_message executor_name, doing, task_id, clazz_name, message = ""
24
+ if message.kind_of? Hash then message = "with \"" + message.inspect + "\"" end
25
+ "\"" + executor_name + "\" " + doing + " \"" + task_id + "#" + clazz_name + "\" " + message
26
+ end
@@ -0,0 +1,4 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => "jdbcsqlite3",
3
+ :database => ":memory:"
4
+ )
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ describe ActsAsExecutor::Task::Clazz do
4
+ before(:each) { @model = Clazz.make }
5
+
6
+ it { @model.should be_a Java::java.lang.Runnable }
7
+ it { @model.should_not allow_public_access_for_methods :run }
8
+
9
+ describe "#run" do
10
+ it "should invoke execute" do
11
+ @model.should_receive :execute
12
+
13
+ @model.send :run
14
+ end
15
+ it "should create accessor for each arguments member" do
16
+ @model.send :run
17
+
18
+ @model.arguments.each_pair do |key, value|
19
+ @model.should respond_to key
20
+ @model.send(key).should == value
21
+ end
22
+ end
23
+
24
+ context "when any exception is thrown" do
25
+ context "when uncaught exception handler exists" do
26
+ it "should invoke handler" do
27
+ handler = double "Handler"
28
+ handler.stub :uncaught_exception_handler
29
+ @model.uncaught_exception_handler = handler.method :uncaught_exception_handler
30
+
31
+ exception = RuntimeError.new
32
+
33
+ @model.should_receive(:execute).and_raise exception
34
+
35
+ handler.should_receive(:uncaught_exception_handler).with exception
36
+
37
+ @model.send :run
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe ActsAsExecutor::Task::Model::ClassMethods do
4
+ subject { ActiveRecord::Base }
5
+ it { should be_a ActsAsExecutor::Task::Model::ClassMethods }
6
+
7
+ describe "#acts_as_executor_task" do
8
+ subject { ExecutorTask }
9
+ it { should include ActsAsExecutor::Common::InstanceSupportMethods }
10
+ it { should include ActsAsExecutor::Task::Model::InstanceMethods }
11
+ it { should include ActsAsExecutor::Task::Model::InstanceSupportMethods }
12
+ end
13
+ end
@@ -0,0 +1,80 @@
1
+ require "spec_helper"
2
+
3
+ describe ActsAsExecutor::Task::Model::InstanceMethods do
4
+ before(:each) { @model = ExecutorTask.make }
5
+
6
+ it { @model.should_not allow_public_access_for_methods :enqueue, :instantiate, :cancel, :done_handler, :uncaught_exception_handler }
7
+
8
+ describe "#enqueue" do
9
+ before(:each) { @clazz = Clazz.make }
10
+
11
+ it "should enqueue task" do
12
+ @model.send(:future).should be_nil
13
+
14
+ future = double "Future"
15
+ future.stub :done_handler=
16
+
17
+ @model.executor.send(:log).should_receive(:debug).with log_message(@model.executor.name, "creating", @model.id.to_s, @model.clazz, @model.arguments)
18
+ @model.should_receive(:instantiate).and_return @clazz
19
+ @model.executor.should_receive(:execute).with(@clazz, @model.id.to_s, @model.schedule, @model.start, @model.every, @model.units).and_return future
20
+
21
+ @model.send :enqueue
22
+
23
+ @model.send(:future).should == future
24
+ end
25
+
26
+ context "when any exception is thrown" do
27
+ it "should log exception" do
28
+ exception = RuntimeError.new
29
+
30
+ @model.should_receive(:instantiate).and_raise exception
31
+ @model.executor.send(:log).should_receive(:error).with log_message(@model.executor.name, "creating", @model.id.to_s, @model.clazz, "encountered an unexpected exception. " + exception)
32
+
33
+ @model.send :enqueue
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#instantiate" do
39
+ it "should return an instance" do
40
+ instance = @model.send :instantiate, @model.clazz, @model.arguments
41
+
42
+ instance.class.name.should == @model.clazz
43
+ instance.arguments.should == @model.arguments
44
+ end
45
+ end
46
+
47
+ describe "#done_handler" do
48
+ it "should invoke destroy" do
49
+ @model.executor.send(:log).should_receive(:debug).with log_message(@model.executor.name, "completed", @model.id.to_s, @model.clazz)
50
+ @model.should_receive :destroy
51
+
52
+ @model.send :done_handler
53
+ end
54
+ end
55
+
56
+ describe "#uncaught_exception_handler" do
57
+ before(:each) { @clazz = Clazz.make }
58
+
59
+ it "should log exception" do
60
+ exception = RuntimeError.new
61
+
62
+ @model.executor.send(:log).should_receive(:error).with log_message(@model.executor.name, "executing", @model.id.to_s, @model.clazz, "encountered an uncaught exception. " + exception)
63
+
64
+ @model.send :uncaught_exception_handler, exception
65
+ end
66
+ end
67
+
68
+ describe "#cancel" do
69
+ it "should cancel task" do
70
+ future = double "Future"
71
+ @model.send :future=, future
72
+
73
+ @model.send(:future).should_receive(:cancel).with true
74
+
75
+ @model.send :cancel
76
+
77
+ @model.send(:future).should be_nil
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,56 @@
1
+ require "spec_helper"
2
+
3
+ describe ActsAsExecutor::Task::Model::InstanceSupportMethods do
4
+ before(:each) { @model = ExecutorTask.make }
5
+
6
+ it { @model.should_not allow_public_access_for_methods :future, :future=, :enqueueable?, :cancelable? }
7
+
8
+ describe "#future" do
9
+ it "should return future" do
10
+ future = double "Future"
11
+ @model.send :future=, future
12
+
13
+ @model.send(:future).should == future
14
+ end
15
+
16
+ context "when id has not been set" do
17
+ it "should raise an argument error" do
18
+ @model.id = nil
19
+
20
+ expect { @model.send :future=, true }.to raise_error ArgumentError, "cannot reference future against nil id"
21
+ end
22
+ end
23
+ end
24
+
25
+ describe "#enqueueable?" do
26
+ context "when future has not been set" do
27
+ it "should return true" do
28
+ @model.should be_enqueueable
29
+ end
30
+ end
31
+
32
+ context "when future has been set" do
33
+ it "should return false" do
34
+ @model.send :future=, true
35
+
36
+ @model.should_not be_enqueueable
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "#cancelable?" do
42
+ context "when future has been set" do
43
+ it "should return true" do
44
+ @model.send :future=, true
45
+
46
+ @model.should be_cancelable
47
+ end
48
+ end
49
+
50
+ context "when future has not been set" do
51
+ it "should return false" do
52
+ @model.should_not be_cancelable
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe ActsAsExecutor::Task::Schedules do
4
+ it { ActsAsExecutor::Task::Schedules::ONE_SHOT.should == "one_shot" }
5
+ it { ActsAsExecutor::Task::Schedules::FIXED_DELAY.should == "fixed_delay" }
6
+ it { ActsAsExecutor::Task::Schedules::FIXED_RATE.should == "fixed_rate" }
7
+ it { ActsAsExecutor::Task::Schedules::ALL.should include ActsAsExecutor::Task::Schedules::ONE_SHOT,
8
+ ActsAsExecutor::Task::Schedules::FIXED_DELAY,
9
+ ActsAsExecutor::Task::Schedules::FIXED_RATE
10
+ }
11
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe ClassExistsValidator do
4
+ before(:each) { @model = ClassExistsValidator.new Hash[ :attributes => true ] }
5
+
6
+ it { @model.should be_a ActiveModel::EachValidator }
7
+
8
+ describe "#validate_each" do
9
+ context "when class does not exist" do
10
+ it "should set error message" do
11
+ attribute = "clazz"
12
+ record = double "Record"
13
+ record.stub(:errors).and_return( { attribute => [] } )
14
+
15
+ @model.validate_each record, attribute, "NonexistentClass"
16
+
17
+ record.errors[attribute].should include "does not exist as a Class"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe ClassIncludesValidator do
4
+ before(:each) { @model = ClassIncludesValidator.new Hash[ :attributes => true, :includes => Comparable ] }
5
+
6
+ it { @model.should be_a ActiveModel::EachValidator }
7
+
8
+ describe "#validate_each" do
9
+ context "when class does not include" do
10
+ it "should set error message" do
11
+ attribute = "clazz"
12
+ record = double "Record"
13
+ record.stub(:errors).and_return( { attribute => [] } )
14
+
15
+ @model.validate_each record, attribute, "Object"
16
+
17
+ record.errors[attribute].should include "does not include Comparable"
18
+ end
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: acts_as_executor
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 1.0.0.beta2
5
+ version: 1.0.0.rc1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Phil Ostler
@@ -10,11 +10,65 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-05 00:00:00 +01:00
13
+ date: 2011-08-25 00:00:00 +01:00
14
14
  default_executable:
15
- dependencies: []
16
-
17
- description: Seamless integration of Java's Executor framework with JRuby on Rails
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activemodel
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "3.0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "3.0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: activerecord-jdbcsqlite3-adapter
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: "1.1"
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: machinist
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - "="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.0.0.beta2
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rspec
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: "2.6"
69
+ type: :development
70
+ version_requirements: *id005
71
+ description: Seamlessly integrates Java's Executor framework with JRuby on Rails
18
72
  email:
19
73
  - philostler@gmail.com
20
74
  executables: []
@@ -27,30 +81,43 @@ files:
27
81
  - lib/acts_as_executor.rb
28
82
  - lib/acts_as_executor/version.rb
29
83
  - lib/acts_as_executor/common/future_task.rb
84
+ - lib/acts_as_executor/common/instance_support_methods.rb
30
85
  - lib/acts_as_executor/common/units.rb
31
86
  - lib/acts_as_executor/executor/factory.rb
32
- - lib/acts_as_executor/executor/kinds.rb
33
- - lib/acts_as_executor/executor/model/actions.rb
34
- - lib/acts_as_executor/executor/model/associations.rb
35
- - lib/acts_as_executor/executor/model/attributes.rb
36
- - lib/acts_as_executor/executor/model/base.rb
37
- - lib/acts_as_executor/executor/model/helpers.rb
38
- - lib/acts_as_executor/executor/model/validations.rb
87
+ - lib/acts_as_executor/executor/model/class_methods.rb
88
+ - lib/acts_as_executor/executor/model/instance_methods.rb
89
+ - lib/acts_as_executor/executor/model/instance_support_methods.rb
90
+ - lib/acts_as_executor/task/clazz.rb
39
91
  - lib/acts_as_executor/task/schedules.rb
40
- - lib/acts_as_executor/task/model/actions.rb
41
- - lib/acts_as_executor/task/model/associations.rb
42
- - lib/acts_as_executor/task/model/attributes.rb
43
- - lib/acts_as_executor/task/model/base.rb
44
- - lib/acts_as_executor/task/model/helpers.rb
45
- - lib/acts_as_executor/task/model/validations.rb
92
+ - lib/acts_as_executor/task/model/class_methods.rb
93
+ - lib/acts_as_executor/task/model/instance_methods.rb
94
+ - lib/acts_as_executor/task/model/instance_support_methods.rb
46
95
  - lib/acts_as_executor/validators/class_exists_validator.rb
47
- - lib/generators/executor_generator.rb
48
- - lib/generators/templates/initializer/acts_as_executor.rb
96
+ - lib/acts_as_executor/validators/class_includes_validator.rb
97
+ - lib/generators/acts_as_executor_generator.rb
49
98
  - lib/generators/templates/migration/create_executors.rb
50
99
  - lib/generators/templates/models/executor.rb
51
100
  - lib/generators/templates/models/executor_task.rb
101
+ - spec/acts_as_executor_spec.rb
52
102
  - spec/spec_helper.rb
53
- - README.rdoc
103
+ - spec/common/future_task_spec.rb
104
+ - spec/common/instance_support_methods_spec.rb
105
+ - spec/common/units_spec.rb
106
+ - spec/executor/factory_spec.rb
107
+ - spec/executor/model/class_methods_spec.rb
108
+ - spec/executor/model/instance_methods_spec.rb
109
+ - spec/executor/model/instance_support_methods_spec.rb
110
+ - spec/support/blueprints.rb
111
+ - spec/support/classes.rb
112
+ - spec/support/custom_matchers.rb
113
+ - spec/support/database_connection.rb
114
+ - spec/task/clazz_spec.rb
115
+ - spec/task/schedules_spec.rb
116
+ - spec/task/model/class_methods_spec.rb
117
+ - spec/task/model/instance_methods_spec.rb
118
+ - spec/task/model/instance_support_methods_spec.rb
119
+ - spec/validators/class_exists_validator_spec.rb
120
+ - spec/validators/class_includes_validator_spec.rb
54
121
  - LICENSE
55
122
  - acts_as_executor.gemspec
56
123
  has_rdoc: true