sidekiq-sqs 0.0.6a

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq-sqs.gemspec
4
+ gemspec
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara request specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jon Moses
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # Sidekiq::Sqs
2
+
3
+ This is a gem that modifies Sidekiq to use Amazon's SQS as a job store. It
4
+ still requires Redis for stats and such, and worker tracking.
5
+
6
+ It works currently. There's zero tests, and we're not using it in production, so
7
+ you might want to be careful.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'sidekiq-sqs'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sidekiq-sqs
22
+
23
+ ## Usage
24
+
25
+ Once the gem is required, (and aws-sdk is configured) simply do:
26
+
27
+ Sidekiq::Sqs.enable!
28
+
29
+ That'll turn it all on. Nothing else _should_ have to change.
30
+
31
+ ## Caveats
32
+
33
+ * Scheduling is completely broken
34
+ * ?
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,42 @@
1
+ require 'active_support'
2
+
3
+ require "sidekiq-sqs/version"
4
+ require 'sidekiq-sqs/manager'
5
+ require 'sidekiq-sqs/fetcher'
6
+ require 'sidekiq-sqs/client'
7
+ require 'sidekiq-sqs/processor'
8
+ require 'sidekiq-sqs/worker'
9
+
10
+ # TODO The retry server middleware directly writes to a retry zset.
11
+ # TODO Need a queue-prefix option to support multiple rails envs
12
+
13
+ module Sidekiq
14
+ module Sqs
15
+ module_function
16
+ def enable!
17
+ require 'sidekiq/manager'
18
+ require 'sidekiq/fetch'
19
+ require 'sidekiq/client'
20
+ require 'sidekiq/processor'
21
+
22
+ Sidekiq::Manager.send :include, Sidekiq::Sqs::Manager
23
+ Sidekiq::Fetcher.send :include, Sidekiq::Sqs::Fetcher
24
+ Sidekiq::Client.send :include, Sidekiq::Sqs::Client
25
+ Sidekiq::Processor.send :include, Sidekiq::Sqs::Processor
26
+ Sidekiq::Worker::ClassMethods.send :include, Sidekiq::Sqs::Worker
27
+
28
+ # Can't figure how to include/extend and not get a private method...
29
+ def Sidekiq.sqs
30
+ @sqs_connection ||= AWS::SQS.new
31
+ end
32
+
33
+ Sidekiq.configure_server do |config|
34
+ config.server_middleware do |chain|
35
+ chain.remove Sidekiq::Middleware::Server::RetryJobs
36
+ chain.remove Sidekiq::Middleware::Server::Timeout
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
@@ -0,0 +1,68 @@
1
+ require 'base64'
2
+ require 'zlib'
3
+
4
+ module Sidekiq
5
+ module Sqs
6
+ module Client
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ class << self
11
+ remove_method :push
12
+ remove_method :push_bulk
13
+
14
+ alias_method_chain :process_single, :sqs
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ def push(item)
20
+ normed = normalize_item(item)
21
+ normed, payload = process_single(item['class'], normed)
22
+
23
+ pushed = false
24
+ if normed['at']
25
+ # FIXME - SQS only supports a delay of 15.minutes
26
+ # probably a schedule queue? or keep this in redis??
27
+ pushed = conn.zadd('schedule', normed['at'].to_s, payload)
28
+ else
29
+ pushed = queue_or_create(normed['queue']).send_message(payload)
30
+ end if normed
31
+ pushed ? normed['jid'] : nil
32
+ end
33
+
34
+ def push_bulk(items)
35
+ normed = normalize_item(items)
36
+ payloads = items['args'].map do |args|
37
+ _, payload = process_single(items['class'], normed.merge('args' => args, 'jid' => SecureRandom.hex(12)))
38
+ payload
39
+ end.compact
40
+
41
+ pushed = false
42
+ queue = queue_or_create(normed['queue'])
43
+
44
+ payloads.each_slice(10) do |items|
45
+ ## FIXME error handling
46
+ pushed = queue.batch_send( items )
47
+ end
48
+
49
+ pushed ? payloads.size : nil
50
+ end
51
+
52
+ def queue_or_create(queue)
53
+ begin
54
+ Sidekiq.sqs.queues.named(queue.to_s)
55
+ rescue AWS::SQS::Errors::NonExistentQueue
56
+ Sidekiq.sqs.queues.create(queue.to_s)
57
+ end
58
+ end
59
+
60
+ def process_single_with_sqs(worker_class, item)
61
+ item, payload = process_single_without_sqs(worker_class, item)
62
+
63
+ return item, Base64.encode64(Zlib::Deflate.deflate(payload))
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,52 @@
1
+ module Sidekiq
2
+ module Sqs
3
+ module Fetcher
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ remove_method :fetch
8
+ alias_method_chain :initialize, :sqs
9
+ end
10
+
11
+ def initialize_with_sqs(mgr, queues, strict)
12
+ initialize_without_sqs(mgr, queues, strict)
13
+
14
+ # Fix Queue names
15
+ @queues = @queues.map {|queue| queue.gsub(/^queue:/, '') }
16
+ @unique_queues = @queues.uniq
17
+ end
18
+
19
+ def fetch
20
+ watchdog('Fetcher#fetch died') do
21
+ return if Sidekiq::Fetcher.done?
22
+
23
+ begin
24
+ queue = nil
25
+ msg = nil
26
+
27
+ ## FIXME
28
+ queues = queues_cmd
29
+ queues.pop # Last entry is TIMEOUT
30
+
31
+ msg = queues.inject(nil) do |message, queue|
32
+ next if message
33
+
34
+ Sidekiq.sqs.queues.named(queue).receive_message
35
+ end
36
+
37
+ if msg
38
+ @mgr.assign!(msg, File.basename(msg.queue.url))
39
+ else
40
+ after(0) { fetch }
41
+ end
42
+ rescue => ex
43
+ logger.error("Error fetching message from queues (#{@queues.join(', ')}): #{ex}")
44
+ logger.error(ex.backtrace.first)
45
+ sleep(self.class::TIMEOUT)
46
+ after(0) { fetch }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,29 @@
1
+ module Sidekiq
2
+ module Sqs
3
+ module Manager
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ remove_method :assign
8
+ end
9
+
10
+ def assign(msg, queue)
11
+ watchdog("Manager#assign died") do
12
+ if stopped?
13
+ # Race condition between Manager#stop if Fetcher
14
+ # is blocked on redis and gets a message after
15
+ # all the ready Processors have been stopped.
16
+ # Push the message back to redis.
17
+ #
18
+ msg.visibility_timeout = 0
19
+ else
20
+ processor = @ready.pop
21
+ @in_progress[processor.object_id] = [msg, queue]
22
+ @busy << processor
23
+ processor.process!(msg, queue)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'base64'
2
+ require 'zlib'
3
+
4
+ module Sidekiq
5
+ module Sqs
6
+ module Processor
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ alias_method_chain :process, :sqs
11
+ end
12
+
13
+ def process_with_sqs(sqs_message, queue)
14
+ begin
15
+ process_without_sqs(Zlib::Inflate.inflate(Base64.decode64(sqs_message.body)), queue)
16
+ ensure
17
+ # FIXME Maybe we want to requeue here?
18
+ sqs_message.delete
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Sqs
3
+ VERSION = "0.0.6a"
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module Sidekiq
2
+ module Sqs
3
+ module Worker
4
+ def perform_async_bulk(args)
5
+ client_push_bulk('class' => self, 'args' => args)
6
+ end
7
+
8
+ # args should be an array of single job arguments
9
+ def client_push_bulk(args)
10
+ Sidekiq::Client.push_bulk(args)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module Sidekiq
2
+ module Sqs
3
+ module TopLevel
4
+ def self.sqs
5
+ @sqs_connection ||= AWS::SQS.new
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq-sqs/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sidekiq-sqs"
8
+ gem.version = Sidekiq::Sqs::VERSION
9
+ gem.authors = ["Jon Moses"]
10
+ gem.email = ["jon@burningbush.us"]
11
+ gem.description = %q{SQS backed job store for Sidekiq. Redis is still used for stats/job worker tracking}
12
+ gem.summary = %q{SQS backed job store for Sidekiq}
13
+ gem.homepage = "http://github.com/jmoses/sidekiq-sqs"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'activesupport', '> 3.0.0'
21
+ gem.add_dependency 'sidekiq', '~> 2.3.2'
22
+ gem.add_dependency 'aws-sdk', '~> 1.6.6'
23
+
24
+ gem.add_development_dependency 'rspec'
25
+ gem.add_development_dependency 'guard'
26
+ gem.add_development_dependency 'guard-rspec'
27
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sidekiq::Sqs::Middleware::Compression do
4
+ context "Server" do
5
+ subject { Sidekiq::Sqs::Middleware::Compression::Server.new }
6
+ let(:message) { stub(body: "eJzLTS0uTkxPBQALwQLm\n") }
7
+
8
+ it "decompresses the message" do
9
+ subject.call(nil, message, nil)
10
+
11
+ message.body.should eq('message')
12
+ end
13
+ end
14
+
15
+ # context "Client" do
16
+ # subject { Sidekiq::Sqs::Middleware::Compression::Client.new }
17
+ # let(:message) { 'message' }
18
+ #
19
+ # it "compresses the message and yields" do
20
+ # expect {|b| subject.call(nil, message, :queue, &b) }.to yield_control
21
+ #
22
+ # message.should eq(Base64.encode64(Zlib::Deflate.deflate('message')))
23
+ # end
24
+ # end
25
+ end
26
+
@@ -0,0 +1,4 @@
1
+ $: << 'lib'
2
+ require 'sidekiq-sqs'
3
+
4
+
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-sqs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6a
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Jon Moses
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>'
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: sidekiq
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.3.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: aws-sdk
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.6.6
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.6.6
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: guard
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: guard-rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: SQS backed job store for Sidekiq. Redis is still used for stats/job worker
111
+ tracking
112
+ email:
113
+ - jon@burningbush.us
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - Guardfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - lib/sidekiq-sqs.rb
125
+ - lib/sidekiq-sqs/client.rb
126
+ - lib/sidekiq-sqs/fetcher.rb
127
+ - lib/sidekiq-sqs/manager.rb
128
+ - lib/sidekiq-sqs/processor.rb
129
+ - lib/sidekiq-sqs/version.rb
130
+ - lib/sidekiq-sqs/worker.rb
131
+ - lib/top_level.rb
132
+ - sidekiq-sqs.gemspec
133
+ - spec/sidekiq-sqs/middleware/compression_spec.rb
134
+ - spec/spec_helper.rb
135
+ homepage: http://github.com/jmoses/sidekiq-sqs
136
+ licenses: []
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>'
151
+ - !ruby/object:Gem::Version
152
+ version: 1.3.1
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 1.8.24
156
+ signing_key:
157
+ specification_version: 3
158
+ summary: SQS backed job store for Sidekiq
159
+ test_files:
160
+ - spec/sidekiq-sqs/middleware/compression_spec.rb
161
+ - spec/spec_helper.rb