resque-assurances 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fc5d107fd8d5b3cabce5237c1ac5fb97ff6727d
4
- data.tar.gz: 3c59a202d683fc50a0a26e6134dfbf333d2687d1
3
+ metadata.gz: c296188518096f9c7d0eef7dbb63132cb4bbfa54
4
+ data.tar.gz: 96f7d16922cbe2a37ec418f3e7f8c16b011df1c7
5
5
  SHA512:
6
- metadata.gz: ad423fa22dd344cad5a70964355cca7daf871be4c33de2ed539f0924c59e4c05506ebffc3e599d13a850acd69ffabe5912c982f930660fa622559c3103161430
7
- data.tar.gz: 9655608707479317b7c55a25b10abe5299ce6db1757af2746be7a68f92ddbd4f13522f2bee04dc9620bd4a64e66095def45ac8f7096fcdb5e7339bc5befe5cbd
6
+ metadata.gz: dd6a301c7bae1069638bc2e9893f56044abcf35fe7384cec98e5aca1b750fd1687e214c6236d569ad045576d585cbbb6e3aca23eac8fa832f04f37767b077448
7
+ data.tar.gz: b05eb5cc3fc367f87965ceb4129b8fa5f67506f065f26f77b69ac47be0db5225e4987a49436a44b4d03ebe05ed29d27747728be85564aa0562b31aec71416063
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Nathan Fixler
1
+ Copyright (c) 2014 WegoWise Inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -0,0 +1,31 @@
1
+ require 'resque'
2
+ require_relative 'resque-assurances/uniqueness'
3
+ require_relative 'resque-assurances/job_key'
4
+
5
+ module Resque
6
+ module Assurances
7
+ REDIS_UNIQUENESS_KEY = 'resque:assurances:uniqueness'
8
+
9
+ class << self
10
+ def reset!
11
+ redis.del(REDIS_UNIQUENESS_KEY)
12
+ end
13
+
14
+ def unique?(key)
15
+ !redis.sismember(REDIS_UNIQUENESS_KEY, key)
16
+ end
17
+
18
+ def set_key(key)
19
+ redis.sadd(REDIS_UNIQUENESS_KEY, key)
20
+ end
21
+
22
+ def remove_key(key)
23
+ redis.srem(REDIS_UNIQUENESS_KEY, key)
24
+ end
25
+
26
+ def redis
27
+ Resque.redis
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module Resque
2
+ module Assurances
3
+ class JobKey
4
+ def initialize(worker, *args)
5
+ @shash = generate_key(worker, *args)
6
+ end
7
+
8
+ def to_s
9
+ @shash
10
+ end
11
+
12
+ private
13
+
14
+ def generate_key(*args)
15
+ Digest::MD5.hexdigest(Resque.encode(args))
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module Resque
2
+ module Assurances
3
+ module Uniqueness
4
+ def before_enqueue_save_job_key(*args)
5
+ key = Resque::Assurances::JobKey.new(self, args)
6
+
7
+ if Resque::Assurances.unique?(key)
8
+ Resque::Assurances.set_key(key)
9
+ true
10
+ else
11
+ false
12
+ end
13
+ end
14
+
15
+ def around_perform_remove_job_key(*args)
16
+ key = Resque::Assurances::JobKey.new(self, args)
17
+ yield *args
18
+ ensure
19
+ Resque::Assurances.remove_key(key)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  module Resque
2
2
  module Assurances
3
- VERSION = '0.0.0'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -15,9 +15,9 @@ Gem::Specification.new do |s|
15
15
  s.description = 'Collection of resque plugins that individually provide specific assurances about how resque workers will behave.'
16
16
 
17
17
  s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- spec/*`.split("n")
18
+ s.test_files = `git ls-files -- spec/*`.split("\n")
19
19
 
20
20
  s.add_runtime_dependency 'resque', '>= 1.0', '< 2'
21
- s.add_development_dependency 'rspec'
22
- s.add_development_dependency 'resque_spec'
21
+ s.add_development_dependency 'rspec', '~> 3.0'
22
+ s.add_development_dependency 'resque_spec', '~> 0.16'
23
23
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resque::Assurances::JobKey do
4
+ it 'generates a unique key from different payloads' do
5
+ key1 = described_class.new(TestWorker, [42])
6
+ key2 = described_class.new(TestWorker, [43])
7
+ expect(key1.to_s).to_not eq key2.to_s
8
+ end
9
+
10
+ it 'generates a unique key from different worker classes' do
11
+ key1 = described_class.new(TestWorker, [42])
12
+ key2 = described_class.new(UniqueWorker, [42])
13
+ expect(key1.to_s).to_not eq key2.to_s
14
+ end
15
+
16
+ it 'generates the same key when worker and payloads are the same' do
17
+ key1 = described_class.new(TestWorker, [42])
18
+ key2 = described_class.new(TestWorker, [42])
19
+ expect(key1.to_s).to eq key2.to_s
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resque::Assurances::Uniqueness do
4
+ it 'enqueues workers once' do
5
+ 2.times { Resque.enqueue(UniqueWorker, 42) }
6
+ expect(UniqueWorker).to have_queue_size_of(1)
7
+ expect(UniqueWorker).to have_queued(42)
8
+ end
9
+
10
+ it 'enqueues workers without args once' do
11
+ 2.times { Resque.enqueue(UniqueWorker) }
12
+ expect(UniqueWorker).to have_queue_size_of(1)
13
+ end
14
+
15
+ it 'enqueues workers with different args' do
16
+ Resque.enqueue(UniqueWorker, 42)
17
+ Resque.enqueue(UniqueWorker, 43)
18
+ expect(UniqueWorker).to have_queue_size_of(2)
19
+ expect(UniqueWorker).to have_queued(42)
20
+ expect(UniqueWorker).to have_queued(43)
21
+ end
22
+
23
+ it 'enqueues different workers with the same args' do
24
+ Resque.enqueue(UniqueWorker, 42)
25
+ Resque.enqueue(AnotherUniqueWorker, 42)
26
+ expect(UniqueWorker).to have_queued(42)
27
+ expect(AnotherUniqueWorker).to have_queued(42)
28
+ end
29
+
30
+ it 'can be queued again after the work is performed' do
31
+ Resque.enqueue(UniqueWorker)
32
+ expect(UniqueWorker).to have_queue_size_of(1)
33
+ ResqueSpec.perform_all(UniqueWorker.queue)
34
+ expect(UniqueWorker).to have_queue_size_of(0)
35
+ Resque.enqueue(UniqueWorker)
36
+ expect(UniqueWorker).to have_queue_size_of(1)
37
+ end
38
+
39
+ it 'can be queued again if the work fails' do
40
+ Resque.enqueue(UniqueWorker, 42)
41
+ expect(UniqueWorker).to have_queue_size_of(1)
42
+ expect(UniqueWorker).to receive(:do_some_work).and_raise(StandardError)
43
+ ResqueSpec.perform_all(UniqueWorker.queue) rescue StandardError
44
+ expect(UniqueWorker).to have_queue_size_of(0)
45
+ Resque.enqueue(UniqueWorker)
46
+ expect(UniqueWorker).to have_queue_size_of(1)
47
+ end
48
+ end
@@ -0,0 +1,16 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+ require 'resque-assurances'
4
+ require 'resque_spec'
5
+ require_relative 'support/test_worker'
6
+ require_relative 'support/unique_worker'
7
+ require_relative 'support/another_unique_worker'
8
+
9
+ Resque.redis.namespace = 'resque:test'
10
+
11
+ RSpec.configure do |config|
12
+ config.before(:each) do
13
+ ResqueSpec.reset!
14
+ Resque::Assurances.reset!
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ class AnotherUniqueWorker < TestWorker
2
+ extend Resque::Assurances::Uniqueness
3
+ end
@@ -0,0 +1,15 @@
1
+ class TestWorker
2
+ class << self
3
+ def queue
4
+ '1'
5
+ end
6
+
7
+ def perform(*args)
8
+ do_some_work(*args)
9
+ end
10
+
11
+ def do_some_work(*_args)
12
+ # all done!
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ class UniqueWorker < TestWorker
2
+ extend Resque::Assurances::Uniqueness
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-assurances
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Fixler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-09 00:00:00.000000000 Z
11
+ date: 2014-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: resque
@@ -34,30 +34,30 @@ dependencies:
34
34
  name: rspec
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0'
39
+ version: '3.0'
40
40
  type: :development
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - ">="
44
+ - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '0'
46
+ version: '3.0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: resque_spec
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ">="
51
+ - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: '0.16'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ">="
58
+ - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '0'
60
+ version: '0.16'
61
61
  description: Collection of resque plugins that individually provide specific assurances
62
62
  about how resque workers will behave.
63
63
  email:
@@ -69,10 +69,19 @@ files:
69
69
  - ".gitignore"
70
70
  - ".ruby-version"
71
71
  - Gemfile
72
- - LICENSE.txt
72
+ - LICENSE
73
73
  - README.md
74
+ - lib/resque-assurances.rb
75
+ - lib/resque-assurances/job_key.rb
76
+ - lib/resque-assurances/uniqueness.rb
74
77
  - lib/resque-assurances/version.rb
75
78
  - resque-assurances.gemspec
79
+ - spec/lib/resque-assurances/job_key_spec.rb
80
+ - spec/lib/resque-assurances/uniqueness_spec.rb
81
+ - spec/spec_helper.rb
82
+ - spec/support/another_unique_worker.rb
83
+ - spec/support/test_worker.rb
84
+ - spec/support/unique_worker.rb
76
85
  homepage: https://rubygems.org/gems/resque-assurances
77
86
  licenses:
78
87
  - MIT
@@ -97,4 +106,10 @@ rubygems_version: 2.2.2
97
106
  signing_key:
98
107
  specification_version: 4
99
108
  summary: Provide assurances for resque jobs.
100
- test_files: []
109
+ test_files:
110
+ - spec/lib/resque-assurances/job_key_spec.rb
111
+ - spec/lib/resque-assurances/uniqueness_spec.rb
112
+ - spec/spec_helper.rb
113
+ - spec/support/another_unique_worker.rb
114
+ - spec/support/test_worker.rb
115
+ - spec/support/unique_worker.rb