say_when 0.3.0 → 1.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 (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -4
  3. data/Gemfile +1 -1
  4. data/LICENSE +21 -0
  5. data/README.md +31 -0
  6. data/Rakefile +9 -1
  7. data/lib/generators/say_when/migration/migration_generator.rb +8 -5
  8. data/lib/generators/say_when/migration/templates/migration.rb +11 -8
  9. data/lib/say_when/base_job.rb +2 -0
  10. data/lib/say_when/cron_expression.rb +129 -191
  11. data/lib/say_when/processor/active_messaging.rb +7 -8
  12. data/lib/say_when/processor/base.rb +5 -3
  13. data/lib/say_when/processor/shoryuken.rb +14 -0
  14. data/lib/say_when/processor/simple.rb +2 -0
  15. data/lib/say_when/railtie.rb +9 -0
  16. data/lib/say_when/scheduler.rb +30 -56
  17. data/lib/say_when/storage/active_record/acts.rb +16 -16
  18. data/lib/say_when/storage/active_record/job.rb +17 -24
  19. data/lib/say_when/storage/active_record/job_execution.rb +5 -8
  20. data/lib/say_when/storage/memory/base.rb +3 -1
  21. data/lib/say_when/storage/memory/job.rb +14 -42
  22. data/lib/say_when/tasks.rb +4 -4
  23. data/lib/say_when/triggers/base.rb +4 -3
  24. data/lib/say_when/triggers/cron_strategy.rb +4 -3
  25. data/lib/say_when/triggers/instance_strategy.rb +4 -3
  26. data/lib/say_when/triggers/once_strategy.rb +3 -2
  27. data/lib/say_when/version.rb +3 -1
  28. data/lib/say_when.rb +8 -7
  29. data/lib/tasks/say_when.rake +3 -1
  30. data/say_when.gemspec +25 -19
  31. data/test/active_record_helper.rb +13 -0
  32. data/{spec → test}/db/schema.rb +4 -2
  33. data/{spec/spec_helper.rb → test/minitest_helper.rb} +9 -12
  34. data/test/say_when/cron_expression_spec.rb +74 -0
  35. data/{spec/say_when/processor/active_messaging_spec.rb → test/say_when/processor/active_messaging_test.rb} +16 -13
  36. data/test/say_when/scheduler_test.rb +75 -0
  37. data/test/say_when/storage/active_record/job_test.rb +90 -0
  38. data/test/say_when/storage/memory/job_test.rb +32 -0
  39. data/test/say_when/storage/memory/trigger_test.rb +54 -0
  40. data/test/say_when/triggers/once_strategy_test.rb +23 -0
  41. data/{spec → test}/support/models.rb +5 -3
  42. metadata +166 -153
  43. data/.travis.yml +0 -4
  44. data/generators/say_when_migration/say_when_migration_generator.rb +0 -11
  45. data/generators/say_when_migration/templates/migration.rb +0 -48
  46. data/spec/active_record_spec_helper.rb +0 -11
  47. data/spec/say_when/cron_expression_spec.rb +0 -72
  48. data/spec/say_when/scheduler_spec.rb +0 -76
  49. data/spec/say_when/storage/active_record/job_spec.rb +0 -98
  50. data/spec/say_when/storage/memory/job_spec.rb +0 -45
  51. data/spec/say_when/storage/memory/trigger_spec.rb +0 -54
  52. data/spec/say_when/triggers/once_strategy_spec.rb +0 -22
  53. data/spec/spec.opts +0 -4
@@ -1,98 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../../spec_helper'
2
- require File.dirname(__FILE__) + '/../../../active_record_spec_helper'
3
- require File.dirname(__FILE__) + '/../../../../lib/say_when/storage/active_record/job'
4
-
5
- describe SayWhen::Storage::ActiveRecord::Job do
6
-
7
- before(:each) do
8
- SayWhen::Storage::ActiveRecord::Job.delete_all
9
-
10
- @valid_attributes = {
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
- end
18
-
19
- it "can be instantiated" do
20
- j = SayWhen::Storage::ActiveRecord::Job.create!(@valid_attributes)
21
- j.should_not 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}).should == 1
27
- end
28
-
29
- it "can execute the job" do
30
- j = SayWhen::Storage::ActiveRecord::Job.create(@valid_attributes)
31
- j.execute.should == 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.should_not be_nil
37
- t.trigger.should be_a 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.should == 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.should == SayWhen::BaseJob::STATE_WAITING
50
- j.next_fire_at.should == ce.next_fire_at
51
- end
52
-
53
- it "resets acquired jobs" do
54
- old = 2.hours.ago
55
- j = SayWhen::Storage::ActiveRecord::Job.create!(@valid_attributes.merge({
56
- :status => 'acquired', :updated_at => old, :created_at => old
57
- }))
58
-
59
- SayWhen::Storage::ActiveRecord::Job.reset_acquired(3600)
60
-
61
- j.reload
62
- j.status.should == 'waiting'
63
- end
64
-
65
- it "can find the next job" do
66
- j2_opts = {
67
- :trigger_strategy => :cron,
68
- :trigger_options => {:expression => '0 0 10 ? * * *', :time_zone => 'Pacific Time (US & Canada)'},
69
- :data => {:foo=>'bar', :result=>2},
70
- :job_class => 'SayWhen::Test::TestTask',
71
- :job_method => 'execute'
72
- }
73
-
74
- j1 = SayWhen::Storage::ActiveRecord::Job.create(@valid_attributes)
75
- j2 = SayWhen::Storage::ActiveRecord::Job.create(j2_opts)
76
- next_job = SayWhen::Storage::ActiveRecord::Job.acquire_next(1.day.since)
77
- next_job.should == j2
78
- end
79
-
80
- it "can be fired" do
81
- opts = @valid_attributes[:trigger_options]
82
- ce = SayWhen::CronExpression.new(opts[:expression], opts[:time_zone])
83
- j = SayWhen::Storage::ActiveRecord::Job.create(@valid_attributes)
84
- nfa = ce.last_fire_at(j.created_at - 1.second)
85
- lfa = ce.last_fire_at(nfa - 1.second)
86
- j.next_fire_at = nfa
87
- j.last_fire_at = lfa
88
-
89
- now = Time.now
90
- Time.stub!(:now).and_return(now)
91
-
92
- j.fired
93
- j.next_fire_at.should == ce.next_fire_at(now)
94
- j.last_fire_at.should == now
95
- j.status.should == SayWhen::BaseJob::STATE_WAITING
96
- end
97
-
98
- end
@@ -1,45 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../../spec_helper'
2
- require File.dirname(__FILE__) + '/../../../../lib/say_when/storage/memory/job'
3
-
4
- describe SayWhen::Store::Memory::Job do
5
-
6
- before(:each) do
7
- SayWhen::Store::Memory::Job._reset
8
- @valid_attributes = {
9
- :name => 'Memory::Job::Test',
10
- :group => 'Test',
11
- :data => {:foo=>'bar', :result=>1},
12
- :job_class => 'SayWhen::Test::TestTask',
13
- :job_method => 'execute'
14
- }
15
- end
16
-
17
- it "can be instantiated" do
18
- j = SayWhen::Store::Memory::Job.new(@valid_attributes)
19
- j.should_not be_nil
20
- end
21
-
22
- it "can execute the task for the job" do
23
- j = SayWhen::Store::Memory::Job.new(@valid_attributes)
24
- j.execute_job({:result=>1}).should == 1
25
- end
26
-
27
- it "can execute the job" do
28
- j = SayWhen::Store::Memory::Job.new(@valid_attributes)
29
- j.execute.should == 1
30
- end
31
-
32
- it "can reset acquired jobs" do
33
- j = SayWhen::Store::Memory::Job.new(@valid_attributes)
34
- j.status = 'acquired'
35
- j.updated_at = 2.hours.ago
36
- SayWhen::Store::Memory::Job.reset_acquired(3600)
37
- j.status.should == 'waiting'
38
- end
39
-
40
- it "can find the next job" do
41
- j = SayWhen::Store::Memory::Job.new(@valid_attributes)
42
- next_job = SayWhen::Store::Memory::Job.acquire_next(1.day.since)
43
- next_job.should == j
44
- end
45
- end
@@ -1,54 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../../spec_helper'
2
- # require File.dirname(__FILE__) + '/../../../../lib/say_when/store/memory/trigger'
3
-
4
- # describe SayWhen::Store::Memory::Trigger do
5
-
6
- # before(:each) do
7
- # @valid_attributes = {
8
- # :expression => '0 0 12 ? * * *',
9
- # :time_zone => 'Pacific Time (US & Canada)'
10
- # }
11
- # end
12
-
13
- # it "can be instantiated" do
14
- # t = SayWhen::Store::Memory::Trigger.new(@valid_attributes)
15
- # t.should_not be_nil
16
- # end
17
-
18
- # it "sets a cron_expression" do
19
- # t = SayWhen::Store::Memory::Trigger.new(@valid_attributes)
20
- # t.cron_expression.should_not be_nil
21
- # t.cron_expression.expression.should == '0 0 12 ? * * *'
22
- # t.cron_expression.time_zone.should == 'Pacific Time (US & Canada)'
23
- # end
24
-
25
- # it "has a waiting state on instantiate" do
26
- # t = SayWhen::Store::Memory::Trigger.new(@valid_attributes)
27
- # t.status.should == 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::Store::Memory::Trigger.new(@valid_attributes)
33
- # t.status.should == SayWhen::BaseTrigger::STATE_WAITING
34
- # t.next_fire_at.should == 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::Store::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.should == ce.next_fire_at(now)
50
- # t.last_fire_at.should == now
51
- # t.status.should == SayWhen::BaseTrigger::STATE_WAITING
52
- # end
53
-
54
- # end
@@ -1,22 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
- require File.dirname(__FILE__) + '/../../../lib/say_when/triggers/once_strategy'
3
-
4
- describe SayWhen::Triggers::OnceStrategy do
5
-
6
- it "should be constucted with at option" do
7
- time_at = 1.second.ago
8
- o = SayWhen::Triggers::OnceStrategy.new({:at=>time_at})
9
- o.should_not be_nil
10
- o.once_at.should == time_at
11
- end
12
-
13
- it "should return once at only once" do
14
- time_at = 1.second.ago
15
- o = SayWhen::Triggers::OnceStrategy.new({:at=>time_at})
16
- o.should_not be_nil
17
- o.next_fire_at.should == time_at
18
- o.next_fire_at(time_at + 10.second).should be_nil
19
- o.next_fire_at(time_at - 10.second).should == time_at
20
- end
21
-
22
- end
data/spec/spec.opts DELETED
@@ -1,4 +0,0 @@
1
- --colour
2
- --format progress
3
- --loadby mtime
4
- --reverse