sidekiq-tip 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d9c09d096026300573d79097d5df3145b88d399c
4
+ data.tar.gz: dadbd7f18d6c0c9e697c34c1e2d43d25e9cd34e7
5
+ SHA512:
6
+ metadata.gz: 54a4f3564265edaba1477ef5937b9357d87d1cf5334d8a3856e47b743444061e0a4b4a62dfb6b68aa48fd233d50993e35405e7a71e9dec70c42015e4d2ff9837
7
+ data.tar.gz: 7e358daf21505008ebf7d2ba5bd2a01a43bf58599395b7a0cb54bff11e6391618b566616a6f63510f68ab0e5749543123ee184e9113f2c0480a2e926da6cd1c1
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.sw*
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq-tip.gemspec
4
+ gemspec
5
+
6
+ group :developmnt, :test do
7
+ gem 'pry'
8
+ gem 'sidekiq', git: 'https://github.com/mperham/sidekiq.git'
9
+ gem 'minitest'
10
+ gem 'wrong'
11
+ gem 'mocha'
12
+ gem 'timecop'
13
+ gem 'multi_json'
14
+ gem 'mock_redis'
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Andrew Kumanyaev
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.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Sidekiq::Tip
2
+
3
+ Perform sidekiq jobs in time interval
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sidekiq-tip'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install sidekiq-tip
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ class SomeJob
29
+ include Sidekiq::Worker
30
+ # [0, 0, 0] == [hour, min, sec]
31
+ sidekiq_options start_at: [0, 0, 0], finish_at: [10, 0, 0]
32
+
33
+ def perform
34
+ # ...
35
+ end
36
+ end
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/[my-github-username]/sidekiq-tip/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+
6
+ rescue LoadError
7
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
8
+ end
9
+
10
+ Bundler::GemHelper.install_tasks
11
+
12
+ require 'rake/testtask'
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'lib'
16
+ t.libs << 'test'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = false
19
+ end
20
+
21
+ task :default => :test
@@ -0,0 +1,53 @@
1
+ require 'pry'
2
+
3
+ module Sidekiq
4
+ module Tip
5
+ module Server
6
+ class Middleware
7
+ def initialize(options={})
8
+ @start_at = options.fetch(:start_at, [0, 0, 0]) # hour, minutes, second
9
+ @finish_at = options.fetch(:finish_at, [23, 59, 59]) # hour, minutes, second
10
+ end
11
+
12
+ def call(worker, msg, queue)
13
+ fetch_times(msg)
14
+
15
+ if appropriate_time_interval?
16
+ yield
17
+ else
18
+ worker.class.perform_in(wait_time, msg)
19
+ end
20
+ end
21
+
22
+ #private
23
+
24
+ def fetch_times(msg)
25
+ @start_at = msg.fetch('start_at', @start_at)
26
+ @finish_at = msg.fetch('finish_at', @finish_at)
27
+ end
28
+
29
+ def appropriate_time_interval?
30
+ current_time.between?(start_time, finish_time)
31
+ end
32
+
33
+ def start_time
34
+ hour, min, sec = @start_at
35
+ Time.utc(current_time.year, current_time.month, current_time.day, hour, min, sec)
36
+ end
37
+
38
+ def finish_time
39
+ hour, min, sec = @finish_at
40
+ Time.utc(current_time.year, current_time.month, current_time.day, hour, min, sec)
41
+ end
42
+
43
+ def current_time
44
+ Time.now.utc
45
+ end
46
+
47
+ def wait_time
48
+ (start_time + 86400) - current_time
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Tip
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require "sidekiq/tip/version"
2
+ require "sidekiq/tip/server/middleware"
3
+ require 'pry'
4
+
5
+ Sidekiq.configure_server do |config|
6
+ config.server_middleware do |chain|
7
+ chain.add Sidekiq::Tip::Server::Middleware
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq/tip/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-tip"
8
+ spec.version = Sidekiq::Tip::VERSION
9
+ spec.authors = ["Andrew Kumanyaev"]
10
+ spec.email = ["me@zzet.org"]
11
+ spec.summary = %q{Perform worker in time interval}
12
+ spec.description = %q{Sometimes we need to perform some workers in time interval. For example, we want to run all jobs at night (from 2 am to 6 am).}
13
+ spec.homepage = "https://github.com/zzet/sidekiq-tip"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+ require 'pry'
3
+
4
+ class MiddlawareTest < Minitest::Test
5
+ def test_job_in_interval
6
+ t = Time.utc(2008, 9, 1, 1, 0, 0)
7
+ Timecop.travel(t)
8
+
9
+ assert { SomeJob.jobs.size == 0 }
10
+ SomeJob.perform_async
11
+ assert { SomeJob.jobs.size == 1 }
12
+ SomeJob.drain
13
+ assert { SomeJob.jobs.size == 0 }
14
+
15
+ Timecop.return
16
+ end
17
+
18
+ def test_job_not_in_interval
19
+ t = Time.utc(2008, 9, 1, 1, 0, 0)
20
+ Timecop.travel(t)
21
+
22
+ assert { SomeIvarJob.jobs.size == 0 }
23
+ SomeIvarJob.perform_async
24
+ assert { SomeIvarJob.jobs.size == 1 }
25
+ SomeJob.drain
26
+ assert { SomeIvarJob.jobs.size == 1 }
27
+
28
+ Timecop.return
29
+ end
30
+
31
+ def test_job_without_interval_settings
32
+ t = Time.utc(2008, 9, 1, 1, 0, 0)
33
+ Timecop.travel(t)
34
+
35
+ assert { SomeSimpleJob.jobs.size == 0 }
36
+ SomeSimpleJob.perform_async
37
+ assert { SomeSimpleJob.jobs.size == 1 }
38
+ SomeSimpleJob.drain
39
+ assert { SomeSimpleJob.jobs.size == 0 }
40
+
41
+ Timecop.return
42
+ end
43
+ end
@@ -0,0 +1,66 @@
1
+ # require 'simplecov'
2
+ # SimpleCov.start
3
+
4
+ # require 'coveralls'
5
+ # Coveralls.wear!
6
+
7
+ require 'minitest/autorun'
8
+ require 'minitest/pride'
9
+ require 'minitest/autorun'
10
+ require 'timecop'
11
+ require 'wrong'
12
+
13
+ require 'celluloid/current'
14
+ require 'celluloid/test'
15
+ Celluloid.boot
16
+
17
+ require 'sidekiq'
18
+ require 'sidekiq/cli'
19
+ require 'sidekiq/processor'
20
+ require 'sidekiq/util'
21
+
22
+ require 'sidekiq/testing'
23
+ Sidekiq::Testing.fake!
24
+
25
+ require 'sidekiq/tip'
26
+
27
+ Sidekiq::Testing.server_middleware do |chain|
28
+ chain.add Sidekiq::Tip::Server::Middleware
29
+ end
30
+
31
+ Wrong.config.color
32
+
33
+ require 'sidekiq/redis_connection'
34
+ REDIS_URL = ENV['REDIS_URL'] || 'redis://localhost/15'
35
+ REDIS = Sidekiq::RedisConnection.create(:url => REDIS_URL, :namespace => 'testy')
36
+
37
+ Sidekiq.configure_client do |config|
38
+ config.redis = { :url => REDIS_URL, :namespace => 'testy' }
39
+ end
40
+
41
+ class MiniTest::Test
42
+ include Wrong
43
+ end
44
+
45
+
46
+ class SomeJob
47
+ include Sidekiq::Worker
48
+
49
+ sidekiq_options start_at: [0, 0, 0], finish_at: [10, 0, 0]
50
+
51
+ def perform; end
52
+ end
53
+
54
+ class SomeIvarJob
55
+ include Sidekiq::Worker
56
+
57
+ sidekiq_options start_at: [10, 0, 0], finish_at: [15, 0, 0]
58
+
59
+ def perform; end
60
+ end
61
+
62
+ class SomeSimpleJob
63
+ include Sidekiq::Worker
64
+
65
+ def perform; end
66
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-tip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kumanyaev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Sometimes we need to perform some workers in time interval. For example,
42
+ we want to run all jobs at night (from 2 am to 6 am).
43
+ email:
44
+ - me@zzet.org
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/sidekiq/tip.rb
55
+ - lib/sidekiq/tip/server/middleware.rb
56
+ - lib/sidekiq/tip/version.rb
57
+ - sidekiq-tip.gemspec
58
+ - test/lib/middleware_test.rb
59
+ - test/test_helper.rb
60
+ homepage: https://github.com/zzet/sidekiq-tip
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.14
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Perform worker in time interval
84
+ test_files:
85
+ - test/lib/middleware_test.rb
86
+ - test/test_helper.rb
87
+ has_rdoc: