sidekiq-scheduler 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ require 'sidekiq/testing'
2
+
3
+ module SidekiqScheduler
4
+ module Worker
5
+ module ClassMethods
6
+ alias_method :perform_at_old, :perform_at
7
+ def perform_at(timestamp, *args)
8
+ jobs << { 'class' => self.name, 'timestamp' => timestamp.to_i, 'args' => args }
9
+ true
10
+ end
11
+
12
+ alias_method :perform_in_old, :perform_in
13
+ def perform_in(seconds_from_now, *args)
14
+ timestamp = Time.now + seconds_from_now
15
+ jobs << { 'class' => self.name, 'timestamp' => timestamp.to_i, 'args' => args }
16
+ end
17
+
18
+ alias_method :remove_delayed_old, :remove_delayed
19
+ def remove_delayed(*args)
20
+ old_jobcount = jobs.size
21
+ jobs.reject! { |job| job["class"] == self.name && job["args"] == args }
22
+ old_jobcount - jobs.size
23
+ end
24
+
25
+ alias_method :remove_delayed_from_timestamp_old, :remove_delayed_from_timestamp
26
+ def remove_delayed_from_timestamp(timestamp, *args)
27
+ old_jobcount = jobs.size
28
+ jobs.reject! { |job| job["class"] == self.name && job["timestamp"] == timestamp.to_i && job["args"] == args }
29
+ old_jobcount - jobs.size
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module SidekiqScheduler
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -11,6 +11,14 @@ module SidekiqScheduler
11
11
  def perform_in(seconds_from_now, *args)
12
12
  Sidekiq::Client.delayed_push(Time.now + seconds_from_now, 'class' => self.name, 'args' => args)
13
13
  end
14
+
15
+ def remove_delayed(*args)
16
+ Sidekiq::Client.remove_all_delayed(self.name, *args)
17
+ end
18
+
19
+ def remove_delayed_from_timestamp(timestamp, *args)
20
+ Sidekiq::Client.remove_delayed(timestamp, self.name, *args)
21
+ end
14
22
  end
15
23
  end
16
24
  end
data/test/client_test.rb CHANGED
@@ -34,6 +34,14 @@ class ClientTest < MiniTest::Unit::TestCase
34
34
  assert_equal 2, Sidekiq::Client.remove_all_delayed('Foo', 3, 4)
35
35
  end
36
36
 
37
+ it 'handles removed_delayed from a worker' do
38
+ Sidekiq::Client.delayed_push(1331284491, 'class' => 'MyWorker', 'args' => [1, 2])
39
+ Sidekiq::Client.delayed_push(1331284492, 'class' => 'MyWorker', 'args' => [3, 4])
40
+ Sidekiq::Client.delayed_push(1331284493, 'class' => 'MyWorker', 'args' => [3, 4])
41
+ Sidekiq::Client.delayed_push(1331284493, 'class' => 'MyWorker', 'args' => [5, 6])
42
+ assert_equal 2, MyWorker.remove_delayed(3, 4)
43
+ end
44
+
37
45
  it 'removes messages from specified timestamp' do
38
46
  Sidekiq::Client.delayed_push(1331284491, 'class' => 'Foo', 'args' => [1, 2])
39
47
  Sidekiq::Client.delayed_push(1331284492, 'class' => 'Foo', 'args' => [1, 2])
@@ -41,6 +49,13 @@ class ClientTest < MiniTest::Unit::TestCase
41
49
  assert_equal 1, Sidekiq.redis.llen('delayed:1331284492')
42
50
  end
43
51
 
52
+ it 'removes messages from a worker for a specified timestamp' do
53
+ Sidekiq::Client.delayed_push(1331284491, 'class' => 'MyWorker', 'args' => [1, 2])
54
+ Sidekiq::Client.delayed_push(1331284492, 'class' => 'MyWorker', 'args' => [1, 2])
55
+ assert_equal 1, MyWorker.remove_delayed_from_timestamp(1331284491, 1, 2)
56
+ assert_equal 1, Sidekiq.redis.llen('delayed:1331284492')
57
+ end
58
+
44
59
  it 'removes messages for a queue from specified timestamp' do
45
60
  Sidekiq::Client.delayed_push('foo', 1331284491, 'class' => 'Foo', 'args' => [1, 2])
46
61
  Sidekiq::Client.delayed_push('foo', 1331284492, 'class' => 'Foo', 'args' => [1, 2])
@@ -113,6 +128,5 @@ class ClientTest < MiniTest::Unit::TestCase
113
128
  @redis.verify
114
129
  end
