say_when 1.0.0 → 2.0.0

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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +5 -0
  4. data/Guardfile +50 -0
  5. data/README.md +135 -2
  6. data/Rakefile +1 -0
  7. data/lib/say_when.rb +33 -18
  8. data/lib/say_when/configuration.rb +16 -0
  9. data/lib/say_when/cron_expression.rb +19 -21
  10. data/lib/say_when/poller/base_poller.rb +108 -0
  11. data/lib/say_when/poller/celluloid_poller.rb +30 -0
  12. data/lib/say_when/poller/concurrent_poller.rb +31 -0
  13. data/lib/say_when/poller/simple_poller.rb +37 -0
  14. data/lib/say_when/processor/active_job_strategy.rb +35 -0
  15. data/lib/say_when/processor/simple_strategy.rb +13 -0
  16. data/lib/say_when/processor/test_strategy.rb +21 -0
  17. data/lib/say_when/scheduler.rb +67 -101
  18. data/lib/say_when/storage/active_record_strategy.rb +204 -0
  19. data/lib/say_when/storage/base_job.rb +96 -0
  20. data/lib/say_when/storage/memory_strategy.rb +140 -0
  21. data/lib/say_when/tasks.rb +15 -3
  22. data/lib/say_when/triggers/base.rb +3 -3
  23. data/lib/say_when/triggers/cron_strategy.rb +2 -3
  24. data/lib/say_when/triggers/instance_strategy.rb +3 -4
  25. data/lib/say_when/triggers/once_strategy.rb +3 -4
  26. data/lib/say_when/utils.rb +16 -0
  27. data/lib/say_when/version.rb +1 -1
  28. data/say_when.gemspec +10 -5
  29. data/test/minitest_helper.rb +45 -15
  30. data/test/say_when/configuration_test.rb +14 -0
  31. data/test/say_when/cron_expression_test.rb +140 -0
  32. data/test/say_when/poller/base_poller_test.rb +42 -0
  33. data/test/say_when/poller/celluloid_poller_test.rb +17 -0
  34. data/test/say_when/poller/concurrent_poller_test.rb +19 -0
  35. data/test/say_when/poller/simple_poller_test.rb +27 -0
  36. data/test/say_when/processor/active_job_strategy_test.rb +31 -0
  37. data/test/say_when/processor/simple_strategy_test.rb +15 -0
  38. data/test/say_when/scheduler_test.rb +41 -57
  39. data/test/say_when/storage/active_record_strategy_test.rb +134 -0
  40. data/test/say_when/storage/memory_strategy_test.rb +96 -0
  41. data/test/say_when/triggers/cron_strategy_test.rb +11 -0
  42. data/test/say_when/triggers/instance_strategy_test.rb +13 -0
  43. data/test/say_when/triggers/once_strategy_test.rb +2 -2
  44. data/test/say_when_test.rb +20 -0
  45. metadata +110 -36
  46. data/lib/say_when/base_job.rb +0 -96
  47. data/lib/say_when/processor/active_messaging.rb +0 -21
  48. data/lib/say_when/processor/base.rb +0 -19
  49. data/lib/say_when/processor/shoryuken.rb +0 -14
  50. data/lib/say_when/processor/simple.rb +0 -17
  51. data/lib/say_when/storage/active_record/acts.rb +0 -92
  52. data/lib/say_when/storage/active_record/job.rb +0 -100
  53. data/lib/say_when/storage/active_record/job_execution.rb +0 -14
  54. data/lib/say_when/storage/memory/base.rb +0 -36
  55. data/lib/say_when/storage/memory/job.rb +0 -53
  56. data/test/say_when/cron_expression_spec.rb +0 -74
  57. data/test/say_when/processor/active_messaging_test.rb +0 -41
  58. data/test/say_when/storage/active_record/job_test.rb +0 -90
  59. data/test/say_when/storage/memory/job_test.rb +0 -32
  60. data/test/say_when/storage/memory/trigger_test.rb +0 -54
  61. data/test/support/models.rb +0 -33
