sidekiq_result 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e2aac7b1b6efeb70b0098d1eaa66cbd56115fbc
4
+ data.tar.gz: b07e84fde3df1bc864d7bc871011f7d73892bbdc
5
+ SHA512:
6
+ metadata.gz: c4dcfe4d9d2488b86e8c68d7235b1b4885f2b15b1ecaa9aba73bddeb0192e9a54149dc570ac573cea170076f338b822f26e0902328c9bf36fc052e89beb24769
7
+ data.tar.gz: 24cb6451126992645a9c90e358d44a5f557b6c22618507ba483629ef4b4fbb4dc27beb8f8ff745bb30005206c15fbba4a68d1646075560570867e72ccf61f0cd
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0
4
+ - 2.1
5
+ - 2.2
6
+ - jruby
7
+ - rbx-2
8
+ before_install: gem install bundler -v 1.10.5
9
+ gemfile:
10
+ - Gemfile
11
+ services: redis
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: jruby
15
+ - rvm: rbx-2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq_result.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Chris MacNaughton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Sidekiq::Result
2
+ [![Gem Version](https://badge.fury.io/rb/sidekiq_result.png)](http://badge.fury.io/rb/sidekiq_result)
3
+ [![Code Climate](https://codeclimate.com/github/chrismacnaughton/sidekiq_result.png)](https://codeclimate.com/github/chrismacnaughton/sidekiq_result)
4
+ [![Build Status](https://secure.travis-ci.org/chrismacnaughton/sidekiq_result.png)](http://travis-ci.org/chrismacnaughton/sidekiq_result)
5
+
6
+ An extension to [Sidekiq](http://github.com/mperham/sidekiq) message processing to track your jobs.
7
+
8
+ ## Installation
9
+
10
+ gem install sidekiq-result
11
+
12
+ ## Usage
13
+
14
+ ### Configuration
15
+
16
+ Configure your middleware chains, lookup [Middleware usage](https://github.com/mperham/sidekiq/wiki/Middleware)
17
+ on Sidekiq wiki for more info.
18
+
19
+ ``` ruby
20
+ require 'sidekiq'
21
+ require 'sidekiq-result'
22
+
23
+ Sidekiq.configure_server do |config|
24
+ config.server_middleware do |chain|
25
+ chain.add Sidekiq::Result::ServerMiddleware, expiration: 30.minutes # default
26
+ end
27
+ config.client_middleware do |chain|
28
+ chain.add Sidekiq::Result::ClientMiddleware
29
+ end
30
+ end
31
+ ```
32
+
33
+ After that you can use your jobs as usual and only include `Sidekiq::Result::Worker` module if you want additional functionality of passing the job's finished stateto the client.
34
+
35
+ ``` ruby
36
+ class MyJob
37
+ include Sidekiq::Worker
38
+ include Sidekiq::Result::Worker
39
+
40
+ def perform(*args)
41
+ # your code goes here
42
+ end
43
+ end
44
+ ```
45
+
46
+ To overwrite expiration on worker basis and don't use global expiration for all workers write a expiration method like this below:
47
+
48
+ ``` ruby
49
+ class MyJob
50
+ include Sidekiq::Worker
51
+ include Sidekiq::Result::Worker
52
+
53
+ def expiration
54
+ @expiration ||= 60*60*24*30 # 30 days
55
+ end
56
+
57
+ def perform(*args)
58
+ # your code goes here
59
+ end
60
+ end
61
+ ```
62
+
63
+ But keep in mind that such thing will store details of job as long as expiration is set, so it may charm your Redis storage/memory consumption. Because Redis stores all data in RAM.
64
+
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/chrismacnaughton/sidekiq_result.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sidekiq_result"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ module Sidekiq::Result
2
+ class ServerMiddleware
3
+ include Storage
4
+
5
+ # Parameterized initialization, use it when adding middleware to server chain
6
+ # chain.add Sidekiq::Result::ServerMiddleware, :expiration => 60 * 5
7
+ # @param [Hash] opts middleware initialization options
8
+ # @option opts [Fixnum] :expiration ttl for saving complete jobs
9
+ def initialize(opts = {})
10
+ @expiration = opts[:expiration]
11
+ end
12
+
13
+ # Uses sidekiq's internal jid as id
14
+ # saves result of worker's method into the specified key
15
+ # @param [Worker] worker worker instance, processed here if it's class includesResult::Worker
16
+ # @param [Array] msg job args, should have jid format
17
+ # @param [String] queue queue name
18
+ def call(worker, msg, queue)
19
+ # a way of overriding default expiration time,
20
+ # so worker wouldn't lose its data
21
+ # and it allows also to overwrite global expiration time on worker basis
22
+ if worker.respond_to? :expiration
23
+ if !worker.expiration && worker.respond_to?(:expiration=)
24
+ worker.expiration = @expiration
25
+ else
26
+ @expiration = worker.expiration
27
+ end
28
+ end
29
+ result = yield
30
+ if worker.respond_to?(:store_result?) && worker.store_result?
31
+ set_object_for_id(worker.jid, result, @expiration)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,55 @@
1
+ require 'base64'
2
+ module Sidekiq::Result::Storage
3
+ protected
4
+
5
+ # Gets the object stored at the given key
6
+ def get_object_for_id(id, redis_pool = nil)
7
+ redis_connection(redis_pool) do |conn|
8
+ decode_object_from_redis(conn.get(key(id)))
9
+ end
10
+ end
11
+
12
+ # Stores the given object in the specific key
13
+ def set_object_for_id(id, object, expiration = nil, redis_pool = nil)
14
+ object = encode_object_for_redis(object)
15
+ return if object.nil?
16
+ redis_connection(redis_pool) do |conn|
17
+ conn.setex(key(id), expiration || Sidekiq::Result::DEFAULT_EXPIRATION, object)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def encode_object_for_redis(object)
24
+ puts "About to encode: #{object}"
25
+ begin
26
+ Base64.encode64(Marshal.dump(object))
27
+ rescue
28
+ nil
29
+ end
30
+ end
31
+
32
+ def decode_object_from_redis(string)
33
+ begin
34
+ Marshal.load(Base64.decode64(string))
35
+ rescue
36
+ nil
37
+ end
38
+ end
39
+
40
+ def redis_connection(redis_pool=nil)
41
+ if redis_pool
42
+ redis_pool.with do |conn|
43
+ yield conn
44
+ end
45
+ else
46
+ Sidekiq.redis do |conn|
47
+ yield conn
48
+ end
49
+ end
50
+ end
51
+
52
+ def key(id)
53
+ "sidekiq:result:#{id}"
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Result
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module Sidekiq::Result::Worker
2
+ attr_accessor :expiration
3
+
4
+ def store_result?
5
+ true
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require 'sidekiq_result/version'
2
+ require 'sidekiq_result/storage'
3
+ require 'sidekiq_result/server_middleware'
4
+ require 'sidekiq_result/worker'
5
+ module Sidekiq::Result
6
+ extend Storage
7
+ DEFAULT_EXPIRATION = 5 * 60 # 5 minute timeouts
8
+
9
+ class << self
10
+ # Check if job has registered complete by storing key
11
+ # in it's space
12
+ def complete? id
13
+ !get_object_for_id(id).nil?
14
+ end
15
+
16
+ def result(id)
17
+ get_object_for_id(id)
18
+ end
19
+
20
+ private
21
+ end
22
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq_result/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sidekiq_result'
8
+ spec.version = Sidekiq::Result::VERSION
9
+ spec.authors = ['Chris MacNaughton']
10
+ spec.email = ['chmacnaughton@gmail.com']
11
+
12
+ spec.summary = %q{An extension to sidekiq to retrieve the job result}
13
+ spec.homepage = "https://github.com/ChrisMacNaughton/sidekiq_result"
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+ spec.add_dependency 'sidekiq', '>= 2.7'
29
+ spec.add_development_dependency 'bundler', '~> 1.10'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec'
32
+ spec.add_development_dependency 'pry'
33
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq_result
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris MacNaughton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - chmacnaughton@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .travis.yml
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/sidekiq_result.rb
100
+ - lib/sidekiq_result/server_middleware.rb
101
+ - lib/sidekiq_result/storage.rb
102
+ - lib/sidekiq_result/version.rb
103
+ - lib/sidekiq_result/worker.rb
104
+ - sidekiq_result.gemspec
105
+ homepage: https://github.com/ChrisMacNaughton/sidekiq_result
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.2
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: An extension to sidekiq to retrieve the job result
130
+ test_files: []