115
130
  end
116
-
117
131
  end
118
132
  end
@@ -0,0 +1,6 @@
1
+ class DirectWorker
2
+ include Sidekiq::Worker
3
+ def perform(a, b)
4
+ a + b
5
+ end
6
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+ require 'sidekiq-scheduler/worker'
3
+
4
+ class TestingTest < MiniTest::Unit::TestCase
5
+ describe 'sidekiq-scheduler testing' do
6
+
7
+ it 'stubs the perform_* calls when in testing mode' do
8
+ begin
9
+ require 'sidekiq-scheduler/testing'
10
+ # perform_at
11
+ assert_equal 0, DirectWorker.jobs.size
12
+ assert DirectWorker.perform_at(1331759054, 1, 2)
13
+ assert_equal 1, DirectWorker.jobs.size
14
+ assert_equal 1331759054, DirectWorker.jobs[0]['timestamp']
15
+ DirectWorker.jobs.clear
16
+
17
+ # perform_in
18
+ Timecop.freeze(Time.now) do
19
+ timestamp = Time.now + 30
20
+ assert_equal 0, DirectWorker.jobs.size
21
+ assert DirectWorker.perform_in(30, 1, 2)
22
+ assert_equal 1, DirectWorker.jobs.size
23
+ assert_equal timestamp.to_i, DirectWorker.jobs[0]['timestamp']
24
+ end
25
+ ensure
26
+ # Undo override
27
+ SidekiqScheduler::Worker::ClassMethods.class_eval do
28
+ remove_method :perform_at, :perform_in, :remove_delayed, :remove_delayed_from_timestamp
29
+ alias_method :perform_at, :perform_at_old
30
+ alias_method :perform_in, :perform_in_old
31
+ alias_method :remove_delayed, :remove_delayed_old
32
+ alias_method :remove_delayed_from_timestamp, :remove_delayed_from_timestamp_old
33
+ remove_method :perform_at_old, :perform_in_old, :remove_delayed_old, :remove_delayed_from_timestamp_old
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sidekiq
16
- requirement: &70115084759380 !ruby/object:Gem::Requirement
16
+ requirement: &70095698629680 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.8.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70115084759380
24
+ version_requirements: *70095698629680
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70115084758860 !ruby/object:Gem::Requirement
27
+ requirement: &70095698629040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70115084758860
35
+ version_requirements: *70095698629040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: timecop
38
- requirement: &70115084758160 !ruby/object:Gem::Requirement
38
+ requirement: &70095698628560 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70115084758160
46
+ version_requirements: *70095698628560
47
47
  description: Light weight job scheduling extension for Sidekiq that adds support for
48
48
  queueing items in the future.
49
49
  email:
@@ -57,6 +57,7 @@ files:
57
57
  - lib/sidekiq-scheduler/cli.rb
58
58
  - lib/sidekiq-scheduler/client.rb
59
59
  - lib/sidekiq-scheduler/manager.rb
60
+ - lib/sidekiq-scheduler/testing.rb
60
61
  - lib/sidekiq-scheduler/version.rb
61
62
  - lib/sidekiq-scheduler/worker.rb
62
63
  - lib/sidekiq-scheduler.rb
@@ -69,8 +70,10 @@ files:
69
70
  - test/config.yml
70
71
  - test/fake_env.rb
71
72
  - test/manager_test.rb
73
+ - test/support/direct_worker.rb
72
74
  - test/support/my_worker.rb
73
75
  - test/test_helper.rb
76
+ - test/testing_test.rb
74
77
  homepage: https://github.com/yabawock/sidekiq-scheduler
75
78
  licenses: []
76
79
  post_install_message:
@@ -85,7 +88,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
88
  version: '0'
86
89
  segments:
87
90
  - 0
88
- hash: -1488616446890576615
91
+ hash: 3002219408512225865
89
92
  required_rubygems_version: !ruby/object:Gem::Requirement
90
93
  none: false
91
94
  requirements:
@@ -94,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
97
  version: '0'
95
98
  segments:
96
99
  - 0
97
- hash: -1488616446890576615
100
+ hash: 3002219408512225865
98
101
  requirements: []
99
102
  rubyforge_project:
100
103
  rubygems_version: 1.8.17
@@ -107,5 +110,7 @@ test_files:
107
110
  - test/config.yml
108
111
  - test/fake_env.rb
109
112
  - test/manager_test.rb
113
+ - test/support/direct_worker.rb
110
114
  - test/support/my_worker.rb
111
115
  - test/test_helper.rb
116
+ - test/testing_test.rb