@@ -1,41 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'minitest_helper'
4
-
5
- require 'say_when/processor/active_messaging'
6
- require 'say_when/storage/active_record/job'
7
- require 'activemessaging'
8
-
9
- ActiveMessaging.logger = Logger.new('/dev/null')
10
- ActiveMessaging.load_extensions
11
-
12
- def destination(destination_name)
13
- d = ActiveMessaging::Gateway.find_destination(destination_name).value
14
- ActiveMessaging::Gateway.connection('default').find_destination d
15
- end
16
-
17
- describe SayWhen::Processor::ActiveMessaging do
18
-
19
- before do
20
-
21
- ActiveMessaging::Gateway.connections['default'] = ActiveMessaging::Gateway.adapters[:test].new({})
22
-
23
- ActiveMessaging::Gateway.define do |s|
24
- s.destination :say_when, '/queue/SayWhen'
25
- end
26
-
27
- SayWhen::Scheduler.configure do |scheduler|
28
- scheduler.storage_strategy = :active_record
29
- scheduler.processor_class = SayWhen::Processor::ActiveMessaging
30
- end
31
- @processor = SayWhen::Processor::ActiveMessaging.new(SayWhen::Scheduler.scheduler)
32
- end
33
-
34
- it 'process a job by sending a message' do
35
- @job = Minitest::Mock.new
36
- @job.expect(:id, 100)
37
- @processor.process(@job)
38
- destination(:say_when).messages.size.must_equal 1
39
- YAML::load(destination(:say_when).messages.first.body)[:job_id].must_equal 100
40
- end
41
- end
@@ -1,90 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'minitest_helper'
4
- require 'active_record_helper'
5
- require 'say_when/storage/active_record/job'
6
-
7
- describe SayWhen::Storage::ActiveRecord::Job do
8
-
9
- let(:valid_attributes) {
10
- {
11
- trigger_strategy: :cron,
12
- trigger_options: { expression: '0 0 12 ? * * *', time_zone: 'Pacific Time (US & Canada)' },
13
- data: { foo: 'bar', result: 1 },
14
- job_class: 'SayWhen::Test::TestTask',
15
- job_method: 'execute'
16
- }
17
- }
18
-
19
- it 'can be instantiated' do
20
- j = SayWhen::Storage::ActiveRecord::Job.create!(valid_attributes)
21
- j.wont_be_nil
22
- end
23
-
24
- it 'can execute the task for the job' do
25
- j = SayWhen::Storage::ActiveRecord::Job.create(valid_attributes)
26
- j.execute_job( { result: 1 } ).must_equal 1
27
- end
28
-
29
- it 'can execute the job' do
30
- j = SayWhen::Storage::ActiveRecord::Job.create(valid_attributes)
31
- j.execute.must_equal 1
32
- end
33
-
34
- it 'derives a trigger from the attributes' do
35
- t = SayWhen::Storage::ActiveRecord::Job.create(valid_attributes)
36
- t.trigger.wont_be_nil
37
- t.trigger.must_be_instance_of SayWhen::Triggers::CronStrategy
38
- end
39
-
40
- it 'has a waiting state on create' do
41
- t = SayWhen::Storage::ActiveRecord::Job.create(valid_attributes)
42
- t.status.must_equal SayWhen::BaseJob::STATE_WAITING
43
- end
44
-
45
- it 'has a next fire at set on create' do
46
- opts = valid_attributes[:trigger_options]
47
- ce = SayWhen::CronExpression.new(opts[:expression], opts[:time_zone])
48
- j = SayWhen::Storage::ActiveRecord::Job.create(valid_attributes)
49
- j.status.must_equal SayWhen::BaseJob::STATE_WAITING
50
- j.next_fire_at.must_equal ce.next_fire_at
51
- end
52
-
53
- it 'can find the next job' do
54
- SayWhen::Storage::ActiveRecord::Job.delete_all
55
- j2_opts = {
56
- trigger_strategy: :cron,
57
- trigger_options: { expression: '0 0 10 ? * * *', time_zone: 'Pacific Time (US & Canada)' },
58
- data: { foo: 'bar', result: 2 },
59
- job_class: 'SayWhen::Test::TestTask',
60
- job_method: 'execute'
61
- }
62
-
63
- now = Time.now.change(hour: 0)
64
- Time.stub(:now, now) do
65
- j1 = SayWhen::Storage::ActiveRecord::Job.create(valid_attributes)
66
- j2 = SayWhen::Storage::ActiveRecord::Job.create(j2_opts)
67
-
68
- next_job = SayWhen::Storage::ActiveRecord::Job.acquire_next(2.days.since)
69
- next_job.must_equal j2
70
- end
71
- end
72
-
73
- it 'can be fired' do
74
- opts = valid_attributes[:trigger_options]
75
- ce = SayWhen::CronExpression.new(opts[:expression], opts[:time_zone])
76
- j = SayWhen::Storage::ActiveRecord::Job.create(valid_attributes)
77
- nfa = ce.last_fire_at(j.created_at - 1.second)
78
- lfa = ce.last_fire_at(nfa - 1.second)
79
- j.next_fire_at = nfa
80
- j.last_fire_at = lfa
81
-
82
- now = Time.now
83
- Time.stub(:now, now) do
84
- j.fired
85
- j.next_fire_at.must_equal ce.next_fire_at(now)
86
- j.last_fire_at.must_equal now
87
- j.status.must_equal SayWhen::BaseJob::STATE_WAITING
88
- end
89
- end
90
- end
@@ -1,32 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'minitest_helper'
4
- require 'say_when/storage/memory/job'
5
-
6
- describe SayWhen::Storage::Memory::Job do
7
-
8
- let(:valid_attributes) {
9
- {
10
- :name => 'Memory::Job::Test',
11
- :group => 'Test',
12
- :data => { foo: 'bar', result: 1 },
13
- :job_class => 'SayWhen::Test::TestTask',
14
- :job_method => 'execute'
15
- }
16
- }
17
-
18
- it 'can be instantiated' do
19
- j = SayWhen::Storage::Memory::Job.new(valid_attributes)
20
- j.wont_be_nil
21
- end
22
-
23
- it 'can execute the task for the job' do
24
- j = SayWhen::Storage::Memory::Job.new(valid_attributes)
25
- j.execute_job( { result: 1 } ).must_equal 1
26
- end
27
-
28
- it 'can execute the job' do
29
- j = SayWhen::Storage::Memory::Job.new(valid_attributes)
30
- j.execute.must_equal 1
31
- end
32
- end
@@ -1,54 +0,0 @@
1
- # require 'minitest_helper'
2
- # require 'say_when/storage/memory/trigger'
3
-
4
- # describe SayWhen::Storage::Memory::Trigger do
5
-
6
- # let(:valid_attributes) {
7
- # {
8
- # expression: '0 0 12 ? * * *',
9
- # time_zone: 'Pacific Time (US & Canada)'
10
- # }
11
- # }
12
-
13
- # it 'can be instantiated' do
14
- # t = SayWhen::Storage::Memory::Trigger.new(@valid_attributes)
15
- # t.wont_be_nil
16
- # end
17
-
18
- # it 'sets a cron_expression' do
19
- # t = SayWhen::Storage::Memory::Trigger.new(@valid_attributes)
20
- # t.cron_expression.wont_be_nil
21
- # t.cron_expression.expression.must_equal '0 0 12 ? * * *'
22
- # t.cron_expression.time_zone.must_equal 'Pacific Time (US & Canada)'
23
- # end
24
-
25
- # it 'has a waiting state on instantiate' do
26
- # t = SayWhen::Storage::Memory::Trigger.new(@valid_attributes)
27
- # t.status.must_equal SayWhen::BaseTrigger::STATE_WAITING
28
- # end
29
-
30
- # it 'has a next fire at set on instantiate' do
31
- # ce = SayWhen::CronExpression.new(@valid_attributes[:expression], @valid_attributes[:time_zone])
32
- # t = SayWhen::Storage::Memory::Trigger.new(@valid_attributes)
33
- # t.status.must_equal SayWhen::BaseTrigger::STATE_WAITING
34
- # t.next_fire_at.must_equal ce.next_fire_at
35
- # end
36
-
37
- # it 'can be fired' do
38
- # ce = SayWhen::CronExpression.new(@valid_attributes[:expression], @valid_attributes[:time_zone])
39
- # t = SayWhen::Storage::Memory::Trigger.new(@valid_attributes)
40
- # nfa = ce.last_fire_at(1.second.ago)
41
- # lfa = ce.last_fire_at(nfa - 1.second)
42
- # t.next_fire_at = nfa
43
- # t.last_fire_at = lfa
44
-
45
- # now = Time.now
46
- # Time.stub!(:now).and_return(now)
47
-
48
- # t.fired
49
- # t.next_fire_at.must_equal ce.next_fire_at(now)
50
- # t.last_fire_at.must_equal now
51
- # t.status.must_equal SayWhen::BaseTrigger::STATE_WAITING
52
- # end
53
-
54
- # end
@@ -1,33 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'active_support'
4
-
5
- class TestModel < Object
6
- attr_accessor :id
7
-
8
- def initialize(id=nil)
9
- @id = id
10
- end
11
-
12
- def ==(b)
13
- return false unless b
14
- # puts "compare: #{self.class.name}_#{@id} == #{b.class.name}_#{b.id}"
15
- @id == b.id
16
- end
17
-
18
- class << self
19
-
20
- def find(ids=nil)
21
- Array(ids).collect{|i| self.new(i)}
22
- end
23
-
24
- end
25
-
26
- end
27
-
28
- class User < TestModel
29
- end
30
-
31
- class Account < TestModel
32
- attr_accessor :owner
33
